import type { Logger } from '@core/shared/domain/Logger';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import type { SponsorshipPricingRepository } from '../../domain/repositories/SponsorshipPricingRepository';
import {
    GetEntitySponsorshipPricingUseCase,
    type GetEntitySponsorshipPricingInput
} from './GetEntitySponsorshipPricingUseCase';

describe('GetEntitySponsorshipPricingUseCase', () => {
  let mockSponsorshipPricingRepo: SponsorshipPricingRepository;
  let mockLogger: Logger;
  let mockFindByEntity: Mock;

  beforeEach(() => {
    mockFindByEntity = vi.fn();
    mockSponsorshipPricingRepo = {
      findByEntity: mockFindByEntity,
      findAll: vi.fn(),
      create: vi.fn(),
      update: vi.fn(),
      delete: vi.fn(),
      save: vi.fn(),
      exists: vi.fn(),
      findAcceptingApplications: vi.fn(),
    } as unknown as SponsorshipPricingRepository;
    mockLogger = {
      debug: vi.fn(),
      info: vi.fn(),
      warn: vi.fn(),
      error: vi.fn(),
    };
  });

  it('should return PRICING_NOT_CONFIGURED when no pricing found', async () => {
    const useCase = new GetEntitySponsorshipPricingUseCase(mockSponsorshipPricingRepo,
      mockLogger);

    const dto: GetEntitySponsorshipPricingInput = {
      entityType: 'season',
      entityId: 'season1',
    };

    mockFindByEntity.mockResolvedValue(null);

    const result = await useCase.execute(dto);

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<
      'PRICING_NOT_CONFIGURED',
      { message: string }
    >;
    expect(err.code).toBe('PRICING_NOT_CONFIGURED');
    expect(err.details.message).toContain('No sponsorship pricing configured');
  });

  it('should return pricing data when found', async () => {
    const useCase = new GetEntitySponsorshipPricingUseCase(mockSponsorshipPricingRepo,
      mockLogger);

    const dto: GetEntitySponsorshipPricingInput = {
      entityType: 'season',
      entityId: 'season1',
    };

    const pricing = {
      acceptingApplications: true,
      customRequirements: 'Some requirements',
      mainSlot: {
        price: { amount: 100, currency: 'USD', format: () => '$100' },
        benefits: ['Benefit 1'],
        available: true,
        maxSlots: 5,
      },
      secondarySlots: {
        price: { amount: 50, currency: 'USD', format: () => '$50' },
        benefits: ['Benefit 2'],
        available: true,
        maxSlots: 10,
      },
    };

    mockFindByEntity.mockResolvedValue(pricing);

    const result = await useCase.execute(dto);

    expect(result.isOk()).toBe(true);
    const presented = result.unwrap();
    expect(presented.entityType).toBe('season');
    expect(presented.entityId).toBe('season1');
    expect(presented.acceptingApplications).toBe(true);
    expect(presented.customRequirements).toBe('Some requirements');
    expect(presented.tiers).toHaveLength(2);

    const mainTier = presented.tiers.find(tier => tier.name === 'main');
    const secondaryTier = presented.tiers.find(tier => tier.name === 'secondary');

    expect(mainTier).toBeDefined();
    expect(mainTier?.price.amount).toBe(100);
    expect(mainTier?.price.currency).toBe('USD');
    expect(mainTier?.benefits).toEqual(['Benefit 1']);

    expect(secondaryTier).toBeDefined();
    expect(secondaryTier?.price.amount).toBe(50);
    expect(secondaryTier?.price.currency).toBe('USD');
    expect(secondaryTier?.benefits).toEqual(['Benefit 2']);
  });

  it('should return error when repository throws', async () => {
    const useCase = new GetEntitySponsorshipPricingUseCase(mockSponsorshipPricingRepo,
      mockLogger);

    const dto: GetEntitySponsorshipPricingInput = {
      entityType: 'season',
      entityId: 'season1',
    };

    const error = new Error('Repository error');

    mockFindByEntity.mockRejectedValue(error);

    const result = await useCase.execute(dto);

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