import { getCarpoolsByEvenement, getCarpoolsAVenir, createCarpool, deleteCarpool, updateCarpool } from './covoiturageService';
import http from '@/http';

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

  describe('getCarpoolsByEvenement', () => {
    it('shouldFetchCarpoolsForEvent', async () => {
      const mockCarpools = [
        { id: 1, ville: 'Rennes', nombre: 3, heure: '18:00', propose: 1, auteur: { id: 1, pseudo: 'testuser' } },
      ];
      (http.get as jest.Mock).mockResolvedValueOnce({ data: mockCarpools });

      const result = await getCarpoolsByEvenement(42);

      expect(http.get).toHaveBeenCalledWith('/covoiturages?evenementId=42');
      expect(result).toEqual(mockCarpools);
    });
  });

  describe('createCarpool', () => {
    it('shouldCreateCarpoolOffer', async () => {
      const mockCarpool = { id: 1, ville: 'Brest', nombre: 2, heure: null, propose: 1, auteur: { id: 1, pseudo: 'testuser' } };
      (http.post as jest.Mock).mockResolvedValueOnce({ data: mockCarpool });

      const result = await createCarpool({ evenementId: 42, ville: 'Brest', nombre: 2, propose: 1 });

      expect(http.post).toHaveBeenCalledWith('/covoiturages', {
        evenementId: 42,
        ville: 'Brest',
        nombre: 2,
        propose: 1,
      });
      expect(result).toEqual(mockCarpool);
    });
  });

  describe('deleteCarpool', () => {
    it('shouldDeleteCarpool', async () => {
      (http.delete as jest.Mock).mockResolvedValueOnce({ data: null });

      await deleteCarpool(1);

      expect(http.delete).toHaveBeenCalledWith('/covoiturages/1');
    });
  });

  it('shouldPatchOwnCarpool', async () => {
    const updated = {id: 7, ville: 'Brest', nombre: 3, heure: '18:30', propose: 0, auteur: {id: 42, pseudo: 'breizh56'}};
    (http.patch as jest.Mock).mockResolvedValue({status: 200, data: updated});

    expect(await updateCarpool(7, {ville: 'Brest', nombre: 3, heure: '18:30', propose: 0})).toEqual(updated);
    expect(http.patch).toHaveBeenCalledWith('/covoiturages/7', {ville: 'Brest', nombre: 3, heure: '18:30', propose: 0});
  });

  describe('getCarpoolsAVenir', () => {
    it('shouldFetchTheCarpoolsOfUpcomingEvents', async () => {
      const mockCarpools = [
        { id: 1, ville: 'Rennes', nombre: 3, heure: '18:00', propose: 1, auteur: { id: 1, pseudo: 'testuser' }, evenement: { eveId: 12, libelle: 'Fest-noz', date: '2026-11-07', vilNom: 'Carhaix' } },
      ];
      (http.get as jest.Mock).mockResolvedValueOnce({ data: mockCarpools });

      const result = await getCarpoolsAVenir();

      expect(http.get).toHaveBeenCalledWith('/covoiturages?aVenir=1');
      expect(result).toEqual(mockCarpools);
    });
  });
});
