import { FakeSectorRepository } from '@app/testing/domain/sectors/fake-sector.repository';
import { PbSectorRepository } from '@app/infrastructure/sectors/pb-sector.repository';
import { fakeSectors } from '@app/testing/sector.mock';
import PocketBase from 'pocketbase';

jest.mock('pocketbase'); // on mock le module entier

describe('SectorRepository', () => {
  let sectorRepo: FakeSectorRepository;
  let mockCollection: any;
  let mockPocketBase: any;

  beforeEach(() => {
    // Création d’un faux client PocketBase avec des méthodes mockées
    mockPocketBase = {
      collection: jest.fn().mockReturnValue({
        getFullList: jest.fn(),
        getOne: jest.fn(),
      }),
    };

    // 👇 On remplace la classe importée par notre version mockée
    (PocketBase as jest.Mock).mockImplementation(() => mockPocketBase);

    // on récupère une "collection" simulée
    mockCollection = mockPocketBase.collection('sectors');

    // on instancie le repository à tester
    sectorRepo = new PbSectorRepository();
  });

  // ------------------------------------------
  // 🔹 TEST : list()
  // ------------------------------------------
  it('devrait appeler pb.collection("sectors").getFullList() ', () => {
    mockCollection.getFullList.mockResolvedValue(fakeSectors);

    sectorRepo.list().subscribe((result) => {
      expect(mockPocketBase.collection).toHaveBeenCalledWith('sectors');
      expect(result).toEqual(fakeSectors);
    });
  });

  // ------------------------------------------
  // 🔹 TEST : getOne()
  // ------------------------------------------
  it('devrait appeler pb.collection("sectors").getFirstListItem() ', () => {
    const sectorId = 'sector_001';

    mockCollection.getOne.mockResolvedValue(fakeSectors.find((s) => s.id === sectorId));

    sectorRepo.getOne(sectorId).subscribe((result) => {
      expect(mockCollection.getFirstListItem).toHaveBeenCalledWith(`sectors="${sectorId}"`);
      expect(result).toEqual(fakeSectors[0]);
    });
  });
});
