import {getFormationSession} from './sessionService';
import http from '@/http';

const mockedHttp = http as jest.Mocked<typeof http>;

describe('sessionService', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  describe('getFormationSession', () => {
    it('returnsSessionDataWhenExists', async () => {
      const mockApiResponse = {
        data: {
          fssId: 3796,
          fssDateDebut: '2026-04-14',
          fssDateFin: '2026-04-14',
          fssHeureDebut: '2026-04-14T20:00:00Z',
          fssHeureFin: '2026-04-14T22:00:00Z',
          fssDescription: 'desc',
          fssTarifs: '2€',
          fssAdressestage: 0,
          fssFod: {fodNom: 'Flûte'},
          fssSoustitre: '',
          fssFst: {fstId: 2491},
          animateurs: [],
        },
      };
      mockedHttp.get.mockResolvedValue({ ...mockApiResponse, status: 200 });

      const result = await getFormationSession('3796');

      expect(result.fssId).toBe(3796);
      expect(result.fssFst.fstId).toBe(2491);
      expect(mockedHttp.get).toHaveBeenCalledWith('/formation_sessions/3796');
    });

    it('throwsWhenIdEmpty', async () => {
      await expect(getFormationSession('')).rejects.toThrow('fssId is required');
      expect(mockedHttp.get).not.toHaveBeenCalled();
    });

    it('throwsNotFoundOn404', async () => {
      mockedHttp.get.mockRejectedValue({response: {status: 404}});

      await expect(getFormationSession('999')).rejects.toThrow(/not found/i);
    });
  });
});
