mod completer;

use crate::cli::completer::{
    ShellCompletion, agent_completer, macro_completer, mcp_server_completer, model_completer,
    rag_completer, role_completer, secrets_completer, session_completer,
};
use crate::config::{AssetCategory, InstallFilter, MemoryScope};
use anyhow::{Context, Result};
use clap::{ArgGroup, ValueHint};
use clap::{Parser, crate_authors, crate_description, crate_version};
use clap_complete::ArgValueCompleter;
use is_terminal::IsTerminal;
use std::collections::HashSet;
use std::io::{Read, stdin};

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
#[command(
	name = "coyote",
	author = crate_authors!(),
	version = crate_version!(),
	about = crate_description!(),
	help_template = "\
{before-help}{name} {version}
{author-with-newline}
{about-with-newline}
{usage-heading} {usage}

{all-args}{after-help}
",
	group(
		ArgGroup::new("sbx-mode")
			.args(["sandbox", "fresh"])
			.multiple(true)
			.conflicts_with_all([
				"model", "prompt", "role", "session", "agent", "rag", "rebuild_rag",
				"macro_name", "execute", "code", "file", "no_stream", "no_memory",
				"init_memory", "dry_run", "info", "build_tools", "install",
				"install_from", "sync_models", "list_models", "list_roles",
				"list_sessions", "list_agents", "list_rags", "list_macros",
				"list_skills", "skill", "tail_logs", "completions", "update",
			])
	),
)]
pub struct Cli {
    /// Input text
    #[arg(trailing_var_arg = true)]
    text: Vec<String>,

    /// Select a LLM model
    #[arg(short, long, add = ArgValueCompleter::new(model_completer))]
    pub model: Option<String>,
    /// Use the system prompt
    #[arg(long)]
    pub prompt: Option<String>,
    /// Select a role
    #[arg(short, long, add = ArgValueCompleter::new(role_completer))]
    pub role: Option<String>,
    /// Execute commands in natural language
    #[arg(short = 'e', long)]
    pub execute: bool,
    /// Output code only
    #[arg(short = 'c', long)]
    pub code: bool,
    /// Include files, directories, or URLs
    #[arg(short = 'f', long, value_name = "FILE|URL", value_hint = ValueHint::AnyPath)]
    pub file: Vec<String>,
    /// Turn off stream mode
    #[arg(short = 'S', long)]
    pub no_stream: bool,
    /// Render markdown as raw text with syntax highlighting only (skip the rich markdown renderer)
    #[arg(long)]
    pub raw_markdown: bool,
    /// Display the message without sending it
    #[arg(long)]
    pub dry_run: bool,
    /// Disable loading workspace MCP servers from .coyote/mcp.json, .coyote/.mcp.json, or .mcp.json
    #[arg(long)]
    pub no_workspace_mcp: bool,
    /// Disable memory for this invocation
    #[arg(long)]
    pub no_memory: bool,
    /// Disable loading workspace instructions (COYOTE.md/AGENTS.md/CLAUDE.md/etc.) for this invocation
    #[arg(long)]
    pub no_workspace_instructions: bool,
    /// Override the workspace instructions file chain for this invocation (repeatable, priority order)
    #[arg(long, value_name = "NAME")]
    pub workspace_instructions_file: Vec<String>,
    /// Skip permission prompts by setting AUTO_CONFIRM for all tools (dangerous!)
    #[arg(long)]
    pub dangerously_skip_permissions: bool,

    /// Start or join a session
    #[arg(short = 's', long, help_heading = "Session & Memory", add = ArgValueCompleter::new(session_completer))]
    pub session: Option<Option<String>>,
    /// Ensure the session is empty
    #[arg(long, help_heading = "Session & Memory")]
    pub empty_session: bool,
    /// Ensure the new conversation is saved to the session
    #[arg(long, help_heading = "Session & Memory")]
    pub save_session: bool,
    /// Bootstrap a memory marker so coyote begins loading memory next run
    #[arg(
        long,
        value_name = "SCOPE",
        value_enum,
        help_heading = "Session & Memory"
    )]
    pub init_memory: Option<MemoryScope>,
    /// Scaffold a COYOTE.md workspace instructions file in the current directory
    #[arg(long, help_heading = "Session & Memory")]
    pub init_instructions: bool,
    /// Pre-load an existing skill into the session (repeatable). If a single
    /// `--skill <NAME>` is given and the skill doesn't exist, opens $EDITOR
    /// with a scaffold to create it.
    #[arg(long, value_name = "NAME", help_heading = "Session & Memory")]
    pub skill: Vec<String>,

    /// Start an agent
    #[arg(short = 'a', long, help_heading = "Agents, RAG & Macros", add = ArgValueCompleter::new(agent_completer))]
    pub agent: Option<String>,
    /// Set agent variables
    #[arg(long, value_names = ["NAME", "VALUE"], num_args = 2, help_heading = "Agents, RAG & Macros")]
    pub agent_variable: Vec<String>,
    /// Start a RAG
    #[arg(long, help_heading = "Agents, RAG & Macros", add = ArgValueCompleter::new(rag_completer))]
    pub rag: Option<String>,
    /// Rebuild the RAG to sync document changes
    #[arg(long, help_heading = "Agents, RAG & Macros")]
    pub rebuild_rag: bool,
    /// Execute a macro
    #[arg(long = "macro", value_name = "MACRO", help_heading = "Agents, RAG & Macros", add = ArgValueCompleter::new(macro_completer))]
    pub macro_name: Option<String>,

    /// List all available chat models
    #[arg(long, help_heading = "List & Discovery")]
    pub list_models: bool,
    /// List all roles
    #[arg(long, help_heading = "List & Discovery")]
    pub list_roles: bool,
    /// List all sessions
    #[arg(long, help_heading = "List & Discovery")]
    pub list_sessions: bool,
    /// List all agents
    #[arg(long, help_heading = "List & Discovery")]
    pub list_agents: bool,
    /// List all RAGs
    #[arg(long, help_heading = "List & Discovery")]
    pub list_rags: bool,
    /// List all macros
    #[arg(long, help_heading = "List & Discovery")]
    pub list_macros: bool,
    /// List all installed skills
    #[arg(long, help_heading = "List & Discovery")]
    pub list_skills: bool,

    /// Reinstall bundled assets, overwriting any local changes
    #[arg(
        long,
        value_name = "CATEGORY",
        value_enum,
        help_heading = "Installation & Updates"
    )]
    pub install: Option<AssetCategory>,
    /// Install assets from a remote git repository (URL may be suffixed with #<ref>)
    #[arg(long, value_name = "GIT_URL", help_heading = "Installation & Updates")]
    pub install_from: Option<String>,
    /// Restrict --install-from to a single asset category
    #[arg(
        long,
        value_name = "CATEGORY",
        value_enum,
        requires = "install_from",
        help_heading = "Installation & Updates"
    )]
    pub filter: Option<InstallFilter>,
    /// Overwrite all conflicts without prompting (used with --install-from)
    #[arg(
        long,
        requires = "install_from",
        help_heading = "Installation & Updates"
    )]
    pub install_force: bool,
    /// Sync models updates
    #[arg(long, help_heading = "Installation & Updates")]
    pub sync_models: bool,
    /// Update Coyote to the latest release, or to a specific version
    #[arg(long, value_name = "VERSION", help_heading = "Installation & Updates")]
    pub update: Option<Option<String>>,
    /// With --update, update even if Coyote was installed via a package manager
    #[arg(long, requires = "update", help_heading = "Installation & Updates")]
    pub force: bool,

    /// Add a secret to the Coyote vault
    #[arg(
        long,
        value_name = "SECRET_NAME",
        exclusive = true,
        help_heading = "Vault & Secrets"
    )]
    pub add_secret: Option<String>,
    /// Decrypt a secret from the Coyote vault and print the plaintext
    #[arg(long, value_name = "SECRET_NAME", exclusive = true, help_heading = "Vault & Secrets", add = ArgValueCompleter::new(secrets_completer))]
    pub get_secret: Option<String>,
    /// Update an existing secret in the Coyote vault
    #[arg(long, value_name = "SECRET_NAME", exclusive = true, help_heading = "Vault & Secrets", add = ArgValueCompleter::new(secrets_completer))]
    pub update_secret: Option<String>,
    /// Delete a secret from the Coyote vault
    #[arg(long, value_name = "SECRET_NAME", exclusive = true, help_heading = "Vault & Secrets", add = ArgValueCompleter::new(secrets_completer))]
    pub delete_secret: Option<String>,
    /// List all secrets stored in the Coyote vault
    #[arg(long, exclusive = true, help_heading = "Vault & Secrets")]
    pub list_secrets: bool,

    /// Authenticate with an LLM provider using OAuth (e.g., --authenticate client_name)
    #[arg(
        long,
        exclusive = true,
        value_name = "CLIENT_NAME",
        help_heading = "Authentication"
    )]
    pub authenticate: Option<Option<String>>,
    /// Authenticate with an OAuth-protected remote MCP server (e.g., --auth-mcp server_name)
    #[arg(long, exclusive = true, value_name = "SERVER_NAME", help_heading = "Authentication", add = ArgValueCompleter::new(mcp_server_completer))]
    pub auth_mcp: Option<String>,

    /// Launch Coyote inside a Docker sandbox (via `sbx`); name defaults to current directory basename
    #[arg(long, value_name = "NAME", help_heading = "Sandbox")]
    pub sandbox: Option<Option<String>>,
    /// Start the sandbox with a clean slate. No copied config or tokens; LLM credentials injected via sbx proxy
    #[arg(long, requires = "sandbox", help_heading = "Sandbox")]
    pub fresh: bool,
    /// Declare that no human is present. All user-interaction tools return structured JSON instead of
    /// prompting. Implies --dangerously-skip-permissions. Incompatible with REPL mode (requires a prompt).
    #[arg(long, help_heading = "Sandbox")]
    pub headless: bool,
    /// Run as an ACP agent server over stdio (JSON-RPC 2.0). Every stdout byte must be valid JSON-RPC.
    /// Implies --headless. Single session per process.
    #[arg(long, help_heading = "Sandbox")]
    pub acp_server: bool,
    /// Display information
    #[arg(long, help_heading = "Diagnostics & Tools")]
    pub info: bool,
    /// Build all configured Bash tool scripts
    #[arg(long, help_heading = "Diagnostics & Tools")]
    pub build_tools: bool,
    /// Tail logs
    #[arg(long, help_heading = "Diagnostics & Tools")]
    pub tail_logs: bool,
    /// Disable colored log output
    #[arg(long, requires = "tail_logs", help_heading = "Diagnostics & Tools")]
    pub disable_log_colors: bool,

    /// Generate static shell completion scripts
    #[arg(long, value_name = "SHELL", value_enum, help_heading = "Shell")]
    pub completions: Option<ShellCompletion>,
}

impl Cli {
    pub fn skills(&self) -> Vec<String> {
        let mut seen = HashSet::new();
        let mut out = Vec::with_capacity(self.skill.len());
        for name in &self.skill {
            if seen.insert(name.clone()) {
                out.push(name.clone());
            }
        }

        out
    }

    pub fn text(&self) -> Result<Option<String>> {
        let mut stdin_text = String::new();
        if !stdin().is_terminal() {
            let _ = stdin()
                .read_to_string(&mut stdin_text)
                .context("Invalid stdin pipe")?;
        };
        match self.text.is_empty() {
            true => {
                if stdin_text.is_empty() {
                    Ok(None)
                } else {
                    Ok(Some(stdin_text))
                }
            }
            false => {
                if self.macro_name.is_some() {
                    let text = self
                        .text
                        .iter()
                        .map(|v| shell_words::quote(v))
                        .collect::<Vec<_>>()
                        .join(" ");
                    if stdin_text.is_empty() {
                        Ok(Some(text))
                    } else {
                        Ok(Some(format!("{text} -- {stdin_text}")))
                    }
                } else {
                    let text = self.text.join(" ");
                    if stdin_text.is_empty() {
                        Ok(Some(text))
                    } else {
                        Ok(Some(format!("{text}\n{stdin_text}")))
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use clap::Parser;

    fn parse(args: &[&str]) -> Cli {
        let mut full_args = vec!["coyote"];
        full_args.extend_from_slice(args);
        Cli::try_parse_from(full_args).unwrap()
    }

    #[test]
    fn parse_no_args_defaults() {
        let cli = parse(&[]);
        assert!(cli.model.is_none());
        assert!(cli.role.is_none());
        assert!(cli.session.is_none());
        assert!(cli.agent.is_none());
        assert!(!cli.execute);
        assert!(!cli.code);
        assert!(!cli.no_stream);
        assert!(!cli.dry_run);
        assert!(!cli.info);
        assert!(!cli.build_tools);
        assert!(cli.file.is_empty());
        assert!(cli.text.is_empty());
    }

    #[test]
    fn parse_model_flag() {
        let cli = parse(&["--model", "gpt-4o"]);
        assert_eq!(cli.model, Some("gpt-4o".to_string()));
    }

    #[test]
    fn parse_model_short_flag() {
        let cli = parse(&["-m", "gpt-4o"]);
        assert_eq!(cli.model, Some("gpt-4o".to_string()));
    }

    #[test]
    fn parse_role_flag() {
        let cli = parse(&["--role", "coder"]);
        assert_eq!(cli.role, Some("coder".to_string()));
    }

    #[test]
    fn parse_session_with_name() {
        let cli = parse(&["--session", "my-session"]);
        assert_eq!(cli.session, Some(Some("my-session".to_string())));
    }

    #[test]
    fn parse_agent_flag() {
        let cli = parse(&["--agent", "sisyphus"]);
        assert_eq!(cli.agent, Some("sisyphus".to_string()));
    }

    #[test]
    fn parse_agent_short_flag() {
        let cli = parse(&["-a", "sisyphus"]);
        assert_eq!(cli.agent, Some("sisyphus".to_string()));
    }

    #[test]
    fn parse_execute_flag() {
        let cli = parse(&["-e", "list files"]);
        assert!(cli.execute);
    }

    #[test]
    fn parse_code_flag() {
        let cli = parse(&["-c", "hello world"]);
        assert!(cli.code);
    }

    #[test]
    fn parse_no_stream_flag() {
        let cli = parse(&["-S", "test"]);
        assert!(cli.no_stream);
    }

    #[test]
    fn parse_dry_run_flag() {
        let cli = parse(&["--dry-run", "test"]);
        assert!(cli.dry_run);
    }

    #[test]
    fn parse_info_flag() {
        let cli = parse(&["--info"]);
        assert!(cli.info);
    }

    #[test]
    fn parse_list_flags() {
        assert!(parse(&["--list-models"]).list_models);
        assert!(parse(&["--list-roles"]).list_roles);
        assert!(parse(&["--list-sessions"]).list_sessions);
        assert!(parse(&["--list-agents"]).list_agents);
        assert!(parse(&["--list-rags"]).list_rags);
        assert!(parse(&["--list-macros"]).list_macros);
        assert!(parse(&["--list-skills"]).list_skills);
    }

    #[test]
    fn parse_skill_flag_takes_name() {
        assert_eq!(parse(&["--skill", "git-master"]).skill, vec!["git-master"]);
        assert!(parse(&[]).skill.is_empty());
    }

    #[test]
    fn parse_multiple_skill_flags_preserves_order() {
        assert_eq!(
            parse(&["--skill", "alpha", "--skill", "beta", "--skill", "gamma"]).skill,
            vec!["alpha", "beta", "gamma"]
        );
    }

    #[test]
    fn skills_method_dedupes_preserving_first_occurrence() {
        let cli = parse(&[
            "--skill", "alpha", "--skill", "beta", "--skill", "alpha", "--skill", "gamma",
            "--skill", "beta",
        ]);

        assert_eq!(cli.skills(), vec!["alpha", "beta", "gamma"]);
    }

    #[test]
    fn skills_method_returns_empty_when_no_flags() {
        assert!(parse(&[]).skills().is_empty());
    }

    #[test]
    fn parse_file_flag_single() {
        let cli = parse(&["-f", "file.txt", "question"]);
        assert_eq!(cli.file, vec!["file.txt"]);
    }

    #[test]
    fn parse_file_flag_multiple() {
        let cli = parse(&["-f", "a.txt", "-f", "b.txt", "question"]);
        assert_eq!(cli.file, vec!["a.txt", "b.txt"]);
    }

    #[test]
    fn parse_trailing_text() {
        let cli = parse(&["hello", "world"]);
        assert_eq!(cli.text, vec!["hello", "world"]);
    }

    #[test]
    fn parse_prompt_flag() {
        let cli = parse(&["--prompt", "be a pirate"]);
        assert_eq!(cli.prompt, Some("be a pirate".to_string()));
    }

    #[test]
    fn parse_empty_session_flag() {
        let cli = parse(&["--session", "s", "--empty-session"]);
        assert!(cli.empty_session);
    }

    #[test]
    fn parse_save_session_flag() {
        let cli = parse(&["--session", "s", "--save-session"]);
        assert!(cli.save_session);
    }

    #[test]
    fn parse_build_tools_flag() {
        let cli = parse(&["--build-tools"]);
        assert!(cli.build_tools);
    }

    #[test]
    fn parse_dangerously_skip_permissions_flag() {
        let cli = parse(&["--dangerously-skip-permissions"]);
        assert!(cli.dangerously_skip_permissions);
    }

    #[test]
    fn parse_dangerously_skip_permissions_default_off() {
        let cli = parse(&[]);
        assert!(!cli.dangerously_skip_permissions);
    }

    #[test]
    fn parse_headless_flag() {
        let cli = parse(&["--headless", "do something"]);
        assert!(cli.headless);
    }

    #[test]
    fn parse_headless_default_off() {
        assert!(!parse(&[]).headless);
    }

    #[test]
    fn parse_acp_server_flag() {
        let cli = parse(&["--acp-server"]);
        assert!(cli.acp_server);
    }

    #[test]
    fn parse_acp_server_default_off() {
        assert!(!parse(&[]).acp_server);
    }

    #[test]
    fn parse_sync_models_flag() {
        let cli = parse(&["--sync-models"]);
        assert!(cli.sync_models);
    }

    #[test]
    fn parse_model_with_role() {
        let cli = parse(&["-m", "gpt-4o", "-r", "coder"]);
        assert_eq!(cli.model, Some("gpt-4o".to_string()));
        assert_eq!(cli.role, Some("coder".to_string()));
    }

    #[test]
    fn parse_agent_with_file_and_text() {
        let cli = parse(&["-a", "sisyphus", "-f", "code.rs", "explain", "this"]);
        assert_eq!(cli.agent, Some("sisyphus".to_string()));
        assert_eq!(cli.file, vec!["code.rs"]);
        assert_eq!(cli.text, vec!["explain", "this"]);
    }

    #[test]
    fn parse_role_with_session() {
        let cli = parse(&["-r", "coder", "-s", "dev-session"]);
        assert_eq!(cli.role, Some("coder".to_string()));
        assert_eq!(cli.session, Some(Some("dev-session".to_string())));
    }

    #[test]
    fn cli_text_returns_none_when_no_text_no_stdin() {
        let cli = parse(&[]);
        assert!(cli.text().unwrap().is_none());
    }

    #[test]
    fn cli_text_joins_trailing_args() {
        let cli = parse(&["hello", "world"]);
        assert_eq!(cli.text().unwrap(), Some("hello world".to_string()));
    }

    #[test]
    fn parse_add_secret_flag() {
        let cli = parse(&["--add-secret", "MY_KEY"]);
        assert_eq!(cli.add_secret, Some("MY_KEY".to_string()));
    }

    #[test]
    fn parse_get_secret_flag() {
        let cli = parse(&["--get-secret", "MY_KEY"]);
        assert_eq!(cli.get_secret, Some("MY_KEY".to_string()));
    }

    #[test]
    fn parse_list_secrets_flag() {
        let cli = parse(&["--list-secrets"]);
        assert!(cli.list_secrets);
    }

    #[test]
    fn parse_rag_flag() {
        let cli = parse(&["--rag", "my-rag"]);
        assert_eq!(cli.rag, Some("my-rag".to_string()));
    }

    #[test]
    fn parse_macro_flag() {
        let cli = parse(&["--macro", "my-macro"]);
        assert_eq!(cli.macro_name, Some("my-macro".to_string()));
    }

    #[test]
    fn parse_update_flag_no_value() {
        let cli = parse(&["--update"]);

        assert_eq!(cli.update, Some(None));
    }

    #[test]
    fn parse_update_flag_with_version() {
        let cli = parse(&["--update", "v0.4.0"]);

        assert_eq!(cli.update, Some(Some("v0.4.0".to_string())));
    }

    #[test]
    fn parse_update_with_force() {
        let cli = parse(&["--update", "--force"]);

        assert_eq!(cli.update, Some(None));
        assert!(cli.force);
    }

    #[test]
    fn parse_force_without_update_fails() {
        assert!(Cli::try_parse_from(["coyote", "--force"]).is_err());
    }

    #[test]
    fn parse_sandbox_flag_no_value() {
        let cli = parse(&["--sandbox"]);
        assert_eq!(cli.sandbox, Some(None));
    }

    #[test]
    fn parse_sandbox_flag_with_name() {
        let cli = parse(&["--sandbox", "my-box"]);
        assert_eq!(cli.sandbox, Some(Some("my-box".to_string())));
    }

    #[test]
    fn parse_sandbox_is_exclusive() {
        assert!(Cli::try_parse_from(["coyote", "--sandbox", "--agent", "foo"]).is_err());
    }
}
