//! Node configuration

use serde::{Deserialize, Serialize};
use std::net::SocketAddr;
use std::path::PathBuf;

/// Storage node configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeConfig {
    /// Unique node identifier
    #[serde(default = "default_node_id")]
    pub node_id: String,

    /// gRPC address to listen on
    #[serde(default = "default_grpc_addr")]
    pub grpc_addr: SocketAddr,

    /// Data directory for chunk storage
    #[serde(default = "default_data_dir")]
    pub data_dir: PathBuf,

    /// Zone/rack identifier for placement
    #[serde(default)]
    pub zone: String,

    /// Region identifier
    #[serde(default)]
    pub region: String,

    /// Log level
    #[serde(default = "default_log_level")]
    pub log_level: String,

    /// Maximum storage capacity in bytes (0 = unlimited)
    #[serde(default)]
    pub max_capacity_bytes: u64,

    /// Metrics port for Prometheus scraping
    #[serde(default = "default_metrics_port")]
    pub metrics_port: u16,

    /// Whether chunk writes should be flushed to disk before success is returned.
    #[serde(default)]
    pub sync_on_write: bool,
}

fn default_node_id() -> String {
    uuid::Uuid::new_v4().to_string()
}

fn default_grpc_addr() -> SocketAddr {
    "0.0.0.0:9002".parse().unwrap()
}

fn default_data_dir() -> PathBuf {
    PathBuf::from("/var/lib/lightningstor-node/data")
}

fn default_log_level() -> String {
    "info".to_string()
}

fn default_metrics_port() -> u16 {
    9098
}

impl Default for NodeConfig {
    fn default() -> Self {
        Self {
            node_id: default_node_id(),
            grpc_addr: default_grpc_addr(),
            data_dir: default_data_dir(),
            zone: String::new(),
            region: String::new(),
            log_level: default_log_level(),
            max_capacity_bytes: 0,
            metrics_port: default_metrics_port(),
            sync_on_write: false,
        }
    }
}
