import policyHolderDetails from '@/layouts/policy-holder-details/policy-holder-details.vue';

// Supporting files
import baseMixin from '@/mixins/base-mixin';
import { shallowMount } from '@vue/test-utils';
import settleAllPromises from '@/helpers/layout-helper.js';
import { getMountOptions } from '@/helpers/unit-test-helper.js';
import { useMainStore } from '@/store';
import navigationScenarios from '@/router/router-constants/navigation-scenarios';

// Mock our module for promises.
jest.mock('@/helpers/layout-helper.js', () => jest.fn());

// Mock fetchCmsContentForPage
jest.mock('@/helpers/cms-content-helper', () => ({
    fetchCmsContentForPage: jest.fn()
}));

/** @ignore */
function setupMocks() {
    const wrapper = shallowMount(
        policyHolderDetails,
        getMountOptions({
            router: {
                navigate: jest.fn()
            },
            mainStore: {
                order: {
                    customer: {
                        emailAddress: 'test@test.com'
                    }
                }
            }
        })
    );
    const policies = [{
        vehicles: [
            {
                vin: 'TEST_VIN'
            },
            {
                vin: 'TEST_VIN2'
            }
        ]
    }
    ];
    useMainStore().getCoveragePolicyInfo = jest.fn().mockImplementation(() => Promise.resolve({
        data: {
            policies
        }
    }));

    const apiResponses = {
        policyLookupResponse: {
            policies
        }
    };

    settleAllPromises.mockImplementation(() => apiResponses);

    wrapper.vm.getCmsContent = jest.fn().mockImplementation(() => '');
    wrapper.vm.setCmsContent = jest.fn();
    wrapper.vm.$router.navigateWithSpinner = jest.fn();
    wrapper.vm.navigateBack = baseMixin.methods.navigateBack;
    wrapper.vm.$refs.siteFooter.updateButtonText = jest.fn();
    wrapper.vm.$refs.siteFooter.disableForwardButton = jest.fn();
    wrapper.vm.$refs.siteFooter.enableForwardAction = jest.fn();

    return { wrapper };
}

describe('policy-holder-details.vue', () => {
    test('Should render policy-holder-details sub-components (policy holder first name, last name, street address etc.)', async () => {
        // Arrange
        const { wrapper } = setupMocks({});
        // Act
        const policyHolderFirstName = wrapper.findComponent({ ref: 'policyHolderFirstName' });
        const policyHolderLastName = wrapper.findComponent({ ref: 'policyHolderLastName' });
        const addressQuestions = wrapper.findComponent({ ref: 'addressQuestions' });

        // Assert
        expect(policyHolderFirstName.exists()).toBe(true);
        expect(policyHolderLastName.exists()).toBe(true);
        expect(addressQuestions.exists()).toBe(true);
    });
});

describe('navigation', () => {
    test('if the back button is clicked, navigate back', async () => {
        // Arrange
        const { wrapper } = setupMocks({
        });

        // Act
        await wrapper.vm.navigateBack();

        // Assert
        expect(wrapper.vm.$router.navigateWithSpinner).toHaveBeenCalled();
    });

    test('if the car entered matches one of the vehicles found navigate forward', async () => {
        // Arrange
        const mockRegistrationAddress = {
            streetAddress: '1234 Main St',
            city: 'Columbus',
            state: 'OH',
            zipCode: '43215'
        };

        const { wrapper } = setupMocks();

        await wrapper.setData({
            firstName: 'KK',
            lastName: 'KK',
            customerQuestions: {
                addressQuestions: mockRegistrationAddress
            },
            isCoverageEnabled: false
        });

        wrapper.vm.navigateForward = jest.fn();

        // Act
        await wrapper.vm.forwardButtonAction();

        // Assert
        expect(wrapper.vm.navigateForward).toHaveBeenCalled();
    });

    test('if the coverage policy verified navigate forward to vehicle damage page', async () => {
        // Arrange
        const mockRegistrationAddress = {
            streetAddress: '1234 Main St',
            city: 'Columbus',
            state: 'OH',
            zipCode: '43215'
        };

        const { wrapper } = setupMocks();

        await wrapper.setData({
            firstName: 'KK',
            lastName: 'KK',
            customerQuestions: {
                addressQuestions: mockRegistrationAddress
            },
            isCoverageEnabled: true
        });

        useMainStore().order.policy.policyNumber = 'p_0001';
        useMainStore().order.policy.dateOfLoss = '2022-01-28';
        useMainStore().order.vehicle.policyVehicleId = 0;

        // Act

        await wrapper.vm.forwardButtonAction();

        // Assert
        expect(wrapper.vm.$router.navigate).toHaveBeenCalledWith(
            navigationScenarios.CLICKED_FORWARD_POLICY_VERIFIED,
            undefined
        );
    });

    test('if the coverage policy unverified navigate forward to vehicle damage page', async () => {
        // Arrange
        const mockRegistrationAddress = {
            streetAddress: '1234 Main St',
            city: 'Columbus',
            state: 'OH',
            zipCode: '43215'
        };

        const { wrapper } = setupMocks();

        await wrapper.setData({
            firstName: 'KK',
            lastName: 'KK',
            customerQuestions: {
                addressQuestions: mockRegistrationAddress
            },
            isCoverageEnabled: true
        });

        useMainStore().order.policy.policyNumber = 'p_0001';
        useMainStore().order.policy.dateOfLoss = '2022-01-28';
        useMainStore().order.vehicle.policyVehicleId = null;

        // Act

        await wrapper.vm.forwardButtonAction();

        // Assert
        expect(wrapper.vm.$router.navigate).toHaveBeenCalledWith(
            navigationScenarios.CLICKED_FORWARD_POLICY_UNVERIFIED,
            undefined
        );
    });

    test('if the coverage policy verified navigate back to welcome page', async () => {
        // Arrange
        const mockRegistrationAddress = {
            streetAddress: '1234 Main St',
            city: 'Columbus',
            state: 'OH',
            zipCode: '43215'
        };

        const { wrapper } = setupMocks();

        await wrapper.setData({
            firstName: 'KK',
            lastName: 'KK',
            customerQuestions: {
                addressQuestions: mockRegistrationAddress
            },
            isCoverageEnabled: true
        });

        useMainStore().order.policy.policyNumber = 'p_0001';
        useMainStore().order.policy.dateOfLoss = '2022-01-28';
        useMainStore().order.vehicle.policyVehicleId = 0;

        // Act

        await wrapper.vm.backButtonAction();

        // Assert
        expect(wrapper.vm.$router.navigate).toHaveBeenCalledWith(
            navigationScenarios.CLICKED_BACK_POLICY_VERIFIED,
            undefined
        );
    });
    test('if the coverage policy unverified navigate back to vehicle-selection page', async () => {
        // Arrange
        const mockRegistrationAddress = {
            streetAddress: '1234 Main St',
            city: 'Columbus',
            state: 'OH',
            zipCode: '43215'
        };

        const { wrapper } = setupMocks();

        await wrapper.setData({
            firstName: 'KK',
            lastName: 'KK',
            customerQuestions: {
                addressQuestions: mockRegistrationAddress
            },
            isCoverageEnabled: true
        });

        useMainStore().order.policy.policyNumber = 'p_0001';
        useMainStore().order.policy.dateOfLoss = '2022-01-28';
        useMainStore().order.vehicle.policyVehicleId = null;

        // Act

        await wrapper.vm.backButtonAction();

        // Assert
        expect(wrapper.vm.$router.navigate).toHaveBeenCalledWith(
            navigationScenarios.CLICKED_BACK_POLICY_UNVERIFIED,
            undefined
        );
    });
});
