// tests/parsers/arg-parser-profile.test.mjs
// SC #4: --profile valued flag MUST be recognized on all 6 voyage commands.

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

const COMMANDS = ['trekbrief', 'trekresearch', 'trekplan', 'trekexecute', 'trekreview', 'trekcontinue'];

for (const cmd of COMMANDS) {
  test(`${cmd} — --profile economy parses as valued`, () => {
    const r = parseArgs('--profile economy', cmd);
    assert.equal(r.flags['--profile'], 'economy', `${cmd} should accept --profile economy`);
    assert.equal(r.errors.length, 0, `${cmd} should have no errors`);
    assert.equal(r.unknown.length, 0, `${cmd} should not mark --profile as unknown`);
  });
}

test('trekplan — --profile without value emits ARG_MISSING_VALUE', () => {
  const r = parseArgs('--profile', 'trekplan');
  const missing = r.errors.find((e) => e.code === 'ARG_MISSING_VALUE');
  assert.ok(missing, 'must surface ARG_MISSING_VALUE');
  assert.match(missing.message, /--profile/);
});

test('trekplan — --profile economy --quick combines correctly', () => {
  const r = parseArgs('--profile economy --quick', 'trekplan');
  assert.equal(r.flags['--profile'], 'economy');
  assert.equal(r.flags['--quick'], true);
});

test('trekplan — --profile economy --gates open: --gates is unknown (parsed inline by command prose, not in FLAG_SCHEMA)', () => {
  // Edge case from plan.md Step 2 (per plan-critic minor): --gates is intentionally
  // NOT in FLAG_SCHEMA; commands parse it inline. Verify --profile still parses cleanly
  // and --gates ends up in unknown[] / positional[] rather than colliding with --profile.
  const r = parseArgs('--profile economy --gates open', 'trekplan');
  assert.equal(r.flags['--profile'], 'economy');
  // --gates is unknown to FLAG_SCHEMA; it lands in unknown[] and 'open' becomes positional
  assert.ok(r.unknown.includes('--gates'), `expected --gates in unknown, got: ${JSON.stringify(r.unknown)}`);
  assert.ok(r.positional.includes('open'), `expected 'open' as positional, got: ${JSON.stringify(r.positional)}`);
});

test('trekexecute — --profile balanced --project /tmp/p combines correctly', () => {
  const r = parseArgs('--profile balanced --project /tmp/p', 'trekexecute');
  assert.equal(r.flags['--profile'], 'balanced');
  assert.equal(r.flags['--project'], '/tmp/p');
});

test('trekcontinue — --profile premium parses without --project', () => {
  // trekcontinue had empty valued[] before v4.1 — sanity check the array is now extended
  const r = parseArgs('--profile premium', 'trekcontinue');
  assert.equal(r.flags['--profile'], 'premium');
});
