import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import {
  GetPendingSponsorshipRequestsUseCase,
  type GetPendingSponsorshipRequestsResult,
  type GetPendingSponsorshipRequestsInput,
  type GetPendingSponsorshipRequestsErrorCode,
} from './GetPendingSponsorshipRequestsUseCase';
import { ISponsorshipRequestRepository } from '../../domain/repositories/ISponsorshipRequestRepository';
import { ISponsorRepository } from '../../domain/repositories/ISponsorRepository';
import { SponsorshipRequest } from '../../domain/entities/SponsorshipRequest';
import { Sponsor } from '../../domain/entities/sponsor/Sponsor';
import { Money } from '../../domain/value-objects/Money';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';

describe('GetPendingSponsorshipRequestsUseCase', () => {
  let useCase: GetPendingSponsorshipRequestsUseCase;
  let sponsorshipRequestRepo: {
    findPendingByEntity: Mock;
  };
  let sponsorRepo: {
    findById: Mock;
  };
  let output: UseCaseOutputPort<GetPendingSponsorshipRequestsResult> & {
    present: Mock;
  };

  beforeEach(() => {
    sponsorshipRequestRepo = {
      findPendingByEntity: vi.fn(),
    };
    sponsorRepo = {
      findById: vi.fn(),
    };
    output = {
      present: vi.fn(),
    } as unknown as typeof output;
    useCase = new GetPendingSponsorshipRequestsUseCase(
      sponsorshipRequestRepo as unknown as ISponsorshipRequestRepository,
      sponsorRepo as unknown as ISponsorRepository,
      output,
    );
  });

  it('should present pending sponsorship requests', async () => {
    const input: GetPendingSponsorshipRequestsInput = {
      entityType: 'season',
      entityId: 'entity-1',
    };
    const request = SponsorshipRequest.create({
      id: 'req-1',
      sponsorId: 'sponsor-1',
      entityType: 'season',
      entityId: 'entity-1',
      tier: 'main',
      offeredAmount: Money.create(10000, 'USD'),
      message: 'Test message',
    });
    const sponsor = Sponsor.create({
      id: 'sponsor-1',
      name: 'Test Sponsor',
      contactEmail: 'test@example.com',
      logoUrl: 'https://example.com/logo.png',
    });

    sponsorshipRequestRepo.findPendingByEntity.mockResolvedValue([request]);
    sponsorRepo.findById.mockResolvedValue(sponsor);

    const result = await useCase.execute(input);

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toBeUndefined();

    expect(output.present).toHaveBeenCalledTimes(1);
    const presented = (output.present as Mock).mock.calls[0]?.[0] as GetPendingSponsorshipRequestsResult;

    expect(presented).toBeDefined();
    expect(presented.entityType).toBe('season');
    expect(presented.entityId).toBe('entity-1');
    expect(presented.totalCount).toBe(1);
    expect(presented.requests).toHaveLength(1);
    const summary = presented.requests[0];
    expect(summary).toBeDefined();
    expect(summary!.sponsor).toBeDefined();
    expect(summary!.sponsor!.name.toString()).toBe('Test Sponsor');
    expect(summary!.financials.offeredAmount.amount).toBe(10000);
    expect(summary!.financials.offeredAmount.currency).toBe('USD');
  });

  it('should return error when repository fails', async () => {
    const input: GetPendingSponsorshipRequestsInput = {
      entityType: 'season',
      entityId: 'entity-1',
    };
    const error = new Error('DB error');
    sponsorshipRequestRepo.findPendingByEntity.mockRejectedValue(error);

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<
      GetPendingSponsorshipRequestsErrorCode,
      { message: string }
    >;
    expect(err.code).toBe('REPOSITORY_ERROR');
    expect(err.details.message).toBe('DB error');
    expect(output.present).not.toHaveBeenCalled();
  });
});
