import { vi, describe, expect, it, beforeAll, afterEach } from 'vitest';
import { createWrapper, axios } from 'app/test/vitest/helper';
import VnLog from 'src/components/common/VnLog.vue';

describe('VnLog', () => {
    let vm;
    const fakeLogTreeData = [
        {
            id: 2,
            originFk: 1,
            userFk: 18,
            action: 'update',
            changedModel: 'ClaimObservation',
            oldInstance: {},
            newInstance: {
                claimFk: 1,
                text: 'Waiting for customer',
            },
            creationDate: '2023-09-18T12:25:34.000Z',
            changedModelId: '1',
            changedModelValue: null,
            description: null,
            user: {
                id: 18,
                name: 'salesPerson',
                nickname: 'salesPersonNick',
                image: '4fa3ada0-3ac4-11eb-9ab8-27f6fc3b85fd',
                worker: {
                    id: 18,
                    userFk: 18,
                },
            },
        },
        {
            id: 1,
            originFk: 1,
            userFk: 18,
            action: 'update',
            changedModel: 'Claim',
            oldInstance: {
                pickup: null,
            },
            newInstance: {
                pickup: 'agency',
            },
            creationDate: '2023-09-18T12:25:34.000Z',
            changedModelId: '1',
            changedModelValue: null,
            description: null,
            user: {
                id: 18,
                name: 'salesPerson',
                nickname: 'salesPersonNick',
                image: '4fa3ada0-3ac4-11eb-9ab8-27f6fc3b85fd',
                worker: {
                    id: 18,
                    userFk: 18,
                },
            },
        },
    ];
    const mockValidations = {
        Claim: {
            locale: {
                name: 'reclamación',
            },
        },
        ClaimObservation: {
            locale: {
                name: 'observación',
            },
        },
        ClaimDms: {
            locale: {
                name: 'documento',
            },
        },
        ClaimBeginning: {
            locale: {
                name: 'comienzo',
            },
        },
    };

    beforeAll(async () => {
        axios.get.mockImplementation(() => {
            return { data: fakeLogTreeData };
        });

        vm = createWrapper(VnLog, {
            global: {
                stubs: [],
                mocks: {},
            },
            propsData: {
                model: 'Claim',
            },
        }).vm;
        vm.validations = mockValidations;
    });

    afterEach(() => {
        vi.clearAllMocks();
    });

    it('should correctly set logTree', async () => {
        vm.logTree = vm.getLogTree(fakeLogTreeData);
        expect(vm.logTree[0].originFk).toEqual(1);
        expect(vm.logTree[0].logs[0].user.name).toEqual('salesPerson');
    });

    it('should correctly set the selectedFilters when filtering', () => {
        vm.searchInput = '1';
        vm.userSelect = '21';
        vm.checkboxOptions.insert.selected = true;
        vm.checkboxOptions.update.selected = true;

        vm.selectFilter('search');
        vm.selectFilter('userSelect');

        expect(vm.selectedFilters.changedModelId).toEqual('1');
        expect(vm.selectedFilters.userFk).toEqual('21');
        expect(vm.selectedFilters.action).toEqual({ inq: ['insert', 'update'] });
    });

    it('should correctly set the date from', () => {
        vm.dateFrom = '18-09-2023';
        vm.selectFilter('date', 'from');
        expect(vm.selectedFilters.creationDate.between).toEqual([
            new Date('2023-09-18T00:00:00.000Z'),
            new Date('2023-09-18T21:59:59.999Z'),
        ]);
    });
});
