//! Directive subcommand - directive management commands for orchestrator tasks.

use clap::Args;
use uuid::Uuid;

/// Common arguments for directive commands.
#[derive(Args, Debug, Clone)]
pub struct DirectiveArgs {
    /// API URL
    #[arg(long, env = "MAKIMA_API_URL", default_value = "https://api.makima.jp", global = true)]
    pub api_url: String,

    /// API key for authentication
    #[arg(long, env = "MAKIMA_API_KEY", global = true)]
    pub api_key: String,

    /// Directive ID
    #[arg(long, env = "MAKIMA_DIRECTIVE_ID", global = true)]
    pub directive_id: Uuid,
}

/// Arguments for listing directives (no directive_id required).
#[derive(Args, Debug, Clone)]
pub struct DirectiveListArgs {
    /// API URL
    #[arg(long, env = "MAKIMA_API_URL", default_value = "https://api.makima.jp", global = true)]
    pub api_url: String,

    /// API key for authentication
    #[arg(long, env = "MAKIMA_API_KEY", global = true)]
    pub api_key: String,
}

/// Arguments for add-step command.
#[derive(Args, Debug)]
pub struct AddStepArgs {
    #[command(flatten)]
    pub common: DirectiveArgs,

    /// Step name
    pub name: String,

    /// Step description
    #[arg(long)]
    pub description: Option<String>,

    /// Task plan for the step
    #[arg(long)]
    pub task_plan: Option<String>,

    /// Comma-separated UUIDs of dependency steps
    #[arg(long)]
    pub depends_on: Option<String>,

    /// Order index
    #[arg(long, default_value = "0")]
    pub order_index: i32,
}

/// Arguments for remove-step command.
#[derive(Args, Debug)]
pub struct RemoveStepArgs {
    #[command(flatten)]
    pub common: DirectiveArgs,

    /// Step ID to remove
    pub step_id: Uuid,
}

/// Arguments for set-deps command.
#[derive(Args, Debug)]
pub struct SetDepsArgs {
    #[command(flatten)]
    pub common: DirectiveArgs,

    /// Step ID to update
    pub step_id: Uuid,

    /// Comma-separated UUIDs of dependency steps
    pub depends_on: String,
}

/// Arguments for complete-step/fail-step/skip-step commands.
#[derive(Args, Debug)]
pub struct StepActionArgs {
    #[command(flatten)]
    pub common: DirectiveArgs,

    /// Step ID
    pub step_id: Uuid,
}

/// Arguments for update-goal command.
#[derive(Args, Debug)]
pub struct UpdateGoalArgs {
    #[command(flatten)]
    pub common: DirectiveArgs,

    /// New goal text
    pub goal: String,
}

/// Arguments for batch-add-steps command.
#[derive(Args, Debug)]
pub struct BatchAddStepsArgs {
    #[command(flatten)]
    pub common: DirectiveArgs,

    /// JSON array of steps: [{"name":"...","description":"...","taskPlan":"...","dependsOn":[],"orderIndex":0}]
    #[arg(long)]
    pub json: String,
}

/// Arguments for ask command (ask user a question from directive context).
#[derive(Args, Debug)]
pub struct AskArgs {
    #[command(flatten)]
    pub common: DirectiveArgs,

    /// The question to ask
    #[arg(index = 1)]
    pub question: String,

    /// Optional choices (comma-separated)
    #[arg(long)]
    pub choices: Option<String>,

    /// Context about what this relates to
    #[arg(long)]
    pub context: Option<String>,

    /// Timeout in seconds (default: 3600 = 1 hour)
    #[arg(long, default_value = "3600")]
    pub timeout: i32,

    /// Block indefinitely until user responds (no timeout)
    #[arg(long, default_value = "false")]
    pub phaseguard: bool,

    /// Allow selecting multiple choices (response will be comma-separated)
    #[arg(long, default_value = "false")]
    pub multi_select: bool,

    /// Non-blocking mode - returns immediately without waiting for response
    #[arg(long, default_value = "false")]
    pub non_blocking: bool,

    /// Question type (general, phase_confirmation, contract_complete)
    #[arg(long, default_value = "general")]
    pub question_type: String,
}

/// Arguments for create-order command.
#[derive(Args, Debug)]
pub struct CreateOrderArgs {
    #[command(flatten)]
    pub common: DirectiveArgs,

    /// Order title
    #[arg(long)]
    pub title: String,

    /// Order description
    #[arg(long)]
    pub description: Option<String>,

    /// Priority: critical, high, medium, low, none
    #[arg(long, default_value = "medium")]
    pub priority: String,

    /// Order type: spike or chore only
    #[arg(long, default_value = "spike")]
    pub order_type: String,

    /// Comma-separated labels
    #[arg(long)]
    pub labels: Option<String>,
}

/// Arguments for verify command — checks the current worktree can merge into the
/// directive's base branch with no conflicts. Runs entirely locally; no API call.
#[derive(Args, Debug)]
pub struct VerifyArgs {
    /// Base branch to attempt merging into (e.g., "master", "main").
    #[arg(long, default_value = "master")]
    pub base: String,

    /// Remote name to fetch from (default: "origin").
    #[arg(long, default_value = "origin")]
    pub remote: String,

    /// Head ref to verify (default: current HEAD).
    #[arg(long)]
    pub head: Option<String>,

    /// Skip the `git fetch` step (use already-fetched remote ref).
    #[arg(long, default_value = "false")]
    pub skip_fetch: bool,

    /// Optional directive goal text. When provided the goal is echoed back as a
    /// reminder so the calling orchestrator can self-check goal alignment.
    #[arg(long)]
    pub goal: Option<String>,
}

/// Arguments for update command.
#[derive(Args, Debug)]
pub struct UpdateArgs {
    #[command(flatten)]
    pub common: DirectiveArgs,

    /// PR URL to store on the directive
    #[arg(long)]
    pub pr_url: Option<String>,

    /// PR branch name to store on the directive
    #[arg(long)]
    pub pr_branch: Option<String>,

    /// Status to set on the directive (e.g., completed, paused)
    #[arg(long)]
    pub status: Option<String>,
}

