import { describe, expect, it } from 'vitest';
import { defaultSettings, incompatible, mergeSettings, validateSettings, type MatchSettings } from '../src/matchOptions.ts';

// Исходные настройки с точечными перекрытиями.
const s = (over: Partial<MatchSettings> = {}): MatchSettings => ({ ...defaultSettings(), ...over });

describe('умолчания (спека веб-управления 4.1)', () => {
  it('режим ffa, ничего не задано, фф выключен, FOV 12', () => {
    expect(defaultSettings()).toEqual({ mode: 'ffa', rounds: null, roundKills: null, friendlyFire: false, fov: 12 });
  });

  it('совместимый полный объект проходит валидацию', () => {
    expect(validateSettings(s({ mode: 'elim', rounds: 3 }))).toBeNull();
    expect(validateSettings(s({ mode: 'team', roundKills: 25, friendlyFire: true, fov: 6 }))).toBeNull();
    expect(validateSettings(s({ mode: 'ffa', roundKills: 10 }))).toBeNull();
  });
});

describe('несовместимости и диапазоны (спека веб-управления 4.2, правила CLI)', () => {
  it('rounds — только elim, roundKills — не в elim', () => {
    expect(incompatible('rounds', 'ffa')).toBe(true);
    expect(incompatible('rounds', 'team')).toBe(true);
    expect(incompatible('rounds', 'elim')).toBe(false);
    expect(incompatible('roundKills', 'elim')).toBe(true);
    expect(incompatible('roundKills', 'ffa')).toBe(false);
    expect(incompatible('roundKills', 'team')).toBe(false);
    expect(validateSettings(s({ rounds: 3 }))).toContain('rounds');
    expect(validateSettings(s({ mode: 'elim', roundKills: 10 }))).toContain('roundKills');
  });

  it('диапазоны: fov 1–100, rounds ≥ 1, roundKills ≥ 1', () => {
    expect(validateSettings(s({ fov: 0 }))).toContain('fov');
    expect(validateSettings(s({ fov: 101 }))).toContain('fov');
    expect(validateSettings(s({ fov: 100 }))).toBeNull();
    expect(validateSettings(s({ mode: 'elim', rounds: 0 }))).toContain('rounds');
    expect(validateSettings(s({ roundKills: 0 }))).toContain('roundKills');
  });
});

describe('слияние (спека веб-управления 4.2)', () => {
  it('пустой патч и явные null — одно и то же: наследование исходных', () => {
    const cur = s({ mode: 'team', roundKills: 15, friendlyFire: true, fov: 6 });
    expect(mergeSettings(cur, {})).toEqual({ ok: true, settings: cur });
    expect(mergeSettings(cur, { roundKills: null, fov: null })).toEqual({ ok: true, settings: cur });
  });

  it('явное поле заменяет, остальное наследуется', () => {
    const r = mergeSettings(s({ mode: 'team', roundKills: 15, fov: 6 }), { fov: 9 });
    expect(r).toEqual({ ok: true, settings: { mode: 'team', rounds: null, roundKills: 15, friendlyFire: false, fov: 9 } });
  });

  it('явное несовместимое поле — ошибка (rounds вне elim, roundKills в elim)', () => {
    expect(mergeSettings(s(), { mode: 'ffa', rounds: 3 })).toMatchObject({ ok: false });
    expect(mergeSettings(s(), { mode: 'elim', roundKills: 10 })).toMatchObject({ ok: false });
  });

  it('наследованное несовместимое сбрасывается: elim с rounds → ffa проходит, поле — null', () => {
    const r = mergeSettings(s({ mode: 'elim', rounds: 2 }), { mode: 'ffa' });
    expect(r).toEqual({ ok: true, settings: { mode: 'ffa', rounds: null, roundKills: null, friendlyFire: false, fov: 12 } });
  });

  it('симметрично: team с roundKills → elim сбрасывает порог, остальное живёт', () => {
    const r = mergeSettings(s({ mode: 'team', roundKills: 15, fov: 6, friendlyFire: true }), { mode: 'elim' });
    expect(r).toEqual({ ok: true, settings: { mode: 'elim', rounds: null, roundKills: null, friendlyFire: true, fov: 6 } });
  });

  it('явное значение вне диапазона — ошибка и после сброса несовместимого', () => {
    expect(mergeSettings(s({ mode: 'elim', rounds: 2 }), { rounds: 0 })).toMatchObject({ ok: false });
    expect(mergeSettings(s(), { fov: 0 })).toMatchObject({ ok: false });
  });
});
