import { vitest } from 'vitest';
import { type RouteLocation } from 'vue-router';

import { type MountingOptions, shallowMount } from '@vue/test-utils';
import sinon, { type SinonStubbedInstance } from 'sinon';

import AlertService from '@/shared/alert/alert.service';

import AuditLogDetails from './audit-log-details.vue';
import AuditLogService from './audit-log.service';

type AuditLogDetailsComponentType = InstanceType<typeof AuditLogDetails>;

let route: Partial<RouteLocation>;
const routerGoMock = vitest.fn();

vitest.mock('vue-router', () => ({
  useRoute: () => route,
  useRouter: () => ({ go: routerGoMock }),
}));

const auditLogSample = { id: 123 };

describe('Component Tests', () => {
  let alertService: AlertService;

  afterEach(() => {
    vitest.resetAllMocks();
  });

  describe('AuditLog Management Detail Component', () => {
    let auditLogServiceStub: SinonStubbedInstance<AuditLogService>;
    let mountOptions: MountingOptions<AuditLogDetailsComponentType>['global'];

    beforeEach(() => {
      route = {};
      auditLogServiceStub = sinon.createStubInstance<AuditLogService>(AuditLogService);

      alertService = new AlertService({
        i18n: { t: vitest.fn() } as any,
        toast: {
          show: vitest.fn(),
        } as any,
      });

      mountOptions = {
        stubs: {
          'font-awesome-icon': true,
          'router-link': true,
        },
        provide: {
          alertService,
          auditLogService: () => auditLogServiceStub,
        },
      };
    });

    describe('Navigate to details', () => {
      it('Should call load all on init', async () => {
        // GIVEN
        auditLogServiceStub.find.resolves(auditLogSample);
        route = {
          params: {
            auditLogId: `${123}`,
          },
        };
        const wrapper = shallowMount(AuditLogDetails, { global: mountOptions });
        const comp = wrapper.vm;
        // WHEN
        await comp.$nextTick();

        // THEN
        expect(comp.auditLog).toMatchObject(auditLogSample);
      });
    });

    describe('Previous state', () => {
      it('Should go previous state', async () => {
        auditLogServiceStub.find.resolves(auditLogSample);
        const wrapper = shallowMount(AuditLogDetails, { global: mountOptions });
        const comp = wrapper.vm;
        await comp.$nextTick();

        comp.previousState();
        await comp.$nextTick();

        expect(routerGoMock).toHaveBeenCalledWith(-1);
      });
    });
  });
});
