use std::path::PathBuf;

use crate::app_config::AppType;
use crate::codex_config::get_codex_auth_path;
use crate::config::get_claude_settings_path;
use crate::error::AppError;
use crate::gemini_config::get_gemini_dir;
use crate::opencode_config::get_opencode_dir;
use crate::settings::{get_hermes_override_dir, get_openclaw_override_dir};

/// 返回指定应用所使用的提示词文件路径。
pub fn prompt_file_path(app: &AppType) -> Result<PathBuf, AppError> {
    let base_dir: PathBuf = match app {
        AppType::Claude => get_base_dir_with_fallback(get_claude_settings_path(), ".claude")?,
        AppType::Codex => get_base_dir_with_fallback(get_codex_auth_path(), ".codex")?,
        AppType::Gemini => get_gemini_dir(),
        AppType::OpenCode => get_opencode_dir(),
        AppType::Hermes => get_hermes_override_dir().unwrap_or_else(default_hermes_dir),
        AppType::OpenClaw => get_openclaw_override_dir().unwrap_or_else(default_openclaw_dir),
    };

    let filename = match app {
        AppType::Claude => "CLAUDE.md",
        AppType::Codex => "AGENTS.md",
        AppType::Gemini => "GEMINI.md",
        AppType::OpenCode => "AGENTS.md",
        AppType::Hermes => "AGENTS.md",
        AppType::OpenClaw => "AGENTS.md",
    };

    Ok(base_dir.join(filename))
}

fn default_openclaw_dir() -> PathBuf {
    dirs::home_dir()
        .map(|home| home.join(".openclaw"))
        .unwrap_or_else(|| PathBuf::from(".openclaw"))
}

fn default_hermes_dir() -> PathBuf {
    dirs::home_dir()
        .map(|home| home.join(".hermes"))
        .unwrap_or_else(|| PathBuf::from(".hermes"))
}

fn get_base_dir_with_fallback(
    primary_path: PathBuf,
    fallback_dir: &str,
) -> Result<PathBuf, AppError> {
    primary_path
        .parent()
        .map(|p| p.to_path_buf())
        .or_else(|| dirs::home_dir().map(|h| h.join(fallback_dir)))
        .ok_or_else(|| {
            AppError::localized(
                "home_dir_not_found",
                format!("无法确定 {fallback_dir} 配置目录：用户主目录不存在"),
                format!("Cannot determine {fallback_dir} config directory: user home not found"),
            )
        })
}
