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

describe('GetPendingSponsorshipRequestsUseCase', () => {
  let useCase: GetPendingSponsorshipRequestsUseCase;
  let sponsorshipRequestRepo: {
    findPendingByEntity: Mock;
  };
  let sponsorRepo: {
    findById: Mock;
  };

  beforeEach(() => {
    sponsorshipRequestRepo = {
      findPendingByEntity: vi.fn(),
    };
    sponsorRepo = {
      findById: vi.fn(),
    };
    useCase = new GetPendingSponsorshipRequestsUseCase(
      sponsorshipRequestRepo as unknown as SponsorshipRequestRepository,
      sponsorRepo as unknown as SponsorRepository,
    );
  });

  it('should return 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);
    const successResult = result.unwrap();

    expect(successResult).toBeDefined();
    expect(successResult.entityType).toBe('season');
    expect(successResult.entityId).toBe('entity-1');
    expect(successResult.totalCount).toBe(1);
    expect(successResult.requests).toHaveLength(1);
    const summary = successResult.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');
  });
});
