import { getParticipations, registerParticipation, cancelParticipation, getMyParticipations } from './participationService';
import http from '@/http';

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

  describe('getMyParticipations', () => {
    it('shouldFetchCurrentUserParticipations', async () => {
      const mockData = [
        { id: 1, evenementId: 42, eventDate: '2026-03-15', eventTitle: 'Fest-noz à Quimper', eventLocation: 'Quimper' },
        { id: 2, evenementId: 43, eventDate: '2026-04-01', eventTitle: 'Kan ar Bobl', eventLocation: 'Pontivy' },
      ];
      (http.get as jest.Mock).mockResolvedValueOnce({ data: mockData });

      const result = await getMyParticipations();

      expect(http.get).toHaveBeenCalledWith('/participations/me');
      expect(result).toEqual(mockData);
    });
  });

  describe('getParticipations', () => {
    it('shouldFetchParticipationsForEvent', async () => {
      const mockData = { count: 2, participants: [{ id: 1, pseudo: 'alice' }, { id: 2, pseudo: 'bob' }] };
      (http.get as jest.Mock).mockResolvedValueOnce({ data: mockData });

      const result = await getParticipations(42);

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

  describe('registerParticipation', () => {
    it('shouldRegisterParticipation', async () => {
      const mockResponse = { id: 1, evenementId: 42, pseudo: 'testuser' };
      (http.post as jest.Mock).mockResolvedValueOnce({ data: mockResponse });

      const result = await registerParticipation(42);

      expect(http.post).toHaveBeenCalledWith('/participations', { evenementId: 42 });
      expect(result).toEqual(mockResponse);
    });
  });

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

      await cancelParticipation(1);

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