import { describe, it, expect } from 'vitest';
import { Command } from 'commander';
import { registerMacpCommand } from './cli.js';

describe('registerMacpCommand', () => {
  function buildProgram(): Command {
    const program = new Command();
    program.exitOverride(); // prevent process.exit in tests
    registerMacpCommand(program);
    return program;
  }

  it('registers a "macp" command on the parent', () => {
    const program = buildProgram();
    const macpCmd = program.commands.find((c) => c.name() === 'macp');
    expect(macpCmd).toBeDefined();
  });

  it('registers "macp tasks" subcommand group', () => {
    const program = buildProgram();
    const macpCmd = program.commands.find((c) => c.name() === 'macp')!;
    const tasksCmd = macpCmd.commands.find((c) => c.name() === 'tasks');
    expect(tasksCmd).toBeDefined();
  });

  it('registers "macp tasks list" subcommand with --status and --type flags', () => {
    const program = buildProgram();
    const macpCmd = program.commands.find((c) => c.name() === 'macp')!;
    const tasksCmd = macpCmd.commands.find((c) => c.name() === 'tasks')!;
    const listCmd = tasksCmd.commands.find((c) => c.name() === 'list');
    expect(listCmd).toBeDefined();
    const optionNames = listCmd!.options.map((o) => o.long);
    expect(optionNames).toContain('--status');
    expect(optionNames).toContain('--type');
  });

  it('registers "macp submit" subcommand', () => {
    const program = buildProgram();
    const macpCmd = program.commands.find((c) => c.name() === 'macp')!;
    const submitCmd = macpCmd.commands.find((c) => c.name() === 'submit');
    expect(submitCmd).toBeDefined();
  });

  it('registers "macp gate" subcommand with --fail-on flag', () => {
    const program = buildProgram();
    const macpCmd = program.commands.find((c) => c.name() === 'macp')!;
    const gateCmd = macpCmd.commands.find((c) => c.name() === 'gate');
    expect(gateCmd).toBeDefined();
    const optionNames = gateCmd!.options.map((o) => o.long);
    expect(optionNames).toContain('--fail-on');
  });

  it('registers "macp events" subcommand group', () => {
    const program = buildProgram();
    const macpCmd = program.commands.find((c) => c.name() === 'macp')!;
    const eventsCmd = macpCmd.commands.find((c) => c.name() === 'events');
    expect(eventsCmd).toBeDefined();
  });

  it('registers "macp events tail" subcommand', () => {
    const program = buildProgram();
    const macpCmd = program.commands.find((c) => c.name() === 'macp')!;
    const eventsCmd = macpCmd.commands.find((c) => c.name() === 'events')!;
    const tailCmd = eventsCmd.commands.find((c) => c.name() === 'tail');
    expect(tailCmd).toBeDefined();
  });

  it('has all required top-level subcommands', () => {
    const program = buildProgram();
    const macpCmd = program.commands.find((c) => c.name() === 'macp')!;
    const topLevel = macpCmd.commands.map((c) => c.name());
    expect(topLevel).toContain('tasks');
    expect(topLevel).toContain('submit');
    expect(topLevel).toContain('gate');
    expect(topLevel).toContain('events');
  });
});
