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 PrenotazioneDetails from './prenotazione-details.vue';
import PrenotazioneService from './prenotazione.service';

type PrenotazioneDetailsComponentType = InstanceType<typeof PrenotazioneDetails>;

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

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

const prenotazioneSample = { id: 123 };

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

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

  describe('Prenotazione Management Detail Component', () => {
    let prenotazioneServiceStub: SinonStubbedInstance<PrenotazioneService>;
    let mountOptions: MountingOptions<PrenotazioneDetailsComponentType>['global'];

    beforeEach(() => {
      route = {};
      prenotazioneServiceStub = sinon.createStubInstance<PrenotazioneService>(PrenotazioneService);

      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,
          prenotazioneService: () => prenotazioneServiceStub,
        },
      };
    });

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

        // THEN
        expect(comp.prenotazione).toMatchObject(prenotazioneSample);
      });
    });

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

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

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