use newton_prover_core::config::ConfigLoader;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
#[serde(default)]
pub struct DataProviderConfig {
    /// max cache size bytes
    pub max_cache_size: u64,
    /// instruction limit
    pub fuel: u64,
    /// max wasm stack size bytes
    pub max_wasm_stack: u64,
    /// max http calls per wasm execution
    pub max_http_calls: u32,
    /// max http request size (body + headers) bytes
    pub max_http_request_size: u32,
    /// data cache url
    pub data_cache_url: String,
    /// KMS RSA key ID for decrypting secrets
    pub kms_rsa_key_id: String,
}

impl Default for DataProviderConfig {
    fn default() -> Self {
        Self {
            max_cache_size: 100 * 1024 * 1024, // 100 MiB
            fuel: 100_000_000,                 // 100M instructions
            max_wasm_stack: 1024 * 1024 * 64,  // 64 MiB
            max_http_calls: 50,
            max_http_request_size: 1024 * 1024, // 1 MiB
            data_cache_url: "redis://:redis@127.0.0.1:6379".to_string(),
            kms_rsa_key_id: std::env::var("DATA_PROVIDER_KMS_RSA_KEY_ID").unwrap_or_default(),
        }
    }
}

// Implement ConfigLoader trait - provides load_config method automatically
impl ConfigLoader for DataProviderConfig {
    const FILE_NAME: &'static str = "data-provider";
    const ENV_PREFIX: &'static str = "DATA_PROVIDER";
}

impl DataProviderConfig {
    pub fn set_max_cache_size(&mut self, max_cache_size: u64) {
        self.max_cache_size = max_cache_size;
    }

    pub fn set_fuel(&mut self, fuel: u64) {
        self.fuel = fuel;
    }

    pub fn set_max_wasm_stack(&mut self, max_wasm_stack: u64) {
        self.max_wasm_stack = max_wasm_stack;
    }

    pub fn set_max_http_calls(&mut self, max_http_calls: u32) {
        self.max_http_calls = max_http_calls;
    }

    pub fn set_max_http_request_size(&mut self, max_http_request_size: u32) {
        self.max_http_request_size = max_http_request_size;
    }

    pub fn set_data_cache_url(&mut self, data_cache_url: String) {
        self.data_cache_url = data_cache_url;
    }

    pub fn set_kms_rsa_key_id(&mut self, kms_rsa_key_id: String) {
        self.kms_rsa_key_id = kms_rsa_key_id;
    }
}
