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

/**
 * Smoke test: verifies the command tree is correctly registered.
 * No database connection is opened — we only inspect Commander metadata.
 */
describe('registerBrainCommand', () => {
  function buildProgram(): Command {
    const program = new Command('mosaic');
    // Prevent Commander from calling process.exit on parse errors during tests.
    program.exitOverride();
    registerBrainCommand(program);
    return program;
  }

  it('registers a top-level "brain" command', () => {
    const program = buildProgram();
    const brainCmd = program.commands.find((c) => c.name() === 'brain');
    expect(brainCmd).toBeDefined();
  });

  it('registers "brain projects" with "list" and "create" subcommands', () => {
    const program = buildProgram();
    const brainCmd = program.commands.find((c) => c.name() === 'brain')!;
    const projectsCmd = brainCmd.commands.find((c) => c.name() === 'projects');
    expect(projectsCmd).toBeDefined();

    const subNames = projectsCmd!.commands.map((c) => c.name());
    expect(subNames).toContain('list');
    expect(subNames).toContain('create');
  });

  it('registers "brain missions" with "list" subcommand', () => {
    const program = buildProgram();
    const brainCmd = program.commands.find((c) => c.name() === 'brain')!;
    const missionsCmd = brainCmd.commands.find((c) => c.name() === 'missions');
    expect(missionsCmd).toBeDefined();

    const subNames = missionsCmd!.commands.map((c) => c.name());
    expect(subNames).toContain('list');
  });

  it('registers "brain tasks" with "list" subcommand', () => {
    const program = buildProgram();
    const brainCmd = program.commands.find((c) => c.name() === 'brain')!;
    const tasksCmd = brainCmd.commands.find((c) => c.name() === 'tasks');
    expect(tasksCmd).toBeDefined();

    const subNames = tasksCmd!.commands.map((c) => c.name());
    expect(subNames).toContain('list');
  });

  it('registers "brain conversations" with "list" subcommand', () => {
    const program = buildProgram();
    const brainCmd = program.commands.find((c) => c.name() === 'brain')!;
    const conversationsCmd = brainCmd.commands.find((c) => c.name() === 'conversations');
    expect(conversationsCmd).toBeDefined();

    const subNames = conversationsCmd!.commands.map((c) => c.name());
    expect(subNames).toContain('list');
  });

  it('"brain projects list" accepts --db and --limit options', () => {
    const program = buildProgram();
    const brainCmd = program.commands.find((c) => c.name() === 'brain')!;
    const projectsCmd = brainCmd.commands.find((c) => c.name() === 'projects')!;
    const listCmd = projectsCmd.commands.find((c) => c.name() === 'list')!;

    const optionNames = listCmd.options.map((o) => o.long);
    expect(optionNames).toContain('--db');
    expect(optionNames).toContain('--limit');
  });

  it('"brain missions list" accepts --project option', () => {
    const program = buildProgram();
    const brainCmd = program.commands.find((c) => c.name() === 'brain')!;
    const missionsCmd = brainCmd.commands.find((c) => c.name() === 'missions')!;
    const listCmd = missionsCmd.commands.find((c) => c.name() === 'list')!;

    const optionNames = listCmd.options.map((o) => o.long);
    expect(optionNames).toContain('--project');
  });

  it('"brain tasks list" accepts --project option', () => {
    const program = buildProgram();
    const brainCmd = program.commands.find((c) => c.name() === 'brain')!;
    const tasksCmd = brainCmd.commands.find((c) => c.name() === 'tasks')!;
    const listCmd = tasksCmd.commands.find((c) => c.name() === 'list')!;

    const optionNames = listCmd.options.map((o) => o.long);
    expect(optionNames).toContain('--project');
  });
});
