import { describe, it, expect } from 'vitest';

import {
  RacingDomainInvariantError,
  RacingDomainValidationError,
} from '../../errors/RacingDomainError';

import { SeasonSponsorship } from './SeasonSponsorship';
import { Money } from '../../value-objects/Money';

describe('SeasonSponsorship', () => {
  const validProps = {
    id: 'sponsorship-1',
    seasonId: 'season-1',
    sponsorId: 'sponsor-1',
    tier: 'main' as const,
    pricing: Money.create(1000),
    status: 'pending' as const,
    createdAt: new Date(),
  };

  it('creates a valid SeasonSponsorship', () => {
    const sponsorship = SeasonSponsorship.create(validProps);
    expect(sponsorship.id).toBe('sponsorship-1');
    expect(sponsorship.seasonId).toBe('season-1');
    expect(sponsorship.sponsorId).toBe('sponsor-1');
    expect(sponsorship.tier).toBe('main');
    expect(sponsorship.status).toBe('pending');
  });

  it('throws on invalid id', () => {
    expect(() => SeasonSponsorship.create({ ...validProps, id: '' })).toThrow(RacingDomainValidationError);
  });

  it('throws on invalid seasonId', () => {
    expect(() => SeasonSponsorship.create({ ...validProps, seasonId: '' })).toThrow(RacingDomainValidationError);
  });

  it('throws on invalid sponsorId', () => {
    expect(() => SeasonSponsorship.create({ ...validProps, sponsorId: '' })).toThrow(RacingDomainValidationError);
  });

  it('throws on invalid pricing', () => {
    expect(() => SeasonSponsorship.create({ ...validProps, pricing: Money.create(0) })).toThrow(RacingDomainValidationError);
  });

  it('activates from pending', () => {
    const sponsorship = SeasonSponsorship.create(validProps);
    const activated = sponsorship.activate();
    expect(activated.status).toBe('active');
    expect(activated.activatedAt).toBeInstanceOf(Date);
  });

  it('throws when activating active sponsorship', () => {
    const active = SeasonSponsorship.create({ ...validProps, status: 'active' });
    expect(() => active.activate()).toThrow(RacingDomainInvariantError);
  });

  it('ends active sponsorship', () => {
    const active = SeasonSponsorship.create({ ...validProps, status: 'active' });
    const ended = active.end();
    expect(ended.status).toBe('ended');
    expect(ended.endedAt).toBeInstanceOf(Date);
  });

  it('cancels pending sponsorship', () => {
    const sponsorship = SeasonSponsorship.create(validProps);
    const cancelled = sponsorship.cancel();
    expect(cancelled.status).toBe('cancelled');
    expect(cancelled.cancelledAt).toBeInstanceOf(Date);
  });

  it('isActive returns true for active status', () => {
    const active = SeasonSponsorship.create({ ...validProps, status: 'active' });
    expect(active.isActive()).toBe(true);
  });

  it('calculates platform fee', () => {
    const sponsorship = SeasonSponsorship.create(validProps);
    const fee = sponsorship.getPlatformFee();
    expect(fee.amount).toBe(100);
  });

  it('calculates net amount', () => {
    const sponsorship = SeasonSponsorship.create(validProps);
    const net = sponsorship.getNetAmount();
    expect(net.amount).toBe(900);
  });
});