import {
    deductibleForSelectedVehicle,
    endorsementsForSelectedVehicle,
    noCoverageForSelectedVehicle,
    repairWaivedForSelectedVehicle
} from '@/helpers/policy-vehicle-helper';
import { getRandomInt, getRandomString } from '@/helpers/data-generation';
import endorsementOptions from '@/constants/endorsement-options';

describe('policy vehicle helper', () => {
    describe('noCoverageForSelectedVehicle function', () => {
        it('Null vehicle => returns true', async () => {
            // Arrange
            const vehicle = null;

            // Act
            const result = noCoverageForSelectedVehicle(vehicle);

            // Assert
            expect(result).toBeTruthy();
        });

        it('Empty vehicle coverages list => returns true', async () => {
            // Arrange
            const vehicle = {
                vin: getRandomString(17, 17),
                coverages: []
            };

            // Act
            const result = noCoverageForSelectedVehicle(vehicle);

            // Assert
            expect(result).toBeTruthy();
        });

        it('Non-empty vehicle coverages list => returns false', async () => {
            // Arrange
            const vehicle = {
                vin: getRandomString(17, 17),
                coverages: [
                    {
                        deductible: 0
                    }
                ]
            };

            // Act
            const result = deductibleForSelectedVehicle(vehicle);

            // Assert
            expect(result).toBeFalsy();
        });
    });

    describe('deductibleForSelectedVehicle function', () => {
        it('Null vehicle => returns undefined', async () => {
            // Arrange
            const vehicle = null;

            // Act
            const result = deductibleForSelectedVehicle(vehicle);

            // Assert
            expect(result).toBe(undefined);
        });

        it('Vehicle match with empty coverages list => 0 returned', async () => {
            // Arrange
            const vehicle = {
                vin: getRandomString(17, 17),
                coverages: []
            };

            // Act
            const result = deductibleForSelectedVehicle(vehicle);

            // Assert
            expect(result).toBe(0);
        });

        it('Coverages list non empty => deductible from first coverage returned', async () => {
            // Arrange
            const firstDeductible = getRandomInt(1, 1000);
            const vehicle = {
                vin: getRandomString(17, 17),
                coverages: [
                    {
                        deductible: firstDeductible
                    },
                    {
                        deductible: getRandomInt(1, 1000)
                    }
                ]
            };

            // Act
            const result = deductibleForSelectedVehicle(vehicle);

            // Assert
            expect(result).toBe(firstDeductible);
        });
    });

    describe('repairWaivedForSelectedVehicle function', () => {
        it('Null vehicle => returns false', async () => {
            // Arrange
            const vehicle = null;

            // Act
            const result = repairWaivedForSelectedVehicle(vehicle);

            // Assert
            expect(result).toBeFalsy();
        });

        it('Endorsements list empty => false returned', async () => {
            // Arrange
            const vehicle = {
                vin: getRandomString(17, 17),
                endorsements: []
            };

            // Act
            const result = repairWaivedForSelectedVehicle(vehicle);

            // Assert
            expect(result).toBeFalsy();
        });

        it('Endorsements list non-empty, not containing repair waived => false returned', async () => {
            // Arrange
            const vehicle = {
                vin: getRandomString(17, 17),
                endorsements: [endorsementOptions.EDUCATOR, endorsementOptions.PARKING_GUARD]
            };

            // Act
            const result = repairWaivedForSelectedVehicle(vehicle);

            // Assert
            expect(result).toBeFalsy();
        });

        it('Endorsements list contains repair waived => true returned', async () => {
            // Arrange
            const vehicle = {
                vin: getRandomString(17, 17),
                endorsements: [
                    endorsementOptions.EDUCATOR,
                    endorsementOptions.REPAIR_WAIVED,
                    endorsementOptions.PARKING_GUARD
                ]
            };

            // Act
            const result = repairWaivedForSelectedVehicle(vehicle);

            // Assert
            expect(result).toBeTruthy();
        });
    });

    describe('endorsementsForSelectedVehicle function', () => {
        it('Null vehicle => returns empty array', async () => {
            // Arrange
            const vehicle = null;

            // Act
            const result = endorsementsForSelectedVehicle(vehicle);

            // Assert
            expect(result).toStrictEqual([]);
        });

        it('Null endorsements => returns empty array', async () => {
            // Arrange
            const vehicle = {
                vin: getRandomString(17, 17),
                endorsements: null
            };

            // Act
            const result = endorsementsForSelectedVehicle(vehicle);

            // Assert
            expect(result).toStrictEqual([]);
        });

        it('Endorsements list non-empty, not containing repair waived => false returned', async () => {
            // Arrange
            const endorsements = [endorsementOptions.EDUCATOR, endorsementOptions.PARKING_GUARD];
            const vehicle = {
                vin: getRandomString(17, 17),
                endorsements
            };

            // Act
            const result = endorsementsForSelectedVehicle(vehicle);

            // Assert
            expect(result).toStrictEqual(endorsements);
        });
    });
});
