import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { GetPendingSponsorshipRequestsUseCase } 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';
import { Money } from '../../domain/value-objects/Money';

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 ISponsorshipRequestRepository,
      sponsorRepo as unknown as ISponsorRepository,
    );
  });

  it('should return pending sponsorship requests', async () => {
    const dto = { entityType: 'season' as const, 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: 'logo.png',
    });

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

    const result = await useCase.execute(dto);

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toEqual({
      entityType: 'season',
      entityId: 'entity-1',
      requests: [
        {
          id: 'req-1',
          sponsorId: 'sponsor-1',
          sponsorName: 'Test Sponsor',
          sponsorLogo: 'logo.png',
          tier: 'main',
          offeredAmount: 10000,
          currency: 'USD',
          formattedAmount: '$100.00',
          message: 'Test message',
          createdAt: expect.any(Date),
          platformFee: 1000,
          netAmount: 9000,
        },
      ],
      totalCount: 1,
    });
  });

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

    const result = await useCase.execute(dto);

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({
      code: 'REPOSITORY_ERROR',
      message: 'Failed to fetch pending sponsorship requests',
    });
  });
});