import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { CreateSponsorUseCase } from './CreateSponsorUseCase';
import type { CreateSponsorCommand } from '../dto/CreateSponsorCommand';
import type { ISponsorRepository } from '../../domain/repositories/ISponsorRepository';
import type { Logger } from '@core/shared/application';

describe('CreateSponsorUseCase', () => {
  let useCase: CreateSponsorUseCase;
  let sponsorRepository: {
    create: Mock;
  };
  let logger: {
    debug: Mock;
    info: Mock;
    warn: Mock;
    error: Mock;
  };

  beforeEach(() => {
    sponsorRepository = {
      create: vi.fn(),
    };
    logger = {
      debug: vi.fn(),
      info: vi.fn(),
      warn: vi.fn(),
      error: vi.fn(),
    };
    useCase = new CreateSponsorUseCase(
      sponsorRepository as unknown as ISponsorRepository,
      logger as unknown as Logger,
    );
  });

  it('should create sponsor successfully', async () => {
    const command: CreateSponsorCommand = {
      name: 'Test Sponsor',
      contactEmail: 'test@example.com',
      websiteUrl: 'https://example.com',
      logoUrl: 'https://example.com/logo.png',
    };

    sponsorRepository.create.mockResolvedValue(undefined);

    const result = await useCase.execute(command);

    expect(result.isOk()).toBe(true);
    const data = result.unwrap();
    expect(data.sponsor.id).toBeDefined();
    expect(data.sponsor.name).toBe('Test Sponsor');
    expect(data.sponsor.contactEmail).toBe('test@example.com');
    expect(data.sponsor.websiteUrl).toBe('https://example.com');
    expect(data.sponsor.logoUrl).toBe('https://example.com/logo.png');
    expect(data.sponsor.createdAt).toBeInstanceOf(Date);
    expect(sponsorRepository.create).toHaveBeenCalledTimes(1);
  });

  it('should create sponsor without optional fields', async () => {
    const command: CreateSponsorCommand = {
      name: 'Test Sponsor',
      contactEmail: 'test@example.com',
    };

    sponsorRepository.create.mockResolvedValue(undefined);

    const result = await useCase.execute(command);

    expect(result.isOk()).toBe(true);
    const data = result.unwrap();
    expect(data.sponsor.websiteUrl).toBeUndefined();
    expect(data.sponsor.logoUrl).toBeUndefined();
  });

  it('should return error when name is empty', async () => {
    const command: CreateSponsorCommand = {
      name: '',
      contactEmail: 'test@example.com',
    };

    const result = await useCase.execute(command);

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr().details.message).toBe('Sponsor name is required');
  });

  it('should return error when contactEmail is empty', async () => {
    const command: CreateSponsorCommand = {
      name: 'Test Sponsor',
      contactEmail: '',
    };

    const result = await useCase.execute(command);

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr().details.message).toBe('Sponsor contact email is required');
  });

  it('should return error when contactEmail is invalid', async () => {
    const command: CreateSponsorCommand = {
      name: 'Test Sponsor',
      contactEmail: 'invalid-email',
    };

    const result = await useCase.execute(command);

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr().details.message).toBe('Invalid sponsor contact email format');
  });

  it('should return error when websiteUrl is invalid', async () => {
    const command: CreateSponsorCommand = {
      name: 'Test Sponsor',
      contactEmail: 'test@example.com',
      websiteUrl: 'invalid-url',
    };

    const result = await useCase.execute(command);

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr().details.message).toBe('Invalid sponsor website URL');
  });

  it('should return error when repository throws', async () => {
    const command: CreateSponsorCommand = {
      name: 'Test Sponsor',
      contactEmail: 'test@example.com',
    };

    sponsorRepository.create.mockRejectedValue(new Error('DB error'));

    const result = await useCase.execute(command);

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr().details.message).toBe('DB error');
  });
});