import { describe, expect, it } from 'vitest';
import type { ParticipantRef } from '../../types/ParticipantRef';
import { ChampionshipStanding } from './ChampionshipStanding';

describe('ChampionshipStanding', () => {
  const participant: ParticipantRef = { type: 'driver', id: 'driver1' };

  it('should create a championship standing', () => {
    const standing = ChampionshipStanding.create({
      seasonId: 'season1',
      championshipId: 'champ1',
      participant,
      totalPoints: 100,
      resultsCounted: 5,
      resultsDropped: 1,
      position: 1,
    });

    expect(standing.id).toBe('season1-champ1-driver1');
    expect(standing.seasonId).toBe('season1');
    expect(standing.championshipId).toBe('champ1');
    expect(standing.participant).toBe(participant);
    expect(standing.totalPoints.toNumber()).toBe(100);
    expect(standing.resultsCounted.toNumber()).toBe(5);
    expect(standing.resultsDropped.toNumber()).toBe(1);
    expect(standing.position.toNumber()).toBe(1);
  });

  it('should update position', () => {
    const standing = ChampionshipStanding.create({
      seasonId: 'season1',
      championshipId: 'champ1',
      participant,
      totalPoints: 100,
      resultsCounted: 5,
      resultsDropped: 1,
      position: 1,
    });

    const updated = standing.withPosition(2);
    expect(updated.position.toNumber()).toBe(2);
    expect(updated.id).toBe('season1-champ1-driver1');
    expect(updated.totalPoints.toNumber()).toBe(100);
  });

  it('should throw on invalid seasonId', () => {
    expect(() => ChampionshipStanding.create({
      seasonId: '',
      championshipId: 'champ1',
      participant,
      totalPoints: 100,
      resultsCounted: 5,
      resultsDropped: 1,
      position: 1,
    })).toThrow('Season ID is required');
  });

  it('should throw on invalid championshipId', () => {
    expect(() => ChampionshipStanding.create({
      seasonId: 'season1',
      championshipId: '',
      participant,
      totalPoints: 100,
      resultsCounted: 5,
      resultsDropped: 1,
      position: 1,
    })).toThrow('Championship ID is required');
  });

  it('should throw on invalid participant', () => {
    expect(() => ChampionshipStanding.create({
      seasonId: 'season1',
      championshipId: 'champ1',
      participant: { type: 'driver', id: '' },
      totalPoints: 100,
      resultsCounted: 5,
      resultsDropped: 1,
      position: 1,
    })).toThrow('Participant is required');
  });

  it('should throw on negative points', () => {
    expect(() => ChampionshipStanding.create({
      seasonId: 'season1',
      championshipId: 'champ1',
      participant,
      totalPoints: -1,
      resultsCounted: 5,
      resultsDropped: 1,
      position: 1,
    })).toThrow('Points cannot be negative');
  });

  it('should throw on invalid position', () => {
    expect(() => ChampionshipStanding.create({
      seasonId: 'season1',
      championshipId: 'champ1',
      participant,
      totalPoints: 100,
      resultsCounted: 5,
      resultsDropped: 1,
      position: 0,
    })).toThrow('Position must be a positive integer');
  });
});