import { describe, it, expect } from 'vitest';
import { InMemoryLeagueScoringPresetProvider } from './InMemoryLeagueScoringPresetProvider';

describe('InMemoryLeagueScoringPresetProvider', () => {
  const provider = new InMemoryLeagueScoringPresetProvider();

  describe('listPresets', () => {
    it('should return all available presets', () => {
      const presets = provider.listPresets();

      expect(presets.length).toBeGreaterThanOrEqual(3);
      expect(presets).toEqual(
        expect.arrayContaining([
          expect.objectContaining({
            id: 'sprint-main-driver',
            name: 'Sprint + Main',
            primaryChampionshipType: 'driver',
          }),
          expect.objectContaining({
            id: 'club-default',
            name: 'Club ladder',
            primaryChampionshipType: 'driver',
          }),
          expect.objectContaining({
            id: 'endurance-main-double',
            name: 'Endurance weekend',
            primaryChampionshipType: 'driver',
          }),
        ]),
      );
    });
  });

  describe('getPresetById', () => {
    it('should return the preset for a valid id', () => {
      const preset = provider.getPresetById('sprint-main-driver');

      expect(preset).toEqual({
        id: 'sprint-main-driver',
        name: 'Sprint + Main',
        description: 'Short sprint race plus main race; sprint gives fewer points.',
        primaryChampionshipType: 'driver',
        sessionSummary: 'Sprint + Main',
        bonusSummary: 'Fastest lap +1 point in main race if finishing P10 or better.',
        dropPolicySummary: 'Best 6 results of 8 count towards the championship.',
      });
    });

    it('should return undefined for an invalid id', () => {
      const preset = provider.getPresetById('invalid-id');

      expect(preset).toBeUndefined();
    });
  });

  describe('createScoringConfigFromPreset', () => {
    it('should create a scoring config from a valid preset', () => {
      const config = provider.createScoringConfigFromPreset('sprint-main-driver', 'season-123');

      expect(config).toBeDefined();
      expect(config.championships).toHaveLength(1);
      expect(config.championships[0]?.name).toBe('Driver Championship');
    });

    it('should throw an error for an invalid preset id', () => {
      expect(() => {
        provider.createScoringConfigFromPreset('invalid-id', 'season-123');
      }).toThrow('Scoring preset with id invalid-id not found');
    });
  });
});