//! Command-line interface for the makima CLI.

pub mod config;
pub mod daemon;
pub mod directive;
pub mod server;
pub mod view;

use clap::{Parser, Subcommand};

pub use config::CliConfig;
pub use daemon::DaemonArgs;
pub use directive::DirectiveArgs;
pub use server::ServerArgs;
pub use view::ViewArgs;

/// Makima - unified CLI for server, daemon, and task management.
#[derive(Parser, Debug)]
#[command(name = "makima")]
#[command(version, about = "Makima CLI - server, daemon, and task management", long_about = None)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Commands,
}

#[derive(Subcommand, Debug)]
pub enum Commands {
    /// Run the makima server
    Server(ServerArgs),

    /// Run the daemon (connect to server, manage tasks)
    Daemon(DaemonArgs),

    /// Directive commands for DAG-based project management
    #[command(subcommand)]
    Directive(DirectiveCommand),

    /// Interactive TUI browser for directives and tasks
    View(ViewArgs),

    /// Configure CLI settings (API key, server URL)
    ///
    /// Saves configuration to ~/.makima/config.toml for use by CLI commands.
    #[command(subcommand)]
    Config(ConfigCommand),
}

/// Config subcommands for CLI configuration.
#[derive(Subcommand, Debug)]
pub enum ConfigCommand {
    /// Set the API key
    ///
    /// Saves the API key to ~/.makima/config.toml
    SetKey(config::SetKeyArgs),

    /// Set the API URL
    ///
    /// Saves the API URL to ~/.makima/config.toml
    SetUrl(config::SetUrlArgs),

    /// Show current configuration
    Show,

    /// Show the config file path
    Path,
}

// SupervisorCommand and ContractCommand removed in Phase 5 — contracts
// subsystem is gone. See cli/contract.rs and cli/supervisor.rs deletion.

/// Directive subcommands for DAG-based project management.
#[derive(Subcommand, Debug)]
pub enum DirectiveCommand {
    /// List all directives
    List(directive::DirectiveListArgs),

    /// Get directive status with steps
    Get(DirectiveArgs),

    /// Get directive status (alias for get)
    Status(DirectiveArgs),

    /// Add a step to the directive
    AddStep(directive::AddStepArgs),

    /// Remove a step from the directive
    RemoveStep(directive::RemoveStepArgs),

    /// Set dependencies for a step
    SetDeps(directive::SetDepsArgs),

    /// Start the directive (begin executing steps)
    Start(DirectiveArgs),

    /// Pause the directive
    Pause(DirectiveArgs),

    /// Advance the DAG (find newly-ready steps)
    Advance(DirectiveArgs),

    /// Mark a step as completed
    CompleteStep(directive::StepActionArgs),

    /// Mark a step as failed
    FailStep(directive::StepActionArgs),

    /// Mark a step as skipped
    SkipStep(directive::StepActionArgs),

    /// Batch add multiple steps from JSON
    BatchAddSteps(directive::BatchAddStepsArgs),

    /// Update directive metadata (PR URL, etc.)
    Update(directive::UpdateArgs),

    /// Ask a question and wait for user feedback
    Ask(directive::AskArgs),

    /// Create an order for future work (spike or chore only)
    CreateOrder(directive::CreateOrderArgs),

    /// Verify the current worktree merges cleanly into the directive's base branch.
    ///
    /// Mandatory pre-flight before creating or pushing a directive PR — fails
    /// with a non-zero exit code (and a list of conflicting files) if the merge
    /// would conflict with the base branch.
    Verify(directive::VerifyArgs),
}

impl Cli {
    /// Parse command-line arguments
    pub fn parse_args() -> Self {
        Self::parse()
    }
}
