/**
 * Tests for favoriteService
 */

import { getFavorites, addApiFavorite, removeApiFavorite } from './favoriteService';
import http from '@/http';

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

  describe('getFavorites', () => {
    it('shouldCallApiFavorisEndpoint', async () => {
      const mockFavorites = [
        { id: 1, cibleId: 42, tableCible: 'evenement', libelle: 'Fest Deiz · 15/06/2026 · Moustéru' },
      ];
      (http.get as jest.Mock).mockResolvedValueOnce({ data: mockFavorites });

      const favorites = await getFavorites();

      expect(http.get).toHaveBeenCalledWith('/favoris');
      expect(favorites).toEqual(mockFavorites);
    });

    it('shouldDisplayResolvedLabelFromApi', async () => {
      const mockFavorites = [
        { id: 1, cibleId: 42, tableCible: 'evenement', libelle: 'Fest Deiz · 15/06/2026 · Moustéru' },
        { id: 2, cibleId: 123, tableCible: 'annuaire_entree', libelle: 'Bagad Kemper' },
      ];
      (http.get as jest.Mock).mockResolvedValueOnce({ data: mockFavorites });

      const favorites = await getFavorites();

      expect(favorites[0].libelle).toBe('Fest Deiz · 15/06/2026 · Moustéru');
      expect(favorites[1].libelle).toBe('Bagad Kemper');
    });
  });

  describe('addApiFavorite', () => {
    it('shouldCallApiToAddFavorite', async () => {
      const mockFavorite = { id: 1, cibleId: 42, tableCible: 'evenement', libelle: 'Fest Deiz · 15/06/2026 · Moustéru' };
      (http.post as jest.Mock).mockResolvedValueOnce({ data: mockFavorite });

      const result = await addApiFavorite(42, 'evenement');

      expect(http.post).toHaveBeenCalledWith('/favoris', {
        cibleId: 42,
        tableCible: 'evenement',
      });
      expect(result).toEqual(mockFavorite);
    });
  });

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

      await removeApiFavorite(1);

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