import { test } from 'node:test';
import { strict as assert } from 'node:assert';
import { parseArgs } from '../../lib/parsers/arg-parser.mjs';

test('ultrabrief — empty args', () => {
  const r = parseArgs('', 'ultrabrief');
  assert.equal(r.command, 'ultrabrief');
  assert.deepEqual(r.flags, {});
});

test('ultrabrief — --quick boolean', () => {
  const r = parseArgs('--quick', 'ultrabrief');
  assert.equal(r.flags['--quick'], true);
});

test('ultraresearch — --project value capture', () => {
  const r = parseArgs('--project .claude/projects/2026-04-30-foo', 'ultraresearch');
  assert.equal(r.flags['--project'], '.claude/projects/2026-04-30-foo');
});

test('ultraresearch — --quick --local combined', () => {
  const r = parseArgs('--quick --local', 'ultraresearch');
  assert.equal(r.flags['--quick'], true);
  assert.equal(r.flags['--local'], true);
});

test('ultraplan — --research multi-value', () => {
  const r = parseArgs('--research a.md b.md c.md', 'ultraplan');
  assert.deepEqual(r.flags['--research'], ['a.md', 'b.md', 'c.md']);
});

test('ultraplan — --research multi stops at next flag', () => {
  const r = parseArgs('--research a.md b.md --project /x', 'ultraplan');
  assert.deepEqual(r.flags['--research'], ['a.md', 'b.md']);
  assert.equal(r.flags['--project'], '/x');
});

test('ultraplan — --brief required-value flag', () => {
  const r = parseArgs('--brief brief.md', 'ultraplan');
  assert.equal(r.flags['--brief'], 'brief.md');
});

test('ultraplan — missing value for --brief produces error', () => {
  const r = parseArgs('--brief --quick', 'ultraplan');
  assert.ok(r.errors.find(e => e.code === 'ARG_MISSING_VALUE'));
});

test('ultraplan — --decompose value flag', () => {
  const r = parseArgs('--decompose plan.md', 'ultraplan');
  assert.equal(r.flags['--decompose'], 'plan.md');
});

test('ultraexecute — --resume + --project', () => {
  const r = parseArgs('--resume --project /tmp/p', 'ultraexecute');
  assert.equal(r.flags['--resume'], true);
  assert.equal(r.flags['--project'], '/tmp/p');
});

test('ultraexecute — --step N value', () => {
  const r = parseArgs('--step 3', 'ultraexecute');
  assert.equal(r.flags['--step'], '3');
});

test('ultraexecute — unknown flag goes to unknown[]', () => {
  const r = parseArgs('--mystery foo', 'ultraexecute');
  assert.ok(r.unknown.includes('--mystery'));
});

test('quoted positional with spaces preserved', () => {
  const r = parseArgs('"hello world" simple', 'ultrabrief');
  assert.deepEqual(r.positional, ['hello world', 'simple']);
});

test('unknown command reported as error', () => {
  const r = parseArgs('--quick', 'notacommand');
  assert.ok(r.errors.find(e => e.code === 'ARG_UNKNOWN_COMMAND'));
});

test('ultrareview — --project value capture', () => {
  const r = parseArgs('--project .claude/projects/2026-05-01-foo', 'ultrareview');
  assert.equal(r.flags['--project'], '.claude/projects/2026-05-01-foo');
});

test('ultrareview — --since <ref> value', () => {
  const r = parseArgs('--since HEAD~5', 'ultrareview');
  assert.equal(r.flags['--since'], 'HEAD~5');
});

test('ultrareview — --quick + --validate combined', () => {
  const r = parseArgs('--quick --validate', 'ultrareview');
  assert.equal(r.flags['--quick'], true);
  assert.equal(r.flags['--validate'], true);
});

test('ultrareview — unknown flag goes to unknown[]', () => {
  const r = parseArgs('--mystery foo', 'ultrareview');
  assert.ok(r.unknown.includes('--mystery'));
});

test('ultracontinue — empty args produce no flags and no positional', () => {
  const r = parseArgs('', 'ultracontinue');
  assert.equal(r.command, 'ultracontinue');
  assert.deepEqual(r.flags, {});
  assert.deepEqual(r.positional, []);
  assert.deepEqual(r.errors, []);
});

test('ultracontinue — --help boolean flag', () => {
  const r = parseArgs('--help', 'ultracontinue');
  assert.equal(r.flags['--help'], true);
});

test('ultracontinue — -h treated as positional (no alias resolution)', () => {
  const r = parseArgs('-h', 'ultracontinue');
  assert.deepEqual(r.positional, ['-h']);
  assert.deepEqual(r.errors, []);
  assert.equal(r.flags['--help'], undefined);
});

test('ultracontinue — --cleanup boolean flag', () => {
  const r = parseArgs('--cleanup', 'ultracontinue');
  assert.equal(r.flags['--cleanup'], true);
});

test('ultracontinue — --cleanup --confirm both flags', () => {
  const r = parseArgs('--cleanup --confirm', 'ultracontinue');
  assert.equal(r.flags['--cleanup'], true);
  assert.equal(r.flags['--confirm'], true);
});

test('ultracontinue — positional project dir captured', () => {
  const r = parseArgs('.claude/projects/2026-05-04-foo', 'ultracontinue');
  assert.deepEqual(r.positional, ['.claude/projects/2026-05-04-foo']);
});

test('ultracontinue — .md positional accepted by parser (rejection is command-level)', () => {
  const r = parseArgs('NEXT-SESSION-PROMPT.local.md', 'ultracontinue');
  assert.deepEqual(r.positional, ['NEXT-SESSION-PROMPT.local.md']);
  assert.deepEqual(r.errors, []);
});
