import { describe, expect, it } from 'vitest';
import { BOT_USAGE, parseBotArgs, parseServerArgs } from '../src/args.ts';

describe('parseServerArgs (спека арены 3.3)', () => {
  it('по умолчанию: порт 4880, FOV 12, каталог репозитория, заглушек 0, режим ffa, фф выключен, хост локальный', () => {
    const r = parseServerArgs(['offices/local']);
    expect(r).toEqual({
      office: 'offices/local',
      port: 4880,
      fov: 12,
      catalog: 'catalog/limezu/catalog.yaml',
      dummy: 0,
      mode: 'ffa',
      friendlyFire: false,
      roundKills: undefined,
      rounds: undefined,
      host: '127.0.0.1',
      adminToken: undefined,
      offices: [],
    });
  });

  it('все флаги разбираются', () => {
    const r = parseServerArgs([
      'offices/demo', '--port', '4900', '--fov', '6', '--catalog', '/tmp/cat.yaml', '--dummy', '3',
      '--mode', 'team', '--friendly-fire', '--round-kills', '25', '--host', '0.0.0.0',
      '--admin-token', 'tok', '--offices', 'offices/arena,offices/local',
    ]);
    expect(r).toEqual({
      office: 'offices/demo', port: 4900, fov: 6, catalog: '/tmp/cat.yaml', dummy: 3,
      mode: 'team', friendlyFire: true, roundKills: 25, rounds: undefined, host: '0.0.0.0',
      adminToken: 'tok', offices: ['offices/arena', 'offices/local'],
    });
  });

  it('админ-флаги (спека веб-управления 3.1): токен включает админ, офисы — список через запятую', () => {
    expect(parseServerArgs(['offices/local', '--admin-token', 'tok'])).toMatchObject({ adminToken: 'tok', offices: [] });
    expect(parseServerArgs(['offices/local', '--offices', 'offices/demo, offices/arena,'])).toMatchObject({ offices: ['offices/demo', 'offices/arena'] });
    expect(parseServerArgs(['offices/local', '--admin-token'])).toHaveProperty('error');
    expect(parseServerArgs(['offices/local', '--offices'])).toHaveProperty('error');
  });

  it('админ-токен не съедает имя следующего флага — ошибка, админ не включается (ревью 2)', () => {
    expect(parseServerArgs(['offices/local', '--admin-token', '--offices', 'offices/demo'])).toHaveProperty('error');
    expect(parseServerArgs(['offices/local', '--admin-token', '--host'])).toHaveProperty('error');
  });

  it('хост: без значения ошибка, 0.0.0.0 — слушать все интерфейсы (для игры по сети)', () => {
    expect(parseServerArgs(['offices/local', '--host'])).toHaveProperty('error');
    expect(parseServerArgs(['offices/local', '--host', '0.0.0.0'])).toMatchObject({ host: '0.0.0.0' });
    expect(parseServerArgs(['offices/local', '--host', '192.168.1.10'])).toMatchObject({ host: '192.168.1.10' });
  });

  it('режим и порог: --mode team|ffa|elim, --round-kills ≥ 1 (спека командного боя 4; elimination 4)', () => {
    expect(parseServerArgs(['offices/local', '--mode', 'team'])).toMatchObject({ mode: 'team' });
    expect(parseServerArgs(['offices/local', '--mode', 'ffa'])).toMatchObject({ mode: 'ffa' });
    expect(parseServerArgs(['offices/local', '--mode', 'elim'])).toMatchObject({ mode: 'elim' });
    expect(parseServerArgs(['offices/local', '--mode', 'xxx'])).toHaveProperty('error');
    expect(parseServerArgs(['offices/local', '--mode'])).toHaveProperty('error');
    expect(parseServerArgs(['offices/local', '--round-kills', '0'])).toHaveProperty('error');
    expect(parseServerArgs(['offices/local', '--round-kills', '7'])).toMatchObject({ roundKills: 7 });
  });

  it('elim: --rounds разбирается; вне elim и с --round-kills — ошибки; умолчание — без поля (спека elimination 4)', () => {
    expect(parseServerArgs(['offices/local', '--mode', 'elim'])).toMatchObject({ mode: 'elim', rounds: undefined });
    expect(parseServerArgs(['offices/local', '--mode', 'elim', '--rounds', '5'])).toMatchObject({ mode: 'elim', rounds: 5 });
    expect(parseServerArgs(['offices/local', '--mode', 'elim', '--rounds', '1'])).toMatchObject({ rounds: 1 }); // граница
    expect(parseServerArgs(['offices/local', '--mode', 'elim', '--rounds', '0'])).toHaveProperty('error'); // N ≥ 1
    expect(parseServerArgs(['offices/local', '--rounds', '3'])).toHaveProperty('error'); // вне elim партии порогом не закрываются
    expect(parseServerArgs(['offices/local', '--mode', 'team', '--rounds', '3'])).toHaveProperty('error');
    expect(parseServerArgs(['offices/local', '--mode', 'elim', '--round-kills', '10'])).toHaveProperty('error'); // порога фрагов в elim нет
  });

  it('без каталога офиса и с кривыми числами — ошибка', () => {
    expect(parseServerArgs([])).toHaveProperty('error');
    expect(parseServerArgs(['--port', '4880'])).toHaveProperty('error');
    expect(parseServerArgs(['offices/local', '--port', 'abc'])).toHaveProperty('error');
    expect(parseServerArgs(['offices/local', '--fov', '0'])).toHaveProperty('error');
    expect(parseServerArgs(['offices/local', '--dummy', '-1'])).toHaveProperty('error');
  });

  it('порт 1–65535, заглушки 0–16 — вне диапазона ошибка (спека арены 6.1)', () => {
    expect(parseServerArgs(['offices/local', '--port', '0'])).toHaveProperty('error');
    expect(parseServerArgs(['offices/local', '--port', '70000'])).toHaveProperty('error');
    expect(parseServerArgs(['offices/local', '--port', '1'])).toMatchObject({ port: 1 });
    expect(parseServerArgs(['offices/local', '--port', '65535'])).toMatchObject({ port: 65535 });
    expect(parseServerArgs(['offices/local', '--dummy', '17'])).toHaveProperty('error');
    expect(parseServerArgs(['offices/local', '--dummy', '16'])).toMatchObject({ dummy: 16 });
  });
});

describe('parseBotArgs (спека ботов 2.1; собственный разбор — спека push 6.1)', () => {
  it('по умолчанию имя bot; --name и --url работают', () => {
    expect(parseBotArgs([])).toEqual({ url: 'http://127.0.0.1:4880', name: 'bot' });
    expect(parseBotArgs(['--url', 'http://localhost:5000', '--name', 'Агрессор'])).toEqual({ url: 'http://localhost:5000', name: 'Агрессор' });
  });
  it('ошибки несут BOT_USAGE, а не подсказку клиента', () => {
    expect(parseBotArgs(['--url', 'не-url'])).toEqual({ error: `--url: некорректный адрес\n${BOT_USAGE}` });
    expect(parseBotArgs(['--name'])).toEqual({ error: `--name: нужно имя\n${BOT_USAGE}` });
    expect(parseBotArgs(['--name', '   '])).toEqual({ error: `--name: нужно имя\n${BOT_USAGE}` });
  });
  it('--spectator боту нельзя', () => {
    expect(parseBotArgs(['--spectator'])).toEqual({ error: `--spectator: флаг зрителя-клиента, боту не нужен\n${BOT_USAGE}` });
  });
});
