import { TestBed } from '@angular/core/testing';
import { SectorFacade } from '@app/adapters/sectors/sector.facade';
import { SECTOR_REPOSITORY_TOKEN } from '@app/infrastructure/sectors/sector-repository.token';
import { FakeSectorRepository } from '@app/testing/domain/sectors/fake-sector.repository';
import { fakeSectors } from '@app/testing/sector.mock';
import { ToastrService } from 'ngx-toastr';
import { mockToastR } from '@app/testing/toastr.mock';

describe('SectorFacade', () => {
  let sectorFacade: SectorFacade;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        SectorFacade,
        { provide: SECTOR_REPOSITORY_TOKEN, useClass: FakeSectorRepository },
        { provide: ToastrService, useValue: mockToastR },
      ],
    });

    sectorFacade = TestBed.inject(SectorFacade);
  });

  // ------------------------------------------
  // 🔹 TEST : Chargement des secteurs (load)
  // ------------------------------------------
  it("devrait charger les secteurs d'activité ", () => {
    sectorFacade.load();

    setTimeout(() => {
      expect(sectorFacade.sectors().length).toBe(fakeSectors.length);
      expect(sectorFacade.loading().isLoading).toBe(false);
      expect(sectorFacade.error().hasError).toBe(false);
    }, 0);
  });

  // ------------------------------------------
  // 🔹 TEST : Chargement d’un secteur unique
  // ------------------------------------------
  it('devrait charger un secteur par son ID', () => {
    const sectorId = '1';
    sectorFacade.loadOne(sectorId);

    setTimeout(() => {
      expect(sectorFacade.sector()).toBe(fakeSectors.find((s) => s.id === sectorId));
      expect(sectorFacade.error().hasError).toBe(false);
    }, 0);
  });
});
