import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { GetSponsorsUseCase } from './GetSponsorsUseCase';
import { ISponsorRepository } from '../../domain/repositories/ISponsorRepository';
import { Sponsor } from '../../domain/entities/sponsor/Sponsor';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';

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

  beforeEach(() => {
    sponsorRepository = {
      findAll: vi.fn(),
    };
    output = {
      present: vi.fn(),
    };
    useCase = new GetSponsorsUseCase(
      sponsorRepository as unknown as ISponsorRepository,
      output as unknown as UseCaseOutputPort<unknown>,
    );
  });

  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);
    expect(result.unwrap()).toBeUndefined();
    expect(output.present).toHaveBeenCalledWith({ 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',
      message: 'Failed to fetch sponsors',
    });
    expect(output.present).not.toHaveBeenCalled();
  });
});