import { describe, expect, it } from 'vitest';
import { parseRules, parseTarget } from '../src/index.ts';
import { MINI_RULES, mutateYaml } from './fixtures.ts';

describe('parseTarget', () => {
  it('словарь целей (спека 5.3)', () => {
    expect(parseTarget('own_seat')).toEqual({ kind: 'own_seat' });
    expect(parseTarget('exit')).toEqual({ kind: 'exit' });
    expect(parseTarget('zone:lounge')).toEqual({ kind: 'zone', zoneId: 'lounge' });
    expect(parseTarget('zone:lounge:pose=play')).toEqual({ kind: 'zone', zoneId: 'lounge', pose: 'play' });
    expect(parseTarget('zone:coworking:tag=quiet')).toEqual({ kind: 'zone', zoneId: 'coworking', tag: 'quiet' });
    expect(parseTarget('zone:pod:a')).toEqual({ kind: 'zone', zoneId: 'pod:a' });
    expect(parseTarget('zone:meeting:m1:pose=sit')).toEqual({ kind: 'zone', zoneId: 'meeting:m1', pose: 'sit' });
    expect(parseTarget('zone:x:pose=fly')).toBeNull();
    expect(parseTarget('sofa_seat')).toBeNull();
  });

  it('id зоны с «:» вместе с tag-суффиксом', () => {
    expect(parseTarget('zone:pod:a:tag=quiet')).toEqual({ kind: 'zone', zoneId: 'pod:a', tag: 'quiet' });
  });
});

describe('parseRules', () => {
  it('мини-правила', () => {
    const r = parseRules(MINI_RULES);
    expect(r.ok).toBe(true);
    if (!r.ok) return;
    expect(r.value.dwellMs).toEqual({ agent: 20_000, human: 60_000 });
    expect(r.value.agent.working[1]).toEqual({ kind: 'zone', zoneId: 'coworking', pose: 'sit' });
    expect(r.value.human.meeting.map((t) => t.kind)).toEqual(['meeting_room', 'meeting_room_standing']);
  });

  it('неизвестная цель — ошибка с путём', () => {
    const r = parseRules(mutateYaml(MINI_RULES, (d) => (d.agent.idle[0] = 'sofa_seat')));
    expect(r.ok).toBe(false);
    if (!r.ok) expect(r.errors[0]).toMatchObject({ code: 'unknown_target', message: expect.stringContaining('agent.idle[0]') });
  });

  it('ссылка на несуществующую зону при известном списке зон', () => {
    const r = parseRules(MINI_RULES, 'rules.yaml', new Set(['lounge']));
    expect(r.ok ? [] : r.errors.map((e) => e.code)).toContain('unknown_zone');
  });

  it('зоны с «:» в id проверяются по полному id', () => {
    const rules = mutateYaml(MINI_RULES, (d) => (d.human.meeting = ['zone:meeting:m1:pose=sit']));
    expect(parseRules(rules, 'rules.yaml', new Set(['lounge', 'coworking', 'meeting:m1'])).ok).toBe(true);
    const bad = parseRules(rules, 'rules.yaml', new Set(['lounge', 'coworking', 'meeting']));
    expect(bad.ok ? [] : bad.errors.map((e) => e.code)).toContain('unknown_zone');
  });

  it('нет статуса в правилах → ошибка схемы', () => {
    expect(parseRules(mutateYaml(MINI_RULES, (d) => delete d.agent.needs_input)).ok).toBe(false);
  });

  it('человек idle и needs_input в правилах — парсятся, ошибка с путём (спека 17.11.2026)', () => {
    const r = parseRules(mutateYaml(MINI_RULES, (d) => {
      d.human.idle = ['zone:lounge:pose=play', 'zone:lounge'];
      d.human.needs_input = ['current_work_seat', 'own_seat'];
    }));
    expect(r.ok).toBe(true);
    if (!r.ok) return;
    expect(r.value.human.idle[0]).toEqual({ kind: 'zone', zoneId: 'lounge', pose: 'play' });
    expect(r.value.human.needs_input.map((t) => t.kind)).toEqual(['current_work_seat', 'own_seat']);
    const bad = parseRules(mutateYaml(MINI_RULES, (d) => (d.human.idle = ['sofa_seat'])));
    expect(bad.ok ? [] : bad.errors[0]).toMatchObject({ code: 'unknown_target', message: expect.stringContaining('human.idle[0]') });
  });

  it('idle и needs_input человека не заданы — ведут себя как online (спека 17.11.2026)', () => {
    const r = parseRules(MINI_RULES);
    expect(r.ok).toBe(true);
    if (!r.ok) return;
    expect(r.value.human.idle).toEqual(r.value.human.online);
    expect(r.value.human.needs_input).toEqual(r.value.human.online);
  });

  it('dwell по умолчанию 20 с / 60 с', () => {
    const r = parseRules(mutateYaml(MINI_RULES, (d) => delete d.dwell));
    expect(r.ok && r.value.dwellMs).toEqual({ agent: 20_000, human: 60_000 });
  });
});

describe('bus.wait (спека транспорта 3.6)', () => {
  it('по умолчанию 60 с; длительность как у dwell', () => {
    const r = parseRules(MINI_RULES);
    expect(r.ok && r.value.busWaitMs).toBe(60_000);
    const fast = parseRules(MINI_RULES + 'bus: { wait: 10s }\n');
    expect(fast.ok && fast.value.busWaitMs).toBe(10_000);
    const bad = parseRules(MINI_RULES + 'bus: { wait: soon }\n');
    expect(bad.ok ? [] : bad.errors.map((e) => e.code)).toEqual(['schema']);
  });
});
