//! Arguments for `mcptest init`.

use std::time::Duration;

use clap::{Args, ValueEnum};

use crate::cli::parsers::parse_duration;

/// Arguments for `mcptest init`.
///
/// Scaffolds a starter `tests/example.yaml`, a `tests/example-server.yml`
/// mock catalog, and a top-level `mcptest.yml` project suite in the current
/// directory, so a bare `mcptest run` passes offline right after init.
/// `--with-jury` opts into the v1.1 jury surface
/// scaffold; `--force` overwrites existing files (default behavior is to
/// refuse so a stray `init` does not clobber a real suite). When `--url`
/// is supplied the scaffolder forks to the URL-target template and lets
/// the operator pick an auth recipe via `--auth-*` flags.
#[derive(Debug, Args, Default)]
pub struct InitArgs {
    /// Append a v1.1 jury (`llm-judge`) example to the scaffolded test file.
    #[arg(long = "with-jury")]
    pub with_jury: bool,

    /// Overwrite existing files instead of refusing.
    #[arg(long)]
    pub force: bool,

    /// Scaffold a URL-target suite instead of the stdio template.
    ///
    /// When set, the example test file uses `server.url:` and the chosen
    /// `--auth-*` recipe rather than a `command:` block.
    #[arg(long = "url", value_name = "URL")]
    pub url: Option<String>,

    /// Scaffold a suite from a server discovered in a local MCP client config
    /// (the same servers `mcptest doctor` lists). Pass the server's name.
    /// Conflicts with `--url`.
    #[arg(long = "from-discovered", value_name = "NAME", conflicts_with = "url")]
    pub from_discovered: Option<String>,

    /// Auth recipe for the URL target. One of `none`, `bearer`, `oauth2`,
    /// or `custom-header`. Defaults to `none` for an open dev server.
    #[arg(
        long = "auth",
        value_enum,
        value_name = "RECIPE",
        default_value_t = InitAuthKind::None
    )]
    pub auth: InitAuthKind,

    /// Env var name for the bearer token. Required when `--auth bearer`.
    #[arg(long = "auth-env", value_name = "NAME")]
    pub auth_env: Option<String>,

    /// Custom header name. Required when `--auth custom-header`.
    #[arg(long = "header-name", value_name = "NAME")]
    pub header_name: Option<String>,

    /// Optional env var holding the custom header value. When unset the
    /// scaffold emits a literal `REPLACE_ME` placeholder.
    #[arg(long = "header-value-env", value_name = "VAR")]
    pub header_env: Option<String>,

    /// Poll the server until `initialize` succeeds before running tests.
    ///
    /// Accepts the same `60s`/`2m`/`1h` shape as the global
    /// `--wait-for-ready` flag. The value lands in the scaffolded YAML
    /// under `server.wait_for_ready`.
    #[arg(long = "wait-for-ready", value_name = "DURATION", value_parser = parse_duration)]
    pub wait_for_ready: Option<Duration>,
}

/// Auth recipes the init wizard can scaffold.
///
/// Mirrors `commands::init::InitAuth` one-to-one; the parsing layer keeps
/// the user-facing `--auth` flag as a single value rather than three
/// boolean switches so the CLI surface stays compact.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, ValueEnum)]
#[value(rename_all = "kebab-case")]
pub enum InitAuthKind {
    /// Server accepts open requests.
    #[default]
    None,
    /// Bearer token from an env var.
    Bearer,
    /// OAuth 2.1 authorization-code + PKCE.
    Oauth2,
    /// Single custom HTTP header.
    CustomHeader,
}
