use clap::{Arg, Command};
use clap_complete::{generate, Shell};

pub fn run(shell: Option<String>) {
    let mut cmd = Command::new("rok");
    // build a basic command structure for completions
    cmd = cmd
        .subcommand(Command::new("new").about("Scaffold a new rok/Axum project"))
        .subcommand(
            Command::new("make:controller")
                .about("Generate a controller")
                .arg(Arg::new("name").required(true)),
        )
        .subcommand(
            Command::new("make:model")
                .about("Generate a model")
                .arg(Arg::new("name").required(true)),
        )
        .subcommand(
            Command::new("make:migration")
                .about("Generate a migration")
                .arg(Arg::new("name").required(true)),
        )
        .subcommand(
            Command::new("make:seeder")
                .about("Generate a seeder")
                .arg(Arg::new("name").required(true)),
        )
        .subcommand(
            Command::new("make:validator")
                .about("Generate a validator")
                .arg(Arg::new("name").required(true)),
        )
        .subcommand(
            Command::new("make:crud")
                .about("Full CRUD scaffold")
                .arg(Arg::new("name").required(true)),
        )
        .subcommand(
            Command::new("make:observer")
                .about("Generate an observer")
                .arg(Arg::new("name").required(true)),
        )
        .subcommand(
            Command::new("make:resource")
                .about("Generate an API resource")
                .arg(Arg::new("name").required(true)),
        )
        .subcommand(
            Command::new("make:scope")
                .about("Generate a query scope")
                .arg(Arg::new("name").required(true)),
        )
        .subcommand(
            Command::new("make:policy")
                .about("Generate a policy")
                .arg(Arg::new("name").required(true)),
        )
        .subcommand(
            Command::new("make:job")
                .about("Generate a background job")
                .arg(Arg::new("name").required(true)),
        )
        .subcommand(
            Command::new("make:event")
                .about("Generate an event")
                .arg(Arg::new("name").required(true)),
        )
        .subcommand(
            Command::new("make:listener")
                .about("Generate a listener")
                .arg(Arg::new("name").required(true)),
        )
        .subcommand(
            Command::new("make:notification")
                .about("Generate a notification")
                .arg(Arg::new("name").required(true)),
        )
        .subcommand(
            Command::new("make:test")
                .about("Generate a test file")
                .arg(Arg::new("name").required(true)),
        )
        .subcommand(
            Command::new("make:locale")
                .about("Generate a locale translation")
                .arg(Arg::new("lang").required(true)),
        )
        .subcommand(
            Command::new("make:from-json")
                .about("Generate from JSON payload")
                .arg(Arg::new("name").required(true))
                .arg(Arg::new("json").required(true)),
        )
        .subcommand(Command::new("make:scaffold").about("Run a domain scaffold template"))
        .subcommand(Command::new("make:feature").about("Generate a feature from JSON spec"))
        .subcommand(Command::new("make:ts-client").about("Generate TypeScript API client"))
        .subcommand(Command::new("db:migrate").about("Apply pending migrations"))
        .subcommand(Command::new("db:rollback").about("Rollback migrations"))
        .subcommand(Command::new("db:status").about("Show migration status"))
        .subcommand(Command::new("db:seed").about("Run seeders"))
        .subcommand(Command::new("db:fresh").about("Drop + re-run migrations"))
        .subcommand(
            Command::new("db:pull")
                .about("Introspect table → model")
                .arg(Arg::new("table").required(true)),
        )
        .subcommand(
            Command::new("db:diff")
                .about("Compare model vs schema")
                .arg(Arg::new("model").required(true)),
        )
        .subcommand(Command::new("queue:work").about("Start queue worker"))
        .subcommand(Command::new("queue:status").about("Queue health"))
        .subcommand(
            Command::new("queue:retry")
                .about("Retry a failed job")
                .arg(Arg::new("job-id").required(true)),
        )
        .subcommand(Command::new("queue:flush").about("Delete failed jobs"))
        .subcommand(Command::new("key:generate").about("Generate JWT secret"))
        .subcommand(Command::new("key:rotate").about("Rotate a key in .env"))
        .subcommand(Command::new("secrets:generate").about("Generate .env secrets"))
        .subcommand(Command::new("config:show").about("Show config"))
        .subcommand(Command::new("routes:list").about("List routes"))
        .subcommand(Command::new("dev").about("Start dev server"))
        .subcommand(Command::new("serve").about("Start application server"))
        .subcommand(Command::new("env:check").about("Check env vars"))
        .subcommand(Command::new("rok:init").about("Init .rok config"))
        .subcommand(Command::new("list").about("List all commands"))
        .subcommand(Command::new("check").about("Project health check"))
        .subcommand(Command::new("tui").about("Terminal UI dashboard"))
        .subcommand(Command::new("plan:next").about("Next roadmap phase"))
        .subcommand(Command::new("plan:list").about("List roadmap phases"))
        .subcommand(Command::new("plan:graph").about("Dependency graph"))
        .subcommand(Command::new("plan:status").about("Phase status"))
        .subcommand(Command::new("agent:rules").about("AI agent rules"))
        .subcommand(Command::new("agent:context").about("Phase context"))
        .subcommand(Command::new("agent:feedback").about("Append progress"))
        .subcommand(Command::new("agent:claude").about("Launch Claude Code"))
        .subcommand(Command::new("agent:opencode").about("Launch OpenCode"));

    let shell = shell
        .as_deref()
        .and_then(|s| s.parse::<Shell>().ok())
        .unwrap_or(Shell::Bash);

    generate(shell, &mut cmd, "rok", &mut std::io::stdout());
}
