import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
import { Sponsor } from '../../domain/entities/sponsor/Sponsor';
import type { SponsorRepository } from '../../domain/repositories/SponsorRepository';
import { GetSponsorsUseCase } from './GetSponsorsUseCase';

describe('GetSponsorsUseCase', () => {
  let useCase: GetSponsorsUseCase;
  let sponsorRepository: {
    findAll: Mock;
  };

  beforeEach(() => {
    sponsorRepository = {
      findAll: vi.fn(),
    };
    useCase = new GetSponsorsUseCase(sponsorRepository as unknown as SponsorRepository);
  });

  it('should return all sponsors', async () => {
    const sponsors = [
      Sponsor.create({
        id: 'sponsor-1',
        name: 'Sponsor One',
        contactEmail: 'one@example.com',
        logoUrl: 'https://example.com/logo1.png',
      }),
      Sponsor.create({
        id: 'sponsor-2',
        name: 'Sponsor Two',
        contactEmail: 'two@example.com',
      }),
    ];

    sponsorRepository.findAll.mockResolvedValue(sponsors);

    const result = await useCase.execute();

    expect(result.isOk()).toBe(true);
    const presented = result.unwrap();
    expect(presented.sponsors).toEqual(sponsors);
  });

  it('should return error on repository failure', async () => {
    sponsorRepository.findAll.mockRejectedValue(new Error('DB error'));

    const result = await useCase.execute();

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({
      code: 'REPOSITORY_ERROR',
      details: { message: 'DB error' },
    });
  });
});
