use crate::AppConfig;

impl AppConfig {
    pub fn log_level(&self) -> anyhow::Result<String> {
        if let Some(level) = self.env.get("APP_LOG_LEVEL") {
            return Ok(level.to_string());
        }
        Ok("info".to_string())
    }

    pub fn log_format(&self) -> anyhow::Result<String> {
        if let Some(format) = self.env.get("APP_LOG_FORMAT") {
            return Ok(format.to_string());
        }
        Ok("json".to_string())
    }

    pub fn log_file_enabled(&self) -> anyhow::Result<bool> {
        if let Some(enabled) = self.env.get("APP_LOG_FILE_ENABLED") {
            return Ok(enabled.parse::<bool>()?);
        }
        Ok(false)
    }

    pub fn log_file_path(&self) -> anyhow::Result<String> {
        if let Some(path) = self.env.get("APP_LOG_FILE_PATH") {
            return Ok(path.to_string());
        }
        Ok("./logs".to_string())
    }

    pub fn log_file_rotation(&self) -> anyhow::Result<String> {
        if let Some(rotation) = self.env.get("APP_LOG_FILE_ROTATION") {
            return Ok(rotation.to_string());
        }
        Ok("daily".to_string())
    }

    pub fn log_file_max_files(&self) -> anyhow::Result<usize> {
        if let Some(max_files) = self.env.get("APP_LOG_FILE_MAX_FILES") {
            return Ok(max_files.parse::<usize>()?);
        }
        Ok(7)
    }

    pub fn log_file_max_size(&self) -> anyhow::Result<usize> {
        if let Some(max_size) = self.env.get("APP_LOG_FILE_MAX_SIZE") {
            return Ok(max_size.parse::<usize>()?);
        }
        Ok(104857600) // 100MB
    }

    pub fn otel_enabled(&self) -> anyhow::Result<bool> {
        if let Some(enabled) = self.env.get("APP_OTEL_ENABLED") {
            return Ok(enabled.parse::<bool>()?);
        }
        Ok(false)
    }

    pub fn otel_endpoint(&self) -> anyhow::Result<String> {
        if let Some(endpoint) = self.env.get("APP_OTEL_ENDPOINT") {
            return Ok(endpoint.to_string());
        }
        Ok("http://localhost:5080/api/default/v1/traces".to_string())
    }

    pub fn otel_service_name(&self) -> anyhow::Result<String> {
        if let Some(service_name) = self.env.get("APP_OTEL_SERVICE_NAME") {
            return Ok(service_name.to_string());
        }
        Ok(env!("CARGO_PKG_NAME").to_string())
    }

    pub fn otel_service_version(&self) -> anyhow::Result<String> {
        if let Some(service_version) = self.env.get("APP_OTEL_SERVICE_VERSION")
        {
            return Ok(service_version.to_string());
        }
        Ok(env!("CARGO_PKG_VERSION").to_string())
    }

    pub fn otel_authorization(&self) -> anyhow::Result<Option<String>> {
        if let Some(authorization) = self.env.get("APP_OTEL_AUTHORIZATION") {
            return Ok(Some(authorization.to_string()));
        }
        Ok(None)
    }

    pub fn otel_organization(&self) -> anyhow::Result<Option<String>> {
        if let Some(organization) = self.env.get("APP_OTEL_ORGANIZATION") {
            return Ok(Some(organization.to_string()));
        }
        Ok(None)
    }
}
