import { mount, flushPromises } from '@vue/test-utils';
import { createTestingPinia } from '@pinia/testing';
import navigationScenarios from '@/router/router-constants/navigation-scenarios';
import routerParams from '@/router/router-constants/router-params';
import { useMainStore } from '@/store';
import vehicleCategories from '@/constants/vehicle-categories';
import VehicleDamageComponent from '@/layouts/vehicle-damage/vehicle-damage.vue';
import vehicleQuestionsMixin from '@/mixins/vehicle-questions-mixin';

const mockRoute = {
    params: {}
};
const mockRouter = {
    navigate: jest.fn()
};
jest.mock('@/mixins/vehicle-questions-mixin', () => ({
    methods: {
        navigateForward: jest.fn(),
        getPartsOrQuestions: jest.fn(() => Promise.resolve({
            data: {
                partsOrQuestions: []
            }
        }))
    }
}));
const mountOptions = {
    global: {
        mixins: [
            {
                computed: {
                    navigationScenarios() {
                        return navigationScenarios;
                    },
                    routerParams() {
                        return routerParams;
                    },
                    vehicleCategories() {
                        return vehicleCategories;
                    }
                },
                methods: {
                    getCmsContent: jest.fn(),
                    getFooterInfoBoxHeight: jest.fn(() => 80),
                    getPageNameByQueryString: jest.fn(() => ''),
                    pushEventToGA: jest.fn()
                }
            }
        ],
        mocks: {
            $route: mockRoute,
            $router: mockRouter
        },
        stubs: {
            siteHeader: true,
            siteSubHeader: true,
            siteDoorOptions: true,
            damageLocationQuestion: true,
            windshieldOptions: true,
            replaceOptionsQuestion: true,
            alert: true
        }
    }
};

beforeEach(() => {
    jest.clearAllMocks();
});
describe('vehicle-damage.vue', () => {
    test('When damage selected is a repair, navigate to coverage-statement page', async () => {
        mountOptions.global.plugins = [createTestingPinia({
            initialState: {
                main: {
                    order: {
                        damage: {
                            isRepair: true
                        }
                    }
                }
            }
        })];
        const wrapper = mount(VehicleDamageComponent, mountOptions);
        const siteFooterWrapper = wrapper.getComponent({ ref: 'siteFooter' });

        useMainStore().getSupportingItems = jest.fn().mockImplementation(() => Promise.resolve({
            data: { data: [
                {
                    description: null,
                    partNumber: 'SUPPLIES-REPAIR',
                    partType: 'REPAIR FEE'
                },
                {
                    description: null,
                    partNumber: 'WSREPAIR',
                    partType: 'REPAIR FEE'
                }
            ] }
        }));

        siteFooterWrapper.vm.$emit('forwardClicked');

        await flushPromises();
        expect(mockRouter.navigate).toHaveBeenCalledTimes(1);
        expect(mockRouter.navigate)
            .toHaveBeenCalledWith(navigationScenarios.CLICKED_FORWARD_WITH_REPAIR, mockRoute);
    });
    test('When damage selected is a replace but not windshield, navigate forward from vehicle-questions-mixin', async () => {
        mountOptions.global.plugins = [createTestingPinia({
            initialState: {
                main: {
                    order: {
                        damage: {
                            isRepair: false,
                            glassToReplace: [{ glassLocation: 'Rear', glassName: 'Stationary' }]
                        }
                    }
                }
            }
        })];
        const wrapper = mount(VehicleDamageComponent, mountOptions);
        const siteFooterWrapper = wrapper.getComponent({ ref: 'siteFooter' });

        useMainStore().getSupportingItems = jest.fn().mockImplementation(() => Promise.resolve({
            data: { data: [
                {
                    description: null,
                    partNumber: 'SUPPLIES-REPAIR',
                    partType: 'REPAIR FEE'
                },
                {
                    description: null,
                    partNumber: 'WSREPAIR',
                    partType: 'REPAIR FEE'
                }
            ] }
        }));

        siteFooterWrapper.vm.$emit('forwardClicked');

        await flushPromises();
        expect(vehicleQuestionsMixin.methods.getPartsOrQuestions).toHaveBeenCalledTimes(1);
        expect(vehicleQuestionsMixin.methods.navigateForward).toHaveBeenCalledTimes(1);
    });
    test('Error in getPartsOrQuestions call => error thrown in forwardButtonAction', async () => {
        mountOptions.global.plugins = [createTestingPinia({
            initialState: {
                main: {
                    order: {
                        damage: {
                            isRepair: false
                        },
                        vehicle: {
                            vin: 'MOCK VIN'
                        }
                    }
                }
            }
        })];
        mountOptions.data = () => ({});

        const wrapper = mount(VehicleDamageComponent, mountOptions);
        const partsQuestionsErrorResponse = {
            error: 'Error getting parts'
        };
        vehicleQuestionsMixin.methods.getPartsOrQuestions.mockImplementation(() => (
            Promise.reject(partsQuestionsErrorResponse)
        ));

        expect.assertions(1);
        try {
            await wrapper.vm.forwardButtonAction();
        } catch (e) {
            expect(e).toBe(partsQuestionsErrorResponse);
        }
    });
});
