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

use zeroclaw::Config as ZeroClawConfig;

use crate::compat::cron_adapter::configure_embedded_cron;
use crate::compat::memory_adapter::configure_embedded_memory;
use crate::config::DeepSeekSettings;

const SGCLAW_ZEROCLAW_WORKSPACE_DIR: &str = ".sgclaw-zeroclaw-workspace";

pub fn build_zeroclaw_config(workspace_root: &Path) -> Result<ZeroClawConfig, crate::config::ConfigError> {
    let settings = DeepSeekSettings::from_env()?;
    Ok(build_zeroclaw_config_from_settings(
        workspace_root,
        &settings,
    ))
}

pub fn build_zeroclaw_config_from_settings(
    workspace_root: &Path,
    settings: &DeepSeekSettings,
) -> ZeroClawConfig {
    let workspace_dir = zeroclaw_workspace_dir(workspace_root);
    let mut config = ZeroClawConfig::default();
    config.workspace_dir = workspace_dir.clone();
    config.config_path = workspace_dir.join("config.toml");
    config.default_provider = Some("deepseek".to_string());
    config.default_model = Some(settings.model.clone());
    config.api_key = Some(settings.api_key.clone());
    config.api_url = Some(settings.base_url.clone());
    configure_embedded_memory(&mut config);
    configure_embedded_cron(&mut config);
    config
}

pub fn zeroclaw_workspace_dir(workspace_root: &Path) -> PathBuf {
    workspace_root.join(SGCLAW_ZEROCLAW_WORKSPACE_DIR)
}
