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

import {
    applyScoringPresetToTimings,
    type ScoringPresetTimings,
} from '@core/racing/domain/services/ScoringPresetTimingService';

describe('ScoringPresetTimingService', () => {
  it('applies sprint/double style presets with two sessions and sprint minutes', () => {
    const initial: ScoringPresetTimings = {
      practiceMinutes: 5,
      qualifyingMinutes: 10,
      mainRaceMinutes: 20,
      sessionCount: 1,
    };

    const result = applyScoringPresetToTimings('Sprint-Main-Double', initial);

    expect(result.practiceMinutes).toBe(15);
    expect(result.qualifyingMinutes).toBe(20);
    expect(result.sprintRaceMinutes).toBe(20);
    expect(result.mainRaceMinutes).toBe(35);
    expect(result.sessionCount).toBe(2);
  });

  it('applies endurance/long style presets with single main session and no sprint', () => {
    const initial: ScoringPresetTimings = {
      practiceMinutes: 10,
      qualifyingMinutes: 15,
      sprintRaceMinutes: 10,
      mainRaceMinutes: 30,
      sessionCount: 2,
    };

    const result = applyScoringPresetToTimings('Endurance-Main', initial);

    expect(result.practiceMinutes).toBe(30);
    expect(result.qualifyingMinutes).toBe(30);
    expect(result.mainRaceMinutes).toBe(90);
    expect(result.sessionCount).toBe(1);
    expect(result.sprintRaceMinutes).toBeUndefined();
  });

  it('applies default timing rules for non-matching presets and clears sprint minutes', () => {
    const initial: ScoringPresetTimings = {
      practiceMinutes: 10,
      qualifyingMinutes: 15,
      sprintRaceMinutes: 10,
      mainRaceMinutes: 30,
      sessionCount: 2,
    };

    const result = applyScoringPresetToTimings('club-default', initial);

    expect(result.practiceMinutes).toBe(20);
    expect(result.qualifyingMinutes).toBe(30);
    expect(result.mainRaceMinutes).toBe(40);
    expect(result.sessionCount).toBe(1);
    expect(result.sprintRaceMinutes).toBeUndefined();
  });

  it('treats pattern id matching as case-insensitive', () => {
    const initial: ScoringPresetTimings = {};

    const lower = applyScoringPresetToTimings('endurance-main', initial);
    const upper = applyScoringPresetToTimings('ENDURANCE-MAIN', initial);

    expect(lower).toEqual(upper);
  });
});
