// Components
import tpaConfirmation from '@/layouts/tpa-confirmation/tpa-confirmation.vue';

// Supporting Files
import { getMountOptions } from '@/helpers/unit-test-helper.js';
import { getDefaultState, useMainStore } from '@/store/index.js';
import settleAllPromises from '@/helpers/layout-helper.js';
import { fetchCmsContentForPage } from '@/helpers/cms-content-helper';
import { mount } from '@vue/test-utils';
import { createTestingPinia } from '@pinia/testing';

jest.mock('@/helpers/layout-helper.js', () => jest.fn());

jest.mock('@/helpers/cms-content-helper', () => ({
    fetchCmsContentForPage: jest.fn(),
    processIfStatements: jest.fn(),
    doesCopyContainRouterLink: jest.fn(),
    splitCopyOnCMSPlaceHolder: jest.fn().mockImplementation(() => 'test'),
    getRouterLinkRouteFromCopy: jest.fn(),
    getRouterLinkDisplayTextFromCopy: jest.fn(),
    getPhoneLinkHtmlStringFromPhoneNumber: jest.fn(),
    doesCopyContainTextLink: jest.fn()
}));

const mockMixin = {
    methods: {
        getCmsContent: jest.fn().mockImplementation(() => ''),
        setCmsContent: jest.fn()
    }
};

const footerStub = {
    render: () => {},
    methods: {
        updateButtonText: jest.fn()
    }
};

function getMountedComponent(mainInitialState = {}, initialData = {}, methodToRun = () => {}) {
    const mountOptions = getMountOptions({
        router: {
            navigate: jest.fn(),
            navigateToExternalUrl: jest.fn()
        }
    });

    mountOptions.global.stubs = {
        siteFooter: footerStub
    };
    const testingPinia = createTestingPinia({
        initialState: {
            main: mainInitialState
        }
    });
    const store = useMainStore(testingPinia);
    store.order = getDefaultState().order;
    store.hasSubmittedOrder = jest.fn().mockReturnValue(true);
    methodToRun();

    mountOptions.global.plugins = [testingPinia];
    mountOptions.mixins = [mockMixin];
    mountOptions.data = () => (
        initialData
    );

    const apiResponses = {
        supportingItems: []
    };
    const apiPromise = Promise.resolve(apiResponses);
    settleAllPromises.mockImplementation(() => apiPromise);
    fetchCmsContentForPage.mockImplementation(() => Promise.resolve());

    const wrapper = mount(tpaConfirmation, mountOptions);
    return { wrapper };
}

const defaultOrder = {
    serviceLocation: {
        provider: {
            companyName: 'Hershey',
            phoneNumber: '726-117-0377'
        }
    },
    currentDeductible: 479,
    isVerified: false,
    carrierPhoneNumber: '757-277-0388',
    vehicle: {
        imageUrl: 'url for image',
        category: 'car'
    }
};

describe('TPAConfirmation.vue', () => {
    describe('Rendering', () => {
        let wrapper;
        let mockStoreActions;
        beforeEach(() => {
            mockStoreActions = () => {
                useMainStore().getSubmittedOrder = jest.fn().mockImplementation(() => defaultOrder);
            };
            wrapper = getMountedComponent({}, {}, mockStoreActions).wrapper;
        });
        test('Should render Site Header', () => {
            // Act
            const siteHeader = wrapper.findComponent({ ref: 'siteHeader' });

            // Assert
            expect(siteHeader.exists()).toBe(true);
        });
        test('Should render Confirmation Body One', () => {
            // Act
            const bodyOne = wrapper.findComponent({ ref: 'tpaConfirmationBodyOne' });

            // Assert
            expect(bodyOne.exists()).toBe(true);
        });
        test('Should render Confirmation Body Two', () => {
            // Act
            const bodyTwo = wrapper.findComponent({ ref: 'tpaConfirmationBodyTwo' });

            // Assert
            expect(bodyTwo.exists()).toBe(true);
        });
        test('If Advanced flow, should display Site Footer', () => {
            // Arrange
            const carrierReturnUrl = 'testURL';
            const initialStore = {
                issConfig: {
                    successReturnURL: carrierReturnUrl
                }
            };
            wrapper = getMountedComponent(initialStore, {}, mockStoreActions).wrapper;

            // Act
            const siteFooter = wrapper.findComponent({ ref: 'siteFooter' });

            // Assert
            expect(siteFooter.exists()).toBe(true);
        });
        test('If Essential flow, should not display Site Footer', () => {
            // Act
            const siteFooter = wrapper.findComponent({ ref: 'siteFooter' });

            // Assert
            expect(siteFooter.isVisible()).toBe(false);
        });
    });
    describe('Methods', () => {
        describe('Navigation', () => {
            const mockStoreActions = () => {
                useMainStore().getSubmittedOrder = jest.fn().mockImplementation(() => defaultOrder);
            };
            test('If Advanced flow, forward button action navigates to carrier URL', () => {
                // Arrange
                const carrierReturnUrl = 'testURL';
                const initialStore = {
                    issConfig: {
                        successReturnURL: carrierReturnUrl
                    }
                };
                const { wrapper } = getMountedComponent(initialStore, {}, mockStoreActions);

                // Act
                wrapper.vm.forwardButtonAction();

                // Assert
                expect(wrapper.vm.$router.navigateToExternalUrl).toHaveBeenCalledWith(carrierReturnUrl);
            });
        });
    });
});
