// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashMap;
use std::path::PathBuf;

use clap::ValueEnum;
use dynamo_llm::entrypoint::RouterConfig;
use dynamo_llm::kv_router::KvRouterConfig;
use dynamo_llm::mocker::protocols::MockEngineArgs;
use dynamo_runtime::pipeline::RouterMode as RuntimeRouterMode;

use crate::Output;

/// Required options depend on the in and out choices
#[derive(clap::Parser, Debug, Clone)]
#[command(version, about, long_about = None)]
pub struct Flags {
    /// The model. The options depend on the engine.
    ///
    /// The full list - only mistralrs supports all three currently:
    /// - Full path of a checked out Hugging Face repository containing safetensor files
    /// - Name of a Hugging Face repository, e.g 'google/flan-t5-small'. The model will be
    ///   downloaded and cached.
    #[arg(index = 1)]
    pub model_path_pos: Option<PathBuf>,

    // `--model-path`. The one above is `dynamo-run <positional-model-path>`
    #[arg(long = "model-path")]
    pub model_path_flag: Option<PathBuf>,

    /// HTTP port. `in=http` only
    /// If tls_cert_path and tls_key_path are provided, this will be TLS/HTTPS.
    #[arg(long, default_value = "8000")]
    pub http_port: u16,

    /// TLS certificate file
    #[arg(long, requires = "tls_key_path")]
    pub tls_cert_path: Option<PathBuf>,

    /// TLS certificate key file
    #[arg(long, requires = "tls_cert_path")]
    pub tls_key_path: Option<PathBuf>,

    /// The name of the model we are serving
    #[arg(long)]
    pub model_name: Option<String>,

    /// Verbose output (-v for debug, -vv for trace)
    #[arg(short = 'v', action = clap::ArgAction::Count, default_value_t = 0)]
    pub verbosity: u8,

    /// If using `out=dyn` with multiple instances, this says how to route the requests.
    ///
    /// Mostly interesting for KV-aware routing.
    /// Defaults to RouterMode::RoundRobin
    #[arg(long, default_value = "round-robin")]
    pub router_mode: RouterMode,

    /// KV Router: Weight for overlap score in worker selection.
    /// Higher values prioritize KV cache reuse. Default: 1.0
    #[arg(long)]
    pub kv_overlap_score_weight: Option<f64>,

    /// KV Router: Temperature for worker sampling via softmax.
    /// Higher values promote more randomness, and 0 fallbacks to deterministic.
    /// Default: 0.0
    #[arg(long)]
    pub router_temperature: Option<f64>,

    /// KV Router: Whether to use KV events to maintain the view of cached blocks
    /// If false, the router predicts cache state based on routing decisions
    /// with TTL-based expiration and pruning, rather than receiving events from workers.
    /// Default: true
    #[arg(long)]
    pub use_kv_events: Option<bool>,

    /// KV Router: Whether to enable replica synchronization across multiple router instances.
    /// When true, routers will publish and subscribe to events to maintain consistent state.
    /// Default: false
    #[arg(long)]
    pub router_replica_sync: Option<bool>,

    /// KV Router: Whether to track active blocks in the router for memory management.
    /// When false, the router will not maintain state about which blocks are active,
    /// reducing memory overhead but potentially affecting scheduling decisions.
    /// Default: true
    #[arg(long)]
    pub router_track_active_blocks: Option<bool>,

    /// Max model context length. Reduce this if you don't have enough VRAM for the full model
    /// context length (e.g. Llama 4).
    /// Defaults to the model's max, which is usually model_max_length in tokenizer_config.json.
    #[arg(long)]
    pub context_length: Option<u32>,

    /// KV cache block size (is this used? Maybe by Python vllm worker?)
    #[arg(long)]
    pub kv_cache_block_size: Option<u32>,

    /// Mocker engine only.
    /// Additional engine-specific arguments from a JSON file.
    /// Contains a mapping of parameter names to values.
    #[arg(long)]
    pub extra_engine_args: Option<PathBuf>,

    /// Path to a JSON file containing default request fields.
    /// These fields will be merged with each request, but can be overridden by the request.
    /// Example file contents:
    /// {
    ///     "model": "Qwen2.5-3B-Instruct",
    ///     "temperature": 0.7,
    ///     "max_completion_tokens": 4096
    /// }
    #[arg(long)]
    pub request_template: Option<PathBuf>,

    /// How many times a request can be migrated to another worker if the HTTP server lost
    /// connection to the current worker.
    #[arg(long, value_parser = clap::value_parser!(u32).range(0..1024))]
    pub migration_limit: Option<u32>,

    /// Which key-value backend to use: etcd, mem, file.
    /// Etcd uses the ETCD_* env vars (e.g. ETCD_ENDPOINTS) for connection details.
    /// File uses root dir from env var DYN_FILE_KV or defaults to $TMPDIR/dynamo_store_kv.
    #[arg(long, default_value = "etcd", value_parser = ["etcd", "file", "mem"])]
    pub store_kv: String,

    /// Determines how requests are distributed from routers to workers. 'tcp' is fastest [nats|http|tcp].
    #[arg(long, default_value = "tcp", value_parser = ["nats", "http", "tcp"])]
    pub request_plane: String,

    /// Everything after a `--`. Not currently used.
    #[arg(index = 2, last = true, hide = true, allow_hyphen_values = true)]
    pub last: Vec<String>,
}

impl Flags {
    /// For each Output variant, check if it would be able to run.
    /// This takes validation out of the main engine creation path.
    pub fn validate(&self, out_opt: &Output) -> anyhow::Result<()> {
        match out_opt {
            Output::Auto => {
                if self.context_length.is_some() {
                    anyhow::bail!(
                        "'--context-length' flag should only be used on the worker node, not on the ingress"
                    );
                }
                if self.kv_cache_block_size.is_some() {
                    anyhow::bail!(
                        "'--kv-cache-block-size' flag should only be used on the worker node, not on the ingress"
                    );
                }
            }
            Output::Echo => {}
            #[cfg(feature = "mistralrs")]
            Output::MistralRs => {}
            Output::Mocker => {
                // nothing to check here
            }
        }

        match out_opt {
            Output::Mocker => {}
            _ => {
                if self.extra_engine_args.is_some() {
                    anyhow::bail!("`--extra-engine-args` is only for the mocker engine");
                }
            }
        }

        Ok(())
    }

    pub fn router_config(&self) -> RouterConfig {
        let mut cfg = KvRouterConfig::default();
        if let Some(w) = self.kv_overlap_score_weight {
            cfg.overlap_score_weight = w;
        }
        if let Some(t) = self.router_temperature {
            cfg.router_temperature = t;
        }
        if let Some(v) = self.use_kv_events {
            cfg.use_kv_events = v;
        }
        if let Some(v) = self.router_replica_sync {
            cfg.router_replica_sync = v;
        }
        if let Some(v) = self.router_track_active_blocks {
            cfg.router_track_active_blocks = v;
        }
        RouterConfig::new(self.router_mode.into(), cfg)
    }

    /// Load extra engine arguments from a JSON file
    /// Returns a HashMap of parameter names to values
    pub fn load_extra_engine_args(
        &self,
    ) -> anyhow::Result<Option<HashMap<String, serde_json::Value>>> {
        if let Some(path) = &self.extra_engine_args {
            let file_content = std::fs::read_to_string(path)?;
            let args: HashMap<String, serde_json::Value> = serde_json::from_str(&file_content)?;
            Ok(Some(args))
        } else {
            Ok(None)
        }
    }

    pub fn mocker_config(&self) -> MockEngineArgs {
        let Some(path) = &self.extra_engine_args else {
            tracing::warn!("Did not specify extra engine args. Using default mocker args.");
            return MockEngineArgs::default();
        };
        MockEngineArgs::from_json_file(path)
            .unwrap_or_else(|e| panic!("Failed to build mocker engine args from {path:?}: {e}"))
    }
}

#[derive(Default, PartialEq, Eq, ValueEnum, Clone, Debug, Copy)]
pub enum RouterMode {
    #[default]
    #[value(name = "round-robin")]
    RoundRobin,
    Random,
    #[value(name = "kv")]
    KV,
}

impl From<RouterMode> for RuntimeRouterMode {
    fn from(r: RouterMode) -> RuntimeRouterMode {
        match r {
            RouterMode::RoundRobin => RuntimeRouterMode::RoundRobin,
            RouterMode::Random => RuntimeRouterMode::Random,
            RouterMode::KV => RuntimeRouterMode::KV,
        }
    }
}
