use std::path::{Path, PathBuf};

use serde::Deserialize;

use crate::error::ControllerError;

#[derive(Debug, Clone, Deserialize)]
pub struct ControllerConfig {
    pub nats_url: String,
    pub workspace: String,
    /// Path to the Ed25519 public key PEM used to verify keeper-signed JWTs.
    pub keeper_public_key_path: PathBuf,
    /// HTTP base URL of the orchestrator API.
    pub orchestrator_url: String,
    /// SurrealDB connection URL. Use `surrealkv://path/to/dir` for embedded,
    /// or `ws://host:port/rpc` for remote.
    pub surrealdb_url: String,
    #[serde(default)]
    pub nats_auth_token: Option<String>,
    /// Orchestrator HTTP auth token (Bearer).
    #[serde(default)]
    pub orchestrator_auth_token: Option<String>,
}

impl ControllerConfig {
    pub fn from_toml(path: &Path) -> Result<Self, ControllerError> {
        let raw = std::fs::read_to_string(path)?;
        toml::from_str(&raw).map_err(|e| ControllerError::Io(std::io::Error::other(e.to_string())))
    }
}
