//! Codegen configuration — deserialized from `mizan.toml` at the consumer
//! project root. Replaces the JS substrate's `mizan.config.mjs`.
//!
//! Example:
//!
//! ```toml
//! project_id = "blazr-studio"
//! output = "src/api"
//! targets = ["react"]
//!
//! [source.fastapi]
//! module = "blazr_session.handlers"
//! cwd = "../.."
//! command = ["uv", "run", "python"]
//!
//! [rust_kernel]
//! path = "../../mizan/frontends/mizan-rust"
//! ```

use std::collections::BTreeMap;
use std::path::PathBuf;

use serde::Deserialize;


#[derive(Debug, Deserialize)]
pub struct Config {
    #[serde(default)]
    pub project_id: Option<String>,

    #[serde(default = "default_output")]
    pub output: PathBuf,

    #[serde(default = "default_targets")]
    pub targets: Vec<String>,

    #[serde(default)]
    pub source: SourceConfig,

    #[serde(default)]
    pub rust_kernel: Option<RustKernelSpec>,

    #[serde(default)]
    pub rust_crate_name: Option<String>,
}


fn default_output() -> PathBuf {
    PathBuf::from("src/api")
}


fn default_targets() -> Vec<String> {
    vec!["react".to_string()]
}


#[derive(Debug, Deserialize, Default)]
pub struct SourceConfig {
    #[serde(default)]
    pub fastapi: Option<FastapiSource>,

    #[serde(default)]
    pub django: Option<DjangoSource>,
}


#[derive(Debug, Deserialize)]
pub struct FastapiSource {
    pub module: String,

    #[serde(default)]
    pub cwd: Option<PathBuf>,

    #[serde(default)]
    pub python: Option<String>,

    #[serde(default)]
    pub command: Option<Vec<String>>,

    #[serde(default)]
    pub env: BTreeMap<String, String>,
}


#[derive(Debug, Deserialize)]
pub struct DjangoSource {
    pub manage_path: PathBuf,

    #[serde(default)]
    pub python: Option<String>,

    #[serde(default)]
    pub command: Option<Vec<String>>,

    #[serde(default)]
    pub env: BTreeMap<String, String>,
}


#[derive(Debug, Deserialize, Clone)]
#[serde(untagged)]
pub enum RustKernelSpec {
    Path {
        path: String,
    },
    Git {
        git: String,
        #[serde(default)]
        tag: Option<String>,
        #[serde(default)]
        rev: Option<String>,
        #[serde(default)]
        branch: Option<String>,
    },
    Version {
        version: String,
    },
}
