Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

87.7 KB
Raw Blame
//! 官方订阅额度查询服务
//!
//! 读取 CLI 工具的已有 OAuth 凭据,查询官方订阅额度。
//! 第一层:仅读取凭据,不实现登录/刷新。

use serde::{Deserialize, Serialize};
use std::time::{SystemTime, UNIX_EPOCH};

use std::collections::{HashMap, HashSet};

use crate::config;

// ── 数据类型 ──────────────────────────────────────────────

/// 凭据状态
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CredentialStatus {
    Valid,
    Expired,
    /// 访问令牌过期,但刷新令牌还能用:客户端下次运行时自己会换新的,不用重新登录。
    RefreshPending,
    NotFound,
    ParseError,
}

/// 单个限速窗口(如 5小时会话、7天周期)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct QuotaTier {
    /// 窗口标识:five_hour, seven_day, seven_day_fable, seven_day_opus 等
    pub name: String,
    /// 使用百分比 0–100
    pub utilization: f64,
    /// ISO 8601 重置时间
    pub resets_at: Option<String>,
    /// ZenMux: 已用额度(USD)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub used_value_usd: Option<f64>,
    /// ZenMux: 窗口上限(USD)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_value_usd: Option<f64>,
}

/// 超额使用信息
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExtraUsage {
    pub is_enabled: bool,
    pub monthly_limit: Option<f64>,
    pub used_credits: Option<f64>,
    pub utilization: Option<f64>,
    pub currency: Option<String>,
}

/// ChatGPT 订阅存下的限额重置次数(Codex「存下重置、需要时再用」)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ResetCredits {
    /// 每一次可用重置的到期时间(ISO 8601),先到期的在前,不过期的是 None 排最后;
    /// 长度就是可用次数(只收 available 且查询时还没过期的)
    pub expires_at: Vec<Option<String>>,
}

/// 订阅额度查询结果
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SubscriptionQuota {
    pub tool: String,
    pub credential_status: CredentialStatus,
    pub credential_message: Option<String>,
    pub success: bool,
    pub tiers: Vec<QuotaTier>,
    pub extra_usage: Option<ExtraUsage>,
    /// 只有 ChatGPT 订阅(codex / codex_oauth)有;没查到时为 None,不影响额度本身
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reset_credits: Option<ResetCredits>,
    /// ChatGPT 订阅买的 Codex Credits 余额(额度用完后才扣);没有、不限量或为 0 时为 None
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub credits_balance: Option<f64>,
    /// 套餐显示名(「Pro 200」「AI Pro」「SuperGrok Heavy」…),授权中心的账号行显示;
    /// 只有能顺带查到套餐的服务填,认不出或查不到时为 None
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub plan_name: Option<String>,
    pub error: Option<String>,
    pub queried_at: Option<i64>,
}

impl SubscriptionQuota {
    pub(crate) fn not_found(tool: &str) -> Self {
        Self {
            tool: tool.to_string(),
            credential_status: CredentialStatus::NotFound,
            credential_message: None,
            success: false,
            tiers: vec![],
            extra_usage: None,
            reset_credits: None,
            credits_balance: None,
            plan_name: None,
            error: None,
            queried_at: None,
        }
    }

    pub(crate) fn error(tool: &str, status: CredentialStatus, message: String) -> Self {
        Self {
            tool: tool.to_string(),
            credential_status: status,
            credential_message: Some(message.clone()),
            success: false,
            tiers: vec![],
            extra_usage: None,
            reset_credits: None,
            credits_balance: None,
            plan_name: None,
            error: Some(message),
            queried_at: Some(now_millis()),
        }
    }
}

// ── Claude 凭据读取 ──────────────────────────────────────

/// Claude OAuth 凭据文件中的嵌套结构
#[derive(Deserialize)]
struct ClaudeOAuthEntry {
    #[serde(rename = "accessToken")]
    access_token: Option<String>,
    #[serde(rename = "expiresAt")]
    expires_at: Option<serde_json::Value>,
    #[serde(rename = "refreshToken")]
    refresh_token: Option<String>,
    #[serde(rename = "refreshTokenExpiresAt")]
    refresh_token_expires_at: Option<serde_json::Value>,
}

/// 读取 Claude OAuth 凭据
///
/// 按优先级尝试以下来源:
/// 1. macOS Keychain(先匹配当前用户账户,再兼容只按 service 查询)
/// 2. 凭据文件 ~/.claude/.credentials.json
///
/// JSON 格式(两种 key 都兼容):
/// {"claudeAiOauth": {"accessToken": "...", "expiresAt": ...}}
/// {"claude.ai_oauth": {"accessToken": "...", "expiresAt": ...}}
fn read_claude_credentials() -> (Option<String>, CredentialStatus, Option<String>) {
    // 来源 1: macOS Keychain
    #[cfg(target_os = "macos")]
    {
        if let Some(result) = read_claude_credentials_from_keychain() {
            return result;
        }
    }

    // 来源 2: 凭据文件
    read_claude_credentials_from_file()
}

/// 从 macOS Keychain 读取 Claude 凭据
#[cfg(target_os = "macos")]
fn read_claude_credentials_from_keychain(
) -> Option<(Option<String>, CredentialStatus, Option<String>)> {
    let account = std::env::var("USER").ok().filter(|user| !user.is_empty());
    read_claude_credentials_from_keychain_with(account.as_deref(), |command| command.output())
}

#[cfg(target_os = "macos")]
fn read_claude_credentials_from_keychain_with(
    account: Option<&str>,
    run: impl FnMut(&mut std::process::Command) -> std::io::Result<std::process::Output>,
) -> Option<(Option<String>, CredentialStatus, Option<String>)> {
    claude_keychain_json_with(account, run).map(|json| parse_claude_credentials_json(&json))
}

/// 钥匙串里带 Claude 登录的那条凭据(JSON);读不了钥匙串或没有时为 `None`(交给文件)。
#[cfg(target_os = "macos")]
fn claude_keychain_json_with(
    account: Option<&str>,
    mut run: impl FnMut(&mut std::process::Command) -> std::io::Result<std::process::Output>,
) -> Option<String> {
    for account in account.into_iter().map(Some).chain(std::iter::once(None)) {
        let mut command = std::process::Command::new("security");
        command.args(["find-generic-password", "-s", "Claude Code-credentials"]);
        if let Some(account) = account {
            command.args(["-a", account]);
        }
        command.arg("-w");

        let output = match run(&mut command) {
            Ok(output) if output.status.success() => output,
            Ok(output) if output.status.code() == Some(SECURITY_ITEM_NOT_FOUND) => continue,
            _ => return None,
        };
        let Ok(json_str) = String::from_utf8(output.stdout) else {
            continue;
        };
        let json = json_str.trim();
        // 同名条目可能只存了 mcpOAuth;没有 Claude token 时继续找,最后回退到文件。
        // 已过期的 Claude token 仍是该账户的凭据,保留其过期/待刷新状态。
        if parse_claude_credentials_json(json).0.is_some() {
            return Some(json.to_string());
        }
    }
    None
}

/// 默认登录的套餐:和默认登录的额度读同一份凭据(钥匙串优先,再是 Claude 配置目录里的文件)。
pub fn read_claude_default_plan() -> Option<crate::services::claude_account::AccountPlan> {
    #[cfg(target_os = "macos")]
    {
        let account = std::env::var("USER").ok().filter(|user| !user.is_empty());
        if let Some(json) =
            claude_keychain_json_with(account.as_deref(), |command| command.output())
        {
            return plan_from_credentials_json(&json);
        }
    }
    let json =
        std::fs::read_to_string(config::get_claude_config_dir().join(".credentials.json")).ok()?;
    plan_from_credentials_json(&json)
}

/// 凭据 JSON 里 Claude Code 记的套餐;没写套餐时为 `None`。
fn plan_from_credentials_json(json: &str) -> Option<crate::services::claude_account::AccountPlan> {
    use crate::services::claude_account::snapshot;

    let serde_json::Value::Object(map) = serde_json::from_str(json).ok()? else {
        return None;
    };
    Some(snapshot::credential_plan(&snapshot::store::Secret(map)))
        .filter(|plan| plan.subscription_type.is_some())
}

/// 从文件读取 Claude 凭据
fn read_claude_credentials_from_file() -> (Option<String>, CredentialStatus, Option<String>) {
    let cred_path = config::get_claude_config_dir().join(".credentials.json");

    if !cred_path.exists() {
        return (None, CredentialStatus::NotFound, None);
    }

    let content = match std::fs::read_to_string(&cred_path) {
        Ok(c) => c,
        Err(e) => {
            return (
                None,
                CredentialStatus::ParseError,
                Some(format!("Failed to read credentials file: {e}")),
            );
        }
    };

    parse_claude_credentials_json(&content)
}

/// 解析 Claude 凭据 JSON(Keychain 和文件共用)
fn parse_claude_credentials_json(
    content: &str,
) -> (Option<String>, CredentialStatus, Option<String>) {
    let parsed: serde_json::Value = match serde_json::from_str(content) {
        Ok(v) => v,
        Err(e) => {
            return (
                None,
                CredentialStatus::ParseError,
                Some(format!("Failed to parse credentials JSON: {e}")),
            );
        }
    };

    // 兼容两种 key 名
    let entry_value = parsed
        .get("claudeAiOauth")
        .or_else(|| parsed.get("claude.ai_oauth"));

    let entry_value = match entry_value {
        Some(v) => v,
        None => {
            return (
                None,
                CredentialStatus::ParseError,
                Some("No OAuth entry found in credentials".to_string()),
            );
        }
    };

    let entry: ClaudeOAuthEntry = match serde_json::from_value(entry_value.clone()) {
        Ok(e) => e,
        Err(e) => {
            return (
                None,
                CredentialStatus::ParseError,
                Some(format!("Failed to parse OAuth entry: {e}")),
            );
        }
    };

    let access_token = match entry.access_token {
        Some(t) if !t.is_empty() => t,
        _ => {
            return (
                None,
                CredentialStatus::ParseError,
                Some("accessToken is empty or missing".to_string()),
            );
        }
    };

    // 检查 token 是否过期
    if let Some(expires_at) = entry.expires_at {
        if is_token_expired(&expires_at) {
            // 访问令牌几小时一换,Claude Code 下次运行时用刷新令牌换新的;
            // 刷新令牌还在就不算登录过期。
            let refreshable = entry.refresh_token.is_some_and(|t| !t.is_empty())
                && !entry
                    .refresh_token_expires_at
                    .as_ref()
                    .is_some_and(is_token_expired);
            return if refreshable {
                (
                    Some(access_token),
                    CredentialStatus::RefreshPending,
                    Some(
                        "Access token has expired; Claude Code refreshes it the next time it runs"
                            .to_string(),
                    ),
                )
            } else {
                (
                    Some(access_token),
                    CredentialStatus::Expired,
                    Some("OAuth token has expired".to_string()),
                )
            };
        }
    }

    (Some(access_token), CredentialStatus::Valid, None)
}

/// 判断 token 是否过期,兼容 Unix 时间戳(秒/毫秒)和 ISO 字符串
fn is_token_expired(expires_at: &serde_json::Value) -> bool {
    let now_secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();

    match expires_at {
        serde_json::Value::Number(n) => {
            if let Some(ts) = n.as_u64() {
                // 区分秒和毫秒(毫秒级时间戳大于 1e12)
                let ts_secs = if ts > 1_000_000_000_000 {
                    ts / 1000
                } else {
                    ts
                };
                ts_secs < now_secs
            } else {
                false
            }
        }
        serde_json::Value::String(s) => {
            // 尝试解析 ISO 8601 格式
            if let Ok(dt) = chrono::DateTime::parse_from_rfc3339(s) {
                (dt.timestamp() as u64) < now_secs
            } else if let Ok(dt) = chrono::NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M:%S%.f")
            {
                (dt.and_utc().timestamp() as u64) < now_secs
            } else {
                false // 无法解析时不视为过期
            }
        }
        _ => false,
    }
}

// ── Claude API 查询 ──────────────────────────────────────

/// Claude OAuth 用量 API 响应中的单个窗口
#[derive(Deserialize)]
struct ApiUsageWindow {
    utilization: Option<f64>,
    resets_at: Option<String>,
}

/// `limits[]` 中的窗口使用 `percent`,而非旧顶层窗口的 `utilization`。
#[derive(Deserialize)]
struct ApiScopedUsageWindow {
    percent: f64,
    resets_at: Option<String>,
}

/// Claude OAuth 用量 API 响应中的超额用量
#[derive(Deserialize)]
struct ApiExtraUsage {
    is_enabled: Option<bool>,
    monthly_limit: Option<f64>,
    used_credits: Option<f64>,
    utilization: Option<f64>,
    currency: Option<String>,
}

/// 已知的 Claude 用量窗口名称;未知的旧格式窗口仍保留原名称。
pub const TIER_FIVE_HOUR: &str = "five_hour";
pub const TIER_SEVEN_DAY: &str = "seven_day";
/// 内部统一名称:Fable 实际由 `limits[].scope.model` 标识。
pub const TIER_SEVEN_DAY_FABLE: &str = "seven_day_fable";
pub const TIER_SEVEN_DAY_OPUS: &str = "seven_day_opus";
pub const TIER_SEVEN_DAY_SONNET: &str = "seven_day_sonnet";

/// Coding Plan(Kimi / MiniMax)的周窗口 tier 名。与 `coding_plan::query_*`
/// 写入、tray 渲染、commands::provider 扁平化三处共用同一标识。
pub const TIER_WEEKLY_LIMIT: &str = "weekly_limit";

/// 月窗口 tier 名。火山方舟 Agent Plan / Coding Plan 有 5h / 周 / 月 三个展示
/// 窗口,月窗口共用此标识;前端 `TIER_I18N_KEYS`
/// 映射到 `subscription.monthly`。
pub const TIER_MONTHLY: &str = "monthly";

/// Kimi 新套餐的月总额度与月编程额度,分别展示,不能合并或当作周额度。
pub const TIER_KIMI_MONTHLY_TOTAL: &str = "kimi_monthly_total";
pub const TIER_KIMI_MONTHLY_CODE: &str = "kimi_monthly_code";

/// Codex 免费方案的 30 天(月)滚动窗口 tier 名。付费方案的次要窗口是 7 天
/// (`seven_day`),免费方案则是 30 天。由 `window_seconds_to_tier_name` 产出、
/// tray 的月分组渲染、前端 `TIER_I18N_KEYS` 映射到 `subscription.thirtyDay`
/// 三处共用同一标识。见 #3651。
pub const TIER_THIRTY_DAY: &str = "30_day";

/// Grok credit 额度窗口的兜底 tier 名。Grok 账单接口只返回一个 credit 用量
/// 窗口,`subscription_grok::tier_name_for_reset` 按重置距离优先映射到
/// `weekly_limit` / `monthly`,两者都不匹配时用此标识;前端 `TIER_I18N_KEYS`
/// 映射到 `subscription.credits`,tray 归入 "c" 分组。
pub const TIER_CREDITS: &str = "credits";

/// Gemini 用量分组名称(按模型而非时间窗口)。`classify_gemini_model` 输出。
pub const TIER_GEMINI_PRO: &str = "gemini_pro";
pub const TIER_GEMINI_FLASH: &str = "gemini_flash";
pub const TIER_GEMINI_FLASH_LITE: &str = "gemini_flash_lite";

const KNOWN_TIERS: &[&str] = &[
    TIER_FIVE_HOUR,
    TIER_SEVEN_DAY,
    TIER_SEVEN_DAY_FABLE,
    TIER_SEVEN_DAY_OPUS,
    TIER_SEVEN_DAY_SONNET,
];

/// 查询 Claude 官方订阅额度
///
/// 瞬时传输失败(网络/超时/读体中断)返回 `Err`(前端 reject → retry + 保留上次
/// 成功值);确定性失败(鉴权/非 2xx/响应体非法 JSON)返回 `Ok(success:false)`。
/// codex/gemini 两个查询函数遵守同一约定。
async fn query_claude_quota(access_token: &str) -> Result<SubscriptionQuota, String> {
    let client = crate::proxy::http_client::get();

    let resp = client
        .get("https://api.anthropic.com/api/oauth/usage")
        .header("Authorization", format!("Bearer {access_token}"))
        .header("anthropic-beta", "oauth-2025-04-20")
        .header("Accept", "application/json")
        .timeout(std::time::Duration::from_secs(15))
        .send()
        .await;

    let resp = match resp {
        Ok(r) => r,
        Err(e) => return Err(format!("Network error: {e}")),
    };

    let status = resp.status();

    if status == reqwest::StatusCode::UNAUTHORIZED || status == reqwest::StatusCode::FORBIDDEN {
        return Ok(SubscriptionQuota::error(
            "claude",
            CredentialStatus::Expired,
            format!("Authentication failed (HTTP {status}). Please re-login with Claude CLI."),
        ));
    }

    if !status.is_success() {
        let body = resp.text().await.unwrap_or_default();
        return Ok(SubscriptionQuota::error(
            "claude",
            CredentialStatus::Valid,
            format!("API error (HTTP {status}): {body}"),
        ));
    }

    // 先 bytes() 再解析:读体失败(超时/连接中断)是瞬时 → Err;拿到完整响应体
    // 后解析失败才是确定性。reqwest 的 json() 把读体错误也包成 decode,无法区分。
    let raw = match resp.bytes().await {
        Ok(b) => b,
        Err(e) => return Err(format!("Failed to read API response: {e}")),
    };
    let body: serde_json::Value = match serde_json::from_slice(&raw) {
        Ok(v) => v,
        Err(e) => {
            return Ok(SubscriptionQuota::error(
                "claude",
                CredentialStatus::Valid,
                format!("Failed to parse API response: {e}"),
            ));
        }
    };

    Ok(parse_claude_quota(&body))
}

/// 兼容旧顶层窗口与新版模型专属周限额,保持查询、缓存和 UI 共用 QuotaTier。
fn parse_claude_quota(body: &serde_json::Value) -> SubscriptionQuota {
    // 解析已知的 tier 窗口
    let mut tiers = Vec::new();
    for &tier_name in KNOWN_TIERS {
        if let Some(window) = body.get(tier_name) {
            if let Ok(w) = serde_json::from_value::<ApiUsageWindow>(window.clone()) {
                if let Some(util) = w.utilization {
                    tiers.push(QuotaTier {
                        name: tier_name.to_string(),
                        utilization: util,
                        resets_at: w.resets_at,
                        used_value_usd: None,
                        max_value_usd: None,
                    });
                }
            }
        }
    }

    // 也解析未知窗口(API 可能返回新的窗口类型)
    if let Some(obj) = body.as_object() {
        for (key, value) in obj {
            if key == "extra_usage" || key == "limits" || KNOWN_TIERS.contains(&key.as_str()) {
                continue;
            }
            if let Ok(w) = serde_json::from_value::<ApiUsageWindow>(value.clone()) {
                if let Some(util) = w.utilization {
                    tiers.push(QuotaTier {
                        name: key.clone(),
                        utilization: util,
                        resets_at: w.resets_at,
                        used_value_usd: None,
                        max_value_usd: None,
                    });
                }
            }
        }
    }

    // 新版模型专属额度覆盖同名旧窗口。逐条解析,单个异常项目不影响其余额度。
    let mut scoped_tiers = HashSet::new();
    if let Some(limits) = body.get("limits").and_then(serde_json::Value::as_array) {
        for limit in limits {
            if limit.get("kind").and_then(serde_json::Value::as_str) != Some("weekly_scoped")
                || limit.get("group").and_then(serde_json::Value::as_str) != Some("weekly")
                // 不把特定使用场景的子限额合并进整个模型的周限额。
                || limit.pointer("/scope/surface").is_some_and(|v| !v.is_null())
            {
                continue;
            }
            let Some(model) = limit
                .pointer("/scope/model/display_name")
                .and_then(serde_json::Value::as_str)
            else {
                continue;
            };
            let tier_name = match model.trim().to_ascii_lowercase().as_str() {
                "fable" => TIER_SEVEN_DAY_FABLE,
                "opus" => TIER_SEVEN_DAY_OPUS,
                "sonnet" => TIER_SEVEN_DAY_SONNET,
                _ => continue,
            };
            let Ok(window) = serde_json::from_value::<ApiScopedUsageWindow>(limit.clone()) else {
                continue;
            };
            if !window.percent.is_finite()
                || window.percent < 0.0
                || !scoped_tiers.insert(tier_name)
            {
                continue;
            }
            // 与 Claude Code 一致:不按 is_active 过滤。0% / resets_at:null
            // 也可能是有效的模型额度;不存在的额度由接口省略。
            let tier = QuotaTier {
                name: tier_name.to_string(),
                utilization: window.percent,
                resets_at: window.resets_at,
                used_value_usd: None,
                max_value_usd: None,
            };
            if let Some(existing) = tiers.iter_mut().find(|t| t.name == tier_name) {
                *existing = tier;
            } else {
                tiers.push(tier);
            }
        }
    }
    tiers.sort_by_key(|tier| {
        KNOWN_TIERS
            .iter()
            .position(|&name| name == tier.name)
            .unwrap_or(KNOWN_TIERS.len())
    });

    // 解析超额使用
    let extra_usage = body.get("extra_usage").and_then(|v| {
        serde_json::from_value::<ApiExtraUsage>(v.clone())
            .ok()
            .map(|e| ExtraUsage {
                is_enabled: e.is_enabled.unwrap_or(false),
                monthly_limit: e.monthly_limit,
                used_credits: e.used_credits,
                utilization: e.utilization,
                currency: e.currency,
            })
    });

    SubscriptionQuota {
        tool: "claude".to_string(),
        credential_status: CredentialStatus::Valid,
        credential_message: None,
        success: true,
        tiers,
        extra_usage,
        reset_credits: None,
        credits_balance: None,
        plan_name: None,
        error: None,
        queried_at: Some(now_millis()),
    }
}

// ── Codex 凭据读取 ──────────────────────────────────────

#[derive(Deserialize)]
struct CodexAuthJson {
    auth_mode: Option<String>,
    tokens: Option<CodexTokens>,
    last_refresh: Option<String>,
}

#[derive(Deserialize)]
struct CodexTokens {
    access_token: Option<String>,
    account_id: Option<String>,
}

/// (access_token, account_id, status, message)
pub(crate) type CodexCredentials = (
    Option<String>,
    Option<String>,
    CredentialStatus,
    Option<String>,
);

/// 读取 Codex OAuth 凭据
///
/// 按优先级尝试以下来源:
/// 1. macOS Keychain (service: "Codex Auth",账户按 Codex 配置目录区分)
/// 2. 凭据文件 ~/.codex/auth.json
///
/// 仅 auth_mode == "chatgpt" (OAuth) 时有效,API key 模式不支持用量查询。
fn read_codex_credentials() -> CodexCredentials {
    #[cfg(target_os = "macos")]
    {
        if let Some(result) = read_codex_credentials_from_keychain() {
            return result;
        }
    }

    read_codex_credentials_from_file()
}

/// 从 macOS Keychain 读取 Codex 凭据
#[cfg(target_os = "macos")]
fn read_codex_credentials_from_keychain() -> Option<CodexCredentials> {
    read_codex_keychain_secret()
        .ok()
        .flatten()
        .map(|json_str| parse_codex_credentials_json(&json_str))
}

/// Codex 在 Keychain 里存登录用的账户名:`cli|` 加规范化后的 Codex 配置目录路径的
/// SHA-256 十六进制前 16 位(codex-rs `login/src/auth/storage.rs` 的 `compute_store_key`)。
/// 规范化失败时用原路径,和上游一致。
#[cfg(target_os = "macos")]
fn codex_keychain_account(codex_home: &std::path::Path) -> String {
    let canonical = codex_home
        .canonicalize()
        .unwrap_or_else(|_| codex_home.to_path_buf());
    let hex = crate::live::engine::sha256_hex(canonical.to_string_lossy().as_bytes());
    format!("cli|{}", &hex[..16])
}

/// `security` 找不到条目时的退出码(errSecItemNotFound)。
#[cfg(target_os = "macos")]
const SECURITY_ITEM_NOT_FOUND: i32 = 44;

/// Keychain 里当前 Codex 配置目录的登录 JSON。服务名所有配置目录共用,必须带上账户名:
/// 只按服务名查,本机有别的配置目录的登录时 `security` 返回第一条匹配的。
///
/// `Ok(None)`:确定没有这一条。`Err`:读不出来(访问被拒、`security` 跑不起来),
/// 不知道里面有什么。
#[cfg(target_os = "macos")]
fn read_codex_keychain_secret() -> Result<Option<String>, String> {
    let account = codex_keychain_account(&crate::codex_config::get_codex_config_dir());
    let output = std::process::Command::new("security")
        .args([
            "find-generic-password",
            "-s",
            "Codex Auth",
            "-a",
            &account,
            "-w",
        ])
        .output()
        .map_err(|error| format!("运行 security 失败: {error}"))?;
    if output.status.code() == Some(SECURITY_ITEM_NOT_FOUND) {
        return Ok(None);
    }
    if !output.status.success() {
        return Err(format!(
            "security 退出码 {:?}: {}",
            output.status.code(),
            String::from_utf8_lossy(&output.stderr).trim()
        ));
    }
    let secret = String::from_utf8_lossy(&output.stdout);
    let secret = secret.trim();
    Ok((!secret.is_empty()).then(|| secret.to_string()))
}

/// 从文件读取 Codex 凭据
fn read_codex_credentials_from_file() -> CodexCredentials {
    let auth_path = crate::codex_config::get_codex_auth_path();

    if !auth_path.exists() {
        return (None, None, CredentialStatus::NotFound, None);
    }

    let content = match std::fs::read_to_string(&auth_path) {
        Ok(c) => c,
        Err(e) => {
            return (
                None,
                None,
                CredentialStatus::ParseError,
                Some(format!("Failed to read Codex auth file: {e}")),
            );
        }
    };

    parse_codex_credentials_json(&content)
}

/// 系统钥匙串里 Codex 的登录(`cli_auth_credentials_store` 为 keyring / auto 时 Codex 存在
/// 这里)。
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum CodexKeychainLogin {
    // 只有 macOS 读得到钥匙串,其他平台的正式构建里只会出现 Unknown。
    #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
    Found(serde_json::Value),
    /// 确定没有,或者内容不是 JSON(Codex 自己也读不了,auto 模式同样退回 `auth.json`)。
    #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
    Missing,
    /// 读不出来:不是 macOS(Windows、Linux 的凭据库 CC Switch 读不了),或者 macOS 上
    /// 访问被拒。Codex 看到的可能是另一个登录,不能拿 `auth.json` 顶替。
    Unknown,
}

pub(crate) fn read_codex_keychain_login() -> CodexKeychainLogin {
    #[cfg(target_os = "macos")]
    {
        match read_codex_keychain_secret() {
            Ok(Some(secret)) => serde_json::from_str(&secret)
                .map_or(CodexKeychainLogin::Missing, CodexKeychainLogin::Found),
            Ok(None) => CodexKeychainLogin::Missing,
            Err(error) => {
                log::warn!("读取 Keychain 里的 Codex 登录失败: {error}");
                CodexKeychainLogin::Unknown
            }
        }
    }
    #[cfg(not(target_os = "macos"))]
    {
        CodexKeychainLogin::Unknown
    }
}

/// 解析 Codex 凭据 JSON(Keychain 和文件共用)
pub(crate) fn parse_codex_credentials_json(content: &str) -> CodexCredentials {
    let auth: CodexAuthJson = match serde_json::from_str(content) {
        Ok(a) => a,
        Err(e) => {
            return (
                None,
                None,
                CredentialStatus::ParseError,
                Some(format!("Failed to parse Codex auth JSON: {e}")),
            );
        }
    };

    // 仅 OAuth 模式有用量数据
    if auth.auth_mode.as_deref() != Some("chatgpt") {
        return (
            None,
            None,
            CredentialStatus::NotFound,
            Some("Codex not using OAuth mode".to_string()),
        );
    }

    let tokens = match auth.tokens {
        Some(t) => t,
        None => {
            return (
                None,
                None,
                CredentialStatus::ParseError,
                Some("No tokens in Codex auth".to_string()),
            );
        }
    };

    let access_token = match tokens.access_token {
        Some(t) if !t.is_empty() => t,
        _ => {
            return (
                None,
                None,
                CredentialStatus::ParseError,
                Some("access_token is empty or missing".to_string()),
            );
        }
    };

    // 检查 token 是否可能过期(距上次刷新 > 8 天)
    if let Some(ref last_refresh) = auth.last_refresh {
        if is_codex_token_stale(last_refresh) {
            return (
                Some(access_token),
                tokens.account_id,
                CredentialStatus::Expired,
                Some("Codex token may be stale (>8 days since last refresh)".to_string()),
            );
        }
    }

    (
        Some(access_token),
        tokens.account_id,
        CredentialStatus::Valid,
        None,
    )
}

/// 判断 Codex token 是否可能过期(Codex CLI 在 >8 天时自动刷新)
fn is_codex_token_stale(last_refresh: &str) -> bool {
    let now_secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();

    if let Ok(dt) = chrono::DateTime::parse_from_rfc3339(last_refresh) {
        let age_secs = now_secs.saturating_sub(dt.timestamp() as u64);
        age_secs > 8 * 24 * 3600
    } else {
        false
    }
}

// ── Codex API 查询 ──────────────────────────────────────

#[derive(Deserialize)]
struct CodexRateLimitWindow {
    used_percent: Option<f64>,
    limit_window_seconds: Option<i64>,
    reset_at: Option<i64>,
}

#[derive(Deserialize)]
struct CodexRateLimit {
    primary_window: Option<CodexRateLimitWindow>,
    secondary_window: Option<CodexRateLimitWindow>,
}

#[derive(Deserialize)]
struct CodexUsageResponse {
    rate_limit: Option<CodexRateLimit>,
    /// 原样收下再挑字段:形状不对时只是不显示余额,不能让整份额度解析失败
    credits: Option<serde_json::Value>,
    #[serde(default)]
    plan_type: Option<String>,
}

/// `wham/usage` 的 `plan_type` → 套餐名,照 Codex 自己 `/status` 的叫法
/// (codex-rs/tui/src/subscription.rs:`business` 在那里也叫 Enterprise)。认不出的首字母大写照写
fn codex_plan_label(plan_type: &str) -> Option<String> {
    let plan = plan_type.trim().to_ascii_lowercase();
    let label = match plan.as_str() {
        "" | "unknown" => return None,
        "free" => "Free",
        "go" => "Go",
        "plus" => "Plus",
        "prolite" => "Pro 100",
        "pro" => "Pro 200",
        "promax" => "Pro 500",
        "team" | "self_serve_business_usage_based" => "Business",
        "self_serve_business_prolite" => "Business Premium",
        "edu" => "Edu",
        "edu_plus" => "Edu Plus",
        "edu_pro" => "Edu Pro",
        p if p == "business" || p == "ent26" || p.starts_with("enterprise") => "Enterprise",
        _ => return Some(title_case_words(&plan)),
    };
    Some(label.to_string())
}

/// `free_workspace` → `Free Workspace`
pub(crate) fn title_case_words(raw: &str) -> String {
    raw.split(['_', '-', ' '])
        .filter(|word| !word.is_empty())
        .map(|word| {
            let mut chars = word.chars();
            match chars.next() {
                Some(first) => first.to_uppercase().chain(chars).collect::<String>(),
                None => String::new(),
            }
        })
        .collect::<Vec<_>>()
        .join(" ")
}

/// `wham/usage` 的 `credits`:`{has_credits, unlimited, balance}`,balance 实测是字符串
/// ("62500"),也收数字(同 CodexBar)。不限量、没有或为 0 时不显示
fn parse_codex_credits_balance(credits: &serde_json::Value) -> Option<f64> {
    if credits.get("has_credits").and_then(|v| v.as_bool()) != Some(true)
        || credits.get("unlimited").and_then(|v| v.as_bool()) == Some(true)
    {
        return None;
    }
    let balance = match credits.get("balance")? {
        serde_json::Value::Number(n) => n.as_f64()?,
        serde_json::Value::String(s) => s.trim().parse::<f64>().ok()?,
        _ => return None,
    };
    (balance.is_finite() && balance > 0.0).then_some(balance)
}

/// 根据窗口秒数映射到 tier 名称(与 Claude 的命名兼容以复用前端 i18n)
fn window_seconds_to_tier_name(secs: i64) -> String {
    match secs {
        18000 => TIER_FIVE_HOUR.to_string(),
        604800 => TIER_SEVEN_DAY.to_string(),
        // Codex 免费方案的 30 天窗口。显式映射到常量,与 tray 月分组、前端
        // TIER_I18N_KEYS 保持同一标识(否则动态回退虽也得到 "30_day",但字符串
        // 分散在多处、易和托盘/前端白名单脱节)。见 #3651。
        2_592_000 => TIER_THIRTY_DAY.to_string(),
        s => {
            let hours = s / 3600;
            if hours >= 24 {
                format!("{}_day", hours / 24)
            } else {
                format!("{}_hour", hours)
            }
        }
    }
}

/// Unix 时间戳(秒)转 ISO 8601 字符串
fn unix_ts_to_iso(ts: i64) -> Option<String> {
    chrono::DateTime::from_timestamp(ts, 0).map(|dt| dt.to_rfc3339())
}

#[derive(Deserialize)]
struct CodexResetCreditsResponse {
    #[serde(default)]
    credits: Vec<CodexResetCreditEntry>,
}

#[derive(Deserialize)]
struct CodexResetCreditEntry {
    status: Option<String>,
    expires_at: Option<String>,
}

/// 解析 `wham/rate-limit-reset-credits`:不信 `available_count`,自己按
/// status == "available" 且没过期来数(同 CodexBar),先到期的排前面
fn parse_codex_reset_credits(
    raw: &[u8],
    now: chrono::DateTime<chrono::Utc>,
) -> Option<ResetCredits> {
    let body: CodexResetCreditsResponse = serde_json::from_slice(raw).ok()?;
    let mut expiries: Vec<Option<chrono::DateTime<chrono::Utc>>> = body
        .credits
        .into_iter()
        .filter(|credit| credit.status.as_deref() == Some("available"))
        .filter_map(|credit| match credit.expires_at {
            None => Some(None),
            // 认不出的到期时间当作不过期,宁可多显示一次也不吞掉
            Some(raw) => match chrono::DateTime::parse_from_rfc3339(&raw) {
                Ok(at) => {
                    let at = at.with_timezone(&chrono::Utc);
                    (at > now).then_some(Some(at))
                }
                Err(_) => Some(None),
            },
        })
        .collect();
    // None(不过期)排最后
    expiries.sort_by_key(|at| (at.is_none(), *at));
    Some(ResetCredits {
        expires_at: expiries
            .into_iter()
            .map(|at| at.map(|at| at.to_rfc3339()))
            .collect(),
    })
}

/// 查存下的限额重置次数。附带查询:任何失败都只返回 None,不连累额度本身
async fn query_codex_reset_credits(
    access_token: &str,
    account_id: Option<&str>,
) -> Option<ResetCredits> {
    let mut req = crate::proxy::http_client::get()
        .get("https://chatgpt.com/backend-api/wham/rate-limit-reset-credits")
        .header("Authorization", format!("Bearer {access_token}"))
        .header("User-Agent", "codex-cli")
        .header("Accept", "application/json")
        .header("OpenAI-Beta", "codex-1");
    if let Some(id) = account_id {
        req = req.header("ChatGPT-Account-Id", id);
    }
    let resp = req
        .timeout(std::time::Duration::from_secs(8))
        .send()
        .await
        .ok()?;
    if !resp.status().is_success() {
        log::debug!("Codex reset credits query failed: HTTP {}", resp.status());
        return None;
    }
    let raw = resp.bytes().await.ok()?;
    parse_codex_reset_credits(&raw, chrono::Utc::now())
}

/// 查询 Codex / ChatGPT 反代订阅额度(连同存下的重置次数,两个请求并行)
///
/// 参数化 `tool_label` 和 `expired_message` 让该函数可被两个调用点共用:
/// - `"codex"` + "Please re-login with Codex CLI."(CLI 凭据路径)
/// - `"codex_oauth"` + "Please re-login via cc-switch."(cc-switch 自管 OAuth 路径)
pub(crate) async fn query_codex_quota(
    access_token: &str,
    account_id: Option<&str>,
    tool_label: &str,
    expired_message: &str,
) -> Result<SubscriptionQuota, String> {
    let (quota, reset_credits) = tokio::join!(
        query_codex_usage(access_token, account_id, tool_label, expired_message),
        query_codex_reset_credits(access_token, account_id),
    );
    let mut quota = quota?;
    if quota.success {
        quota.reset_credits = reset_credits;
    }
    Ok(quota)
}

pub(crate) async fn query_codex_usage(
    access_token: &str,
    account_id: Option<&str>,
    tool_label: &str,
    expired_message: &str,
) -> Result<SubscriptionQuota, String> {
    let client = crate::proxy::http_client::get();

    let mut req = client
        .get("https://chatgpt.com/backend-api/wham/usage")
        .header("Authorization", format!("Bearer {access_token}"))
        .header("User-Agent", "codex-cli")
        .header("Accept", "application/json");

    if let Some(id) = account_id {
        req = req.header("ChatGPT-Account-Id", id);
    }

    let resp = match req.timeout(std::time::Duration::from_secs(15)).send().await {
        Ok(r) => r,
        Err(e) => return Err(format!("Network error: {e}")),
    };

    let status = resp.status();

    if status == reqwest::StatusCode::UNAUTHORIZED || status == reqwest::StatusCode::FORBIDDEN {
        return Ok(SubscriptionQuota::error(
            tool_label,
            CredentialStatus::Expired,
            format!("{expired_message} (HTTP {status})"),
        ));
    }

    if !status.is_success() {
        let body = resp.text().await.unwrap_or_default();
        return Ok(SubscriptionQuota::error(
            tool_label,
            CredentialStatus::Valid,
            format!("API error (HTTP {status}): {body}"),
        ));
    }

    let raw = match resp.bytes().await {
        Ok(b) => b,
        Err(e) => return Err(format!("Failed to read API response: {e}")),
    };
    let body: CodexUsageResponse = match serde_json::from_slice(&raw) {
        Ok(v) => v,
        Err(e) => {
            return Ok(SubscriptionQuota::error(
                tool_label,
                CredentialStatus::Valid,
                format!("Failed to parse API response: {e}"),
            ));
        }
    };

    let mut tiers = Vec::new();

    if let Some(rate_limit) = body.rate_limit {
        for window in [rate_limit.primary_window, rate_limit.secondary_window]
            .into_iter()
            .flatten()
        {
            if let Some(used) = window.used_percent {
                tiers.push(QuotaTier {
                    name: window
                        .limit_window_seconds
                        .map(window_seconds_to_tier_name)
                        .unwrap_or_else(|| "unknown".to_string()),
                    utilization: used,
                    // 已用额度为 0 时,上游可能返回当前时间加窗口时长;
                    // 隐藏该占位时间,避免产生虚假的倒计时。
                    // 使用原始用量,保留非零用量取整后仍剩余 100% 的倒计时。
                    resets_at: if used == 0.0 {
                        None
                    } else {
                        window.reset_at.and_then(unix_ts_to_iso)
                    },
                    used_value_usd: None,
                    max_value_usd: None,
                });
            }
        }
    }

    Ok(SubscriptionQuota {
        tool: tool_label.to_string(),
        credential_status: CredentialStatus::Valid,
        credential_message: None,
        success: true,
        tiers,
        extra_usage: None,
        reset_credits: None,
        credits_balance: body.credits.as_ref().and_then(parse_codex_credits_balance),
        plan_name: body.plan_type.as_deref().and_then(codex_plan_label),
        error: None,
        queried_at: Some(now_millis()),
    })
}

// ── Gemini 凭据读取 ──────────────────────────────────────

/// Gemini OAuth 凭据文件格式(~/.gemini/oauth_creds.json)
#[derive(Deserialize)]
struct GeminiOAuthCredsFile {
    access_token: Option<String>,
    refresh_token: Option<String>,
    expiry_date: Option<i64>, // 毫秒时间戳
}

/// (access_token, refresh_token, status, message)
type GeminiCredentials = (
    Option<String>,
    Option<String>,
    CredentialStatus,
    Option<String>,
);

/// 读取 Gemini OAuth 凭据
///
/// 按优先级尝试以下来源:
/// 1. macOS Keychain (service: "gemini-cli-oauth", account: "main-account")
/// 2. 凭据文件 ~/.gemini/oauth_creds.json(遗留格式)
///
/// 仅 OAuth 认证模式(`oauth-personal`)有效;API key 模式无法查询官方用量。
fn read_gemini_credentials() -> GeminiCredentials {
    #[cfg(target_os = "macos")]
    {
        if let Some(result) = read_gemini_credentials_from_keychain() {
            return result;
        }
    }

    read_gemini_credentials_from_file()
}

/// 从 macOS Keychain 读取 Gemini 凭据
#[cfg(target_os = "macos")]
fn read_gemini_credentials_from_keychain() -> Option<GeminiCredentials> {
    let output = std::process::Command::new("security")
        .args([
            "find-generic-password",
            "-s",
            "gemini-cli-oauth",
            "-a",
            "main-account",
            "-w",
        ])
        .output()
        .ok()?;

    if !output.status.success() {
        return None;
    }

    let json_str = String::from_utf8(output.stdout).ok()?;
    let json_str = json_str.trim();
    if json_str.is_empty() {
        return None;
    }

    Some(parse_gemini_keychain_json(json_str))
}

/// 解析 Keychain 格式的 Gemini 凭据
///
/// Keychain 格式(keytar):
/// ```json
/// { "token": { "accessToken": "...", "refreshToken": "...", "expiresAt": 1234 }, "updatedAt": ... }
/// ```
#[cfg(target_os = "macos")]
fn parse_gemini_keychain_json(content: &str) -> GeminiCredentials {
    let parsed: serde_json::Value = match serde_json::from_str(content) {
        Ok(v) => v,
        Err(e) => {
            return (
                None,
                None,
                CredentialStatus::ParseError,
                Some(format!("Failed to parse Gemini keychain JSON: {e}")),
            )
        }
    };

    let token = match parsed.get("token") {
        Some(t) => t,
        None => {
            // Keychain 中可能是扁平格式,尝试文件格式解析
            return parse_gemini_file_json(content);
        }
    };

    let access_token = token
        .get("accessToken")
        .and_then(|v| v.as_str())
        .map(String::from);
    let refresh_token = token
        .get("refreshToken")
        .and_then(|v| v.as_str())
        .map(String::from);
    let expires_at = token.get("expiresAt").and_then(|v| v.as_i64());

    match access_token {
        Some(at) if !at.is_empty() => {
            // expiresAt 是毫秒时间戳
            if let Some(exp_ms) = expires_at {
                if exp_ms < now_millis() {
                    return (
                        Some(at),
                        refresh_token,
                        CredentialStatus::Expired,
                        Some("Gemini access token has expired".to_string()),
                    );
                }
            }
            (Some(at), refresh_token, CredentialStatus::Valid, None)
        }
        _ => (
            None,
            refresh_token,
            CredentialStatus::ParseError,
            Some("accessToken is empty or missing".to_string()),
        ),
    }
}

/// 从文件读取 Gemini 凭据
fn read_gemini_credentials_from_file() -> GeminiCredentials {
    let cred_path = crate::gemini_config::get_gemini_dir().join("oauth_creds.json");
    if !cred_path.exists() {
        return (None, None, CredentialStatus::NotFound, None);
    }

    let content = match std::fs::read_to_string(&cred_path) {
        Ok(c) => c,
        Err(e) => {
            return (
                None,
                None,
                CredentialStatus::ParseError,
                Some(format!("Failed to read Gemini credentials: {e}")),
            )
        }
    };

    parse_gemini_file_json(&content)
}

/// 解析文件格式的 Gemini 凭据
///
/// 文件格式(oauth_creds.json):
/// ```json
/// { "access_token": "...", "refresh_token": "...", "expiry_date": 1234 }
/// ```
fn parse_gemini_file_json(content: &str) -> GeminiCredentials {
    let creds: GeminiOAuthCredsFile = match serde_json::from_str(content) {
        Ok(c) => c,
        Err(e) => {
            return (
                None,
                None,
                CredentialStatus::ParseError,
                Some(format!("Failed to parse Gemini credentials: {e}")),
            )
        }
    };

    let access_token = match creds.access_token {
        Some(t) if !t.is_empty() => t,
        _ => {
            return (
                None,
                creds.refresh_token,
                CredentialStatus::ParseError,
                Some("access_token is empty or missing".to_string()),
            )
        }
    };

    // expiry_date 是毫秒时间戳
    if let Some(exp_ms) = creds.expiry_date {
        if exp_ms < now_millis() {
            return (
                Some(access_token),
                creds.refresh_token,
                CredentialStatus::Expired,
                Some("Gemini access token has expired".to_string()),
            );
        }
    }

    (
        Some(access_token),
        creds.refresh_token,
        CredentialStatus::Valid,
        None,
    )
}

// ── Gemini Token 刷新 ──────────────────────────────────────

/// Gemini OAuth Client 凭据(公开值,来自 Gemini CLI 源码 google-gemini/gemini-cli)
const GEMINI_OAUTH_CLIENT_ID: &str =
    "681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com";
const GEMINI_OAUTH_CLIENT_SECRET: &str = "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";

/// 使用 refresh_token 刷新 Gemini access token
///
/// Google OAuth access_token 仅有 ~1h 有效期,需要定期用 refresh_token 刷新。
/// refresh_token 本身不过期(除非用户撤销授权)。
async fn refresh_gemini_token(refresh_token: &str) -> Option<String> {
    let client = crate::proxy::http_client::get();

    let resp = client
        .post("https://oauth2.googleapis.com/token")
        .form(&[
            ("client_id", GEMINI_OAUTH_CLIENT_ID),
            ("client_secret", GEMINI_OAUTH_CLIENT_SECRET),
            ("refresh_token", refresh_token),
            ("grant_type", "refresh_token"),
        ])
        .timeout(std::time::Duration::from_secs(15))
        .send()
        .await
        .ok()?;

    if !resp.status().is_success() {
        return None;
    }

    let body: serde_json::Value = resp.json().await.ok()?;
    body.get("access_token")?.as_str().map(String::from)
}

// ── Gemini API 查询 ──────────────────────────────────────

/// loadCodeAssist 响应
#[derive(Deserialize)]
struct GeminiLoadCodeAssistResponse {
    #[serde(rename = "cloudaicompanionProject")]
    cloudaicompanion_project: Option<serde_json::Value>,
}

/// 配额 bucket
#[derive(Deserialize)]
struct GeminiBucketInfo {
    #[serde(rename = "remainingFraction")]
    remaining_fraction: Option<f64>,
    #[serde(rename = "resetTime")]
    reset_time: Option<String>,
    #[serde(rename = "modelId")]
    model_id: Option<String>,
}

/// retrieveUserQuota 响应
#[derive(Deserialize)]
struct GeminiQuotaResponse {
    buckets: Option<Vec<GeminiBucketInfo>>,
}

/// 从 loadCodeAssist 响应中提取项目 ID
fn extract_project_id(value: &serde_json::Value) -> Option<String> {
    match value {
        serde_json::Value::String(s) => Some(s.clone()),
        serde_json::Value::Object(obj) => obj
            .get("id")
            .or_else(|| obj.get("projectId"))
            .and_then(|v| v.as_str())
            .map(String::from),
        _ => None,
    }
}

/// 将 Gemini 模型 ID 分类为 Pro / Flash / Flash Lite
fn classify_gemini_model(model_id: &str) -> &str {
    if model_id.contains("flash-lite") {
        TIER_GEMINI_FLASH_LITE
    } else if model_id.contains("flash") {
        TIER_GEMINI_FLASH
    } else if model_id.contains("pro") {
        TIER_GEMINI_PRO
    } else {
        model_id
    }
}

/// 查询 Gemini 官方订阅额度
///
/// 两步 API 调用:
/// 1. loadCodeAssist → 获取 cloudaicompanionProject
/// 2. retrieveUserQuota → 获取按模型分桶的配额数据
async fn query_gemini_quota(access_token: &str) -> Result<SubscriptionQuota, String> {
    let client = crate::proxy::http_client::get();

    // ── Step 1: loadCodeAssist 获取项目 ID ──
    let load_resp = client
        .post("https://cloudcode-pa.googleapis.com/v1internal:loadCodeAssist")
        .header("Authorization", format!("Bearer {access_token}"))
        .header("Content-Type", "application/json")
        .json(&serde_json::json!({
            "metadata": {
                "ideType": "GEMINI_CLI",
                "pluginType": "GEMINI"
            }
        }))
        .timeout(std::time::Duration::from_secs(15))
        .send()
        .await;

    let load_resp = match load_resp {
        Ok(r) => r,
        Err(e) => return Err(format!("Network error (loadCodeAssist): {e}")),
    };

    let load_status = load_resp.status();
    if load_status == reqwest::StatusCode::UNAUTHORIZED
        || load_status == reqwest::StatusCode::FORBIDDEN
    {
        return Ok(SubscriptionQuota::error(
            "gemini",
            CredentialStatus::Expired,
            format!("Authentication failed (HTTP {load_status}). Please re-login with Gemini CLI."),
        ));
    }
    if !load_status.is_success() {
        let body = load_resp.text().await.unwrap_or_default();
        return Ok(SubscriptionQuota::error(
            "gemini",
            CredentialStatus::Valid,
            format!("loadCodeAssist failed (HTTP {load_status}): {body}"),
        ));
    }

    let load_raw = match load_resp.bytes().await {
        Ok(b) => b,
        Err(e) => return Err(format!("Failed to read loadCodeAssist response: {e}")),
    };
    let load_body: GeminiLoadCodeAssistResponse = match serde_json::from_slice(&load_raw) {
        Ok(v) => v,
        Err(e) => {
            return Ok(SubscriptionQuota::error(
                "gemini",
                CredentialStatus::Valid,
                format!("Failed to parse loadCodeAssist response: {e}"),
            ));
        }
    };

    let project_id = load_body
        .cloudaicompanion_project
        .as_ref()
        .and_then(extract_project_id);

    // ── Step 2: retrieveUserQuota 获取配额 ──
    let mut quota_body = serde_json::json!({});
    if let Some(ref pid) = project_id {
        quota_body["project"] = serde_json::Value::String(pid.clone());
    }

    let quota_resp = client
        .post("https://cloudcode-pa.googleapis.com/v1internal:retrieveUserQuota")
        .header("Authorization", format!("Bearer {access_token}"))
        .header("Content-Type", "application/json")
        .json(&quota_body)
        .timeout(std::time::Duration::from_secs(15))
        .send()
        .await;

    let quota_resp = match quota_resp {
        Ok(r) => r,
        Err(e) => return Err(format!("Network error (retrieveUserQuota): {e}")),
    };

    let quota_status = quota_resp.status();
    if quota_status == reqwest::StatusCode::UNAUTHORIZED
        || quota_status == reqwest::StatusCode::FORBIDDEN
    {
        return Ok(SubscriptionQuota::error(
            "gemini",
            CredentialStatus::Expired,
            format!("Authentication failed (HTTP {quota_status})."),
        ));
    }
    if !quota_status.is_success() {
        let body = quota_resp.text().await.unwrap_or_default();
        return Ok(SubscriptionQuota::error(
            "gemini",
            CredentialStatus::Valid,
            format!("retrieveUserQuota failed (HTTP {quota_status}): {body}"),
        ));
    }

    let quota_raw = match quota_resp.bytes().await {
        Ok(b) => b,
        Err(e) => return Err(format!("Failed to read quota response: {e}")),
    };
    let quota_data: GeminiQuotaResponse = match serde_json::from_slice(&quota_raw) {
        Ok(v) => v,
        Err(e) => {
            return Ok(SubscriptionQuota::error(
                "gemini",
                CredentialStatus::Valid,
                format!("Failed to parse quota response: {e}"),
            ));
        }
    };

    // ── 按模型分类汇总,每类取最低 remainingFraction ──
    let mut category_map: HashMap<String, (f64, Option<String>)> = HashMap::new();

    if let Some(buckets) = quota_data.buckets {
        for bucket in buckets {
            let model_id = bucket.model_id.as_deref().unwrap_or("unknown");
            let category = classify_gemini_model(model_id).to_string();
            let remaining = bucket.remaining_fraction.unwrap_or(1.0).clamp(0.0, 1.0);

            let entry = category_map
                .entry(category)
                .or_insert((remaining, bucket.reset_time.clone()));
            if remaining < entry.0 {
                entry.0 = remaining;
                if bucket.reset_time.is_some() {
                    entry.1.clone_from(&bucket.reset_time);
                }
            }
        }
    }

    // 转换为 tiers(remainingFraction → utilization: 已用百分比)
    let sort_order = |name: &str| -> usize {
        match name {
            TIER_GEMINI_PRO => 0,
            TIER_GEMINI_FLASH => 1,
            TIER_GEMINI_FLASH_LITE => 2,
            _ => 3,
        }
    };

    let mut tiers: Vec<QuotaTier> = category_map
        .into_iter()
        .map(|(name, (remaining, reset_time))| QuotaTier {
            name,
            utilization: (1.0 - remaining) * 100.0,
            resets_at: reset_time,
            used_value_usd: None,
            max_value_usd: None,
        })
        .collect();

    tiers.sort_by_key(|t| sort_order(&t.name));

    Ok(SubscriptionQuota {
        tool: "gemini".to_string(),
        credential_status: CredentialStatus::Valid,
        credential_message: None,
        success: true,
        tiers,
        extra_usage: None,
        reset_credits: None,
        credits_balance: None,
        plan_name: crate::services::code_assist_plan::code_assist_plan(&load_raw),
        error: None,
        queried_at: Some(now_millis()),
    })
}

// ── 入口函数 ──────────────────────────────────────────────

/// 按读到的 Claude 凭据查额度:访问令牌过期也先试一次(可能实际上还有效)。
async fn claude_quota_from(
    (token, status, message): (Option<String>, CredentialStatus, Option<String>),
) -> Result<SubscriptionQuota, String> {
    match status {
        CredentialStatus::NotFound => Ok(SubscriptionQuota::not_found("claude")),
        CredentialStatus::ParseError => Ok(SubscriptionQuota::error(
            "claude",
            CredentialStatus::ParseError,
            message.unwrap_or_else(|| "Failed to parse credentials".to_string()),
        )),
        CredentialStatus::Expired | CredentialStatus::RefreshPending => {
            if let Some(token) = token {
                let result = query_claude_quota(&token).await?;
                if result.success {
                    return Ok(result);
                }
            }
            Ok(SubscriptionQuota::error(
                "claude",
                status,
                message.unwrap_or_else(|| "OAuth token has expired".to_string()),
            ))
        }
        CredentialStatus::Valid => {
            let token = token.expect("token must be Some when status is Valid");
            query_claude_quota(&token).await
        }
    }
}

/// 绑了账号的 Claude 官方卡:按那个账号自己的登录查额度(在用的号读默认槽位,没在用的读它
/// 的账号目录)。令牌有效就直接查;过期了(没在用的号放几个小时就会这样)在那个目录里跑一次
/// `claude -p /usage`,由 Claude Code 自己刷新并缓存额度,再读它的缓存。CC Switch 不刷新令牌。
pub async fn get_claude_account_quota(
    data_dir: std::path::PathBuf,
    account: String,
) -> Result<ClaudeAccountQuota, String> {
    use crate::services::claude_account::{note_plan, snapshot, usage};

    let data_dir_for_expiry = data_dir.clone();
    let found = {
        let account = account.clone();
        tokio::task::spawn_blocking(move || {
            let slot = snapshot::account_slot(&data_dir, &account)?;
            let secret = snapshot::store::platform_store().read(&slot);
            // 套餐顺手记进账号:授权中心和没开额度的卡片也能显示
            let plan = secret
                .as_ref()
                .ok()
                .and_then(Option::as_ref)
                .map(snapshot::credential_plan);
            if let Some(plan) = &plan {
                note_plan(&data_dir, &account, plan);
            }
            Some((slot, secret, plan))
        })
        .await
        .map_err(|error| error.to_string())?
    };
    let Some((slot, secret, plan)) = found else {
        return Ok(ClaudeAccountQuota::without_plan(
            SubscriptionQuota::not_found("claude"),
        ));
    };
    let Some(secret) = secret.map_err(|error| error.to_string())? else {
        return Ok(ClaudeAccountQuota::without_plan(
            SubscriptionQuota::not_found("claude"),
        ));
    };
    let with_plan = |quota| ClaudeAccountQuota {
        quota,
        plan: plan.clone().filter(|plan| plan.subscription_type.is_some()),
    };
    let json = String::from_utf8_lossy(&secret.to_bytes()).into_owned();
    let direct = claude_quota_from(parse_claude_credentials_json(&json)).await?;
    if !needs_cli_usage(&direct) {
        return Ok(with_plan(direct));
    }

    let account_uuid = account_uuid_of(&account).to_string();
    let profile = slot.global_config();
    let config_dir = match &slot {
        snapshot::store::Slot::Default { .. } => None,
        snapshot::store::Slot::Account(dir) => Some(dir.clone()),
    };
    let cached = tokio::task::spawn_blocking(move || {
        let ran = usage::refresh_via_cli(&account, config_dir.as_deref());
        // Claude Code 刷新时会续上登录:记下新的过期时间,免得界面按旧的报「要重新登录」
        if let Some(dir) = config_dir.filter(|_| ran && !snapshot::is_live(&account)) {
            snapshot::note_login_expiry(&data_dir_for_expiry, &account, &dir);
        }
        usage::read_cached(&profile, &account_uuid)
    })
    .await
    .map_err(|error| error.to_string())?;
    Ok(with_plan(account_quota_with_cache(
        direct,
        cached,
        now_millis(),
    )))
}

/// 跟随本地登录的 Claude 官方卡:按 `~/.claude` 现在的登录查额度。令牌过期了(几个小时没用
/// Claude Code 就会这样)和绑号卡一样,在默认槽位里跑一次 `claude -p /usage`,读它缓存的额度。
async fn get_local_claude_quota() -> Result<SubscriptionQuota, String> {
    use crate::services::claude_account::{snapshot, usage};

    let direct = claude_quota_from(read_claude_credentials()).await?;
    // 改了配置目录时登录不一定在 ~/.claude,在那里跑会拿到别的号的额度
    if !needs_cli_usage(&direct)
        || crate::settings::get_override_dir(&crate::app_config::AppType::Claude).is_some()
    {
        return Ok(direct);
    }
    let cached = tokio::task::spawn_blocking(|| {
        let slot = snapshot::store::Slot::default_slot();
        let account = snapshot::default_owner(&slot)?;
        usage::refresh_via_cli(&account, None);
        usage::read_cached(&slot.global_config(), account_uuid_of(&account))
    })
    .await
    .map_err(|error| error.to_string())?;
    Ok(account_quota_with_cache(direct, cached, now_millis()))
}

/// 账号 key(`<accountUuid>:<organizationUuid>`)里的 `accountUuid`。
fn account_uuid_of(account: &str) -> &str {
    account.split_once(':').map_or(account, |(uuid, _)| uuid)
}

/// 绑号卡的额度,带上这个号的套餐(凭据里记的;读不到时缺省)。
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ClaudeAccountQuota {
    #[serde(flatten)]
    pub quota: SubscriptionQuota,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub plan: Option<crate::services::claude_account::AccountPlan>,
}

impl ClaudeAccountQuota {
    fn without_plan(quota: SubscriptionQuota) -> Self {
        Self { quota, plan: None }
    }
}

/// 直接查的结果是令牌过期:要在槽位里跑 `claude -p /usage`,让 Claude Code 自己刷新。
fn needs_cli_usage(direct: &SubscriptionQuota) -> bool {
    !direct.success
        && matches!(
            direct.credential_status,
            CredentialStatus::Expired | CredentialStatus::RefreshPending
        )
}

/// 令牌过期时用 Claude Code 缓存的额度;缓存太旧(`/usage` 一直没跑成)就还是报过期。
fn account_quota_with_cache(
    direct: SubscriptionQuota,
    cached: Option<crate::services::claude_account::usage::CachedUsage>,
    now_ms: i64,
) -> SubscriptionQuota {
    use crate::services::claude_account::usage::CACHE_MAX_AGE;

    match cached {
        Some(cached) if now_ms - cached.fetched_at_ms <= CACHE_MAX_AGE.as_millis() as i64 => {
            SubscriptionQuota {
                queried_at: Some(cached.fetched_at_ms),
                ..parse_claude_quota(&cached.utilization)
            }
        }
        _ => direct,
    }
}

/// 查询指定 CLI 工具的官方订阅额度
///
/// 瞬时传输失败以 `Err` 传播(前端 reject → retry + 保留上次成功值)。Expired
/// 分支的"过期也试一把"重试同样用 `?` 传播瞬时错误——不能折叠成"已过期",
/// 否则一次网络抖动会被误报成确定性的凭据过期。
pub async fn get_subscription_quota(tool: &str) -> Result<SubscriptionQuota, String> {
    match tool {
        "claude" => get_local_claude_quota().await,
        "codex" => {
            let (token, account_id, status, message) = read_codex_credentials();

            match status {
                CredentialStatus::NotFound => Ok(SubscriptionQuota::not_found("codex")),
                CredentialStatus::ParseError => Ok(SubscriptionQuota::error(
                    "codex",
                    CredentialStatus::ParseError,
                    message.unwrap_or_else(|| "Failed to parse credentials".to_string()),
                )),
                CredentialStatus::Expired | CredentialStatus::RefreshPending => {
                    // 即使可能过期也尝试调用 API
                    if let Some(token) = token {
                        let result = query_codex_quota(
                            &token,
                            account_id.as_deref(),
                            "codex",
                            "Authentication failed. Please re-login with Codex CLI.",
                        )
                        .await?;
                        if result.success {
                            return Ok(result);
                        }
                    }
                    Ok(SubscriptionQuota::error(
                        "codex",
                        CredentialStatus::Expired,
                        message.unwrap_or_else(|| "Codex OAuth token may be stale".to_string()),
                    ))
                }
                CredentialStatus::Valid => {
                    let token = token.expect("token must be Some when status is Valid");
                    query_codex_quota(
                        &token,
                        account_id.as_deref(),
                        "codex",
                        "Authentication failed. Please re-login with Codex CLI.",
                    )
                    .await
                }
            }
        }
        "gemini" => {
            let (token, refresh_token, status, message) = read_gemini_credentials();

            match status {
                CredentialStatus::NotFound => Ok(SubscriptionQuota::not_found("gemini")),
                CredentialStatus::ParseError => Ok(SubscriptionQuota::error(
                    "gemini",
                    CredentialStatus::ParseError,
                    message.unwrap_or_else(|| "Failed to parse credentials".to_string()),
                )),
                CredentialStatus::Expired | CredentialStatus::RefreshPending => {
                    // Gemini access_token 仅 ~1h 有效,尝试用 refresh_token 刷新
                    if let Some(ref rt) = refresh_token {
                        if let Some(new_token) = refresh_gemini_token(rt).await {
                            return query_gemini_quota(&new_token).await;
                        }
                    }
                    // 刷新失败,尝试用旧 token
                    if let Some(ref token) = token {
                        let result = query_gemini_quota(token).await?;
                        if result.success {
                            return Ok(result);
                        }
                    }
                    Ok(SubscriptionQuota::error(
                        "gemini",
                        CredentialStatus::Expired,
                        message.unwrap_or_else(|| "Gemini OAuth token has expired".to_string()),
                    ))
                }
                CredentialStatus::Valid => {
                    let token = token.expect("token must be Some when status is Valid");
                    query_gemini_quota(&token).await
                }
            }
        }
        "grokbuild" => crate::services::subscription_grok::get_grok_subscription_quota().await,
        "agy" => crate::services::subscription_agy::get_agy_subscription_quota().await,
        _ => Ok(SubscriptionQuota::not_found(tool)),
    }
}

// ── 辅助函数 ──────────────────────────────────────────────

fn now_millis() -> i64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis() as i64
}

#[cfg(test)]
mod tests {
    use super::*;

    // ── 绑号卡额度:令牌过期时才借 Claude Code 的缓存,太旧的缓存不用 ──

    fn expired_quota() -> SubscriptionQuota {
        SubscriptionQuota::error(
            "claude",
            CredentialStatus::Expired,
            "OAuth token has expired".to_string(),
        )
    }

    fn cached_usage(fetched_at_ms: i64) -> crate::services::claude_account::usage::CachedUsage {
        crate::services::claude_account::usage::CachedUsage {
            fetched_at_ms,
            utilization: serde_json::json!({
                "five_hour": {"utilization": 13.0, "resets_at": null}
            }),
        }
    }

    #[test]
    fn default_plan_reads_the_plan_next_to_the_login() {
        let plan = plan_from_credentials_json(
            r#"{"claudeAiOauth":{"accessToken":"at","subscriptionType":"team","rateLimitTier":"default_claude_max_5x"}}"#,
        )
        .unwrap();
        assert_eq!(plan.subscription_type.as_deref(), Some("team"));
        assert_eq!(
            plan.rate_limit_tier.as_deref(),
            Some("default_claude_max_5x")
        );
        assert_eq!(plan_from_credentials_json(r#"{"claudeAiOauth":{}}"#), None);
        assert_eq!(plan_from_credentials_json("not json"), None);
    }

    #[test]
    fn only_an_expired_token_runs_claude_usage() {
        assert!(needs_cli_usage(&expired_quota()));
        let pending = SubscriptionQuota::error(
            "claude",
            CredentialStatus::RefreshPending,
            "pending".to_string(),
        );
        assert!(needs_cli_usage(&pending));
        // 查成功、凭据坏了、或接口别的错误(令牌有效)都不跑
        assert!(!needs_cli_usage(&parse_claude_quota(
            &serde_json::json!({})
        )));
        assert!(!needs_cli_usage(&SubscriptionQuota::not_found("claude")));
        assert!(!needs_cli_usage(&SubscriptionQuota::error(
            "claude",
            CredentialStatus::Valid,
            "HTTP 500".to_string(),
        )));
    }

    #[test]
    fn expired_token_uses_a_fresh_cache_but_not_a_stale_one() {
        let now = 1_700_000_000_000_i64;
        let fresh = account_quota_with_cache(expired_quota(), Some(cached_usage(now - 1_000)), now);
        assert!(fresh.success);
        assert_eq!(fresh.queried_at, Some(now - 1_000));
        assert_eq!(fresh.tiers.len(), 1);

        // /usage 一直没跑成:几天前的缓存不能当成查到了,照样报过期
        let stale_at = now - 3 * 24 * 60 * 60 * 1000;
        let stale = account_quota_with_cache(expired_quota(), Some(cached_usage(stale_at)), now);
        assert!(!stale.success);
        assert!(matches!(stale.credential_status, CredentialStatus::Expired));

        let none = account_quota_with_cache(expired_quota(), None, now);
        assert!(!none.success);
    }

    #[test]
    fn account_uuid_is_the_part_before_the_organization() {
        assert_eq!(account_uuid_of("acc-1:org-1"), "acc-1");
        assert_eq!(account_uuid_of("acc-1:"), "acc-1");
        assert_eq!(account_uuid_of("acc-1"), "acc-1");
    }

    #[cfg(target_os = "macos")]
    fn claude_keychain_output(code: i32, secret: &str) -> std::process::Output {
        use std::os::unix::process::ExitStatusExt;
        std::process::Output {
            status: std::process::ExitStatus::from_raw(code << 8),
            stdout: secret.as_bytes().to_vec(),
            stderr: Vec::new(),
        }
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn claude_keychain_prefers_the_current_account() {
        let mut calls = 0;
        let (token, status, _) =
            read_claude_credentials_from_keychain_with(Some("alice"), |command| {
                calls += 1;
                assert_eq!(command.get_program(), "security");
                assert_eq!(
                    command.get_args().collect::<Vec<_>>(),
                    [
                        "find-generic-password",
                        "-s",
                        "Claude Code-credentials",
                        "-a",
                        "alice",
                        "-w"
                    ]
                );
                Ok(claude_keychain_output(
                    0,
                    r#"{"claudeAiOauth":{"accessToken":"current-user-token"}}"#,
                ))
            })
            .unwrap();
        assert_eq!(calls, 1);
        assert_eq!(token.as_deref(), Some("current-user-token"));
        assert!(matches!(status, CredentialStatus::Valid));
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn claude_keychain_tries_legacy_query_when_account_has_no_usable_oauth() {
        for (code, secret) in [
            (SECURITY_ITEM_NOT_FOUND, ""),
            (0, r#"{"mcpOAuth":{}}"#),
            (0, "invalid JSON"),
            (0, r#"{"claudeAiOauth":{}}"#),
        ] {
            let mut calls = 0;
            let (token, status, _) =
                read_claude_credentials_from_keychain_with(Some("alice"), |command| {
                    calls += 1;
                    if calls == 1 {
                        return Ok(claude_keychain_output(code, secret));
                    }
                    assert_eq!(
                        command.get_args().collect::<Vec<_>>(),
                        [
                            "find-generic-password",
                            "-s",
                            "Claude Code-credentials",
                            "-w"
                        ]
                    );
                    Ok(claude_keychain_output(
                        0,
                        r#"{"claude.ai_oauth":{"accessToken":"legacy-token"}}"#,
                    ))
                })
                .unwrap();
            assert_eq!(calls, 2);
            assert_eq!(token.as_deref(), Some("legacy-token"));
            assert!(matches!(status, CredentialStatus::Valid));
        }
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn claude_keychain_without_oauth_allows_file_fallback() {
        for (account, code, expected_calls) in
            [(Some("alice"), 0, 2), (None, 0, 1), (Some("alice"), 1, 1)]
        {
            let mut calls = 0;
            let result = read_claude_credentials_from_keychain_with(account, |_| {
                calls += 1;
                Ok(claude_keychain_output(code, r#"{"mcpOAuth":{}}"#))
            });
            assert!(result.is_none());
            assert_eq!(calls, expected_calls);
        }
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn claude_keychain_preserves_expired_account_credentials() {
        let mut calls = 0;
        let (token, status, _) = read_claude_credentials_from_keychain_with(Some("alice"), |_| {
            calls += 1;
            Ok(claude_keychain_output(
                0,
                r#"{"claudeAiOauth":{"accessToken":"expired-token","expiresAt":1}}"#,
            ))
        })
        .unwrap();
        assert_eq!(calls, 1);
        assert_eq!(token.as_deref(), Some("expired-token"));
        assert!(matches!(status, CredentialStatus::Expired));
    }

    #[test]
    fn codex_reset_credits_count_only_unexpired_available() {
        let now = chrono::DateTime::parse_from_rfc3339("2026-10-04T00:00:00Z")
            .unwrap()
            .with_timezone(&chrono::Utc);
        let raw = br#"{
            "available_count": 9,
            "credits": [
                {"id":"a","reset_type":"weekly","status":"available","granted_at":"2026-09-20T00:00:00Z","expires_at":"2026-10-20T00:00:00Z"},
                {"id":"b","reset_type":"weekly","status":"available","granted_at":"2026-09-01T00:00:00Z","expires_at":"2026-10-03T00:00:00Z"},
                {"id":"c","reset_type":"weekly","status":"redeemed","granted_at":"2026-09-01T00:00:00Z","expires_at":"2026-10-30T00:00:00Z"},
                {"id":"d","reset_type":"weekly","status":"available","granted_at":"2026-09-01T00:00:00Z","expires_at":null},
                {"id":"e","reset_type":"weekly","status":"available","granted_at":"2026-09-25T00:00:00.123Z","expires_at":"2026-10-08T12:00:00.123Z"}
            ]
        }"#;
        let credits = parse_codex_reset_credits(raw, now).unwrap();
        // b 已过期、c 已用掉;剩下按到期先后,不过期的排最后
        assert_eq!(credits.expires_at.len(), 3);
        assert!(credits.expires_at[0]
            .as_deref()
            .unwrap()
            .starts_with("2026-10-08T12:00:00.123"));
        assert!(credits.expires_at[1]
            .as_deref()
            .unwrap()
            .starts_with("2026-10-20"));
        assert_eq!(credits.expires_at[2], None);
    }

    #[test]
    fn codex_reset_credits_empty_and_malformed() {
        let now = chrono::Utc::now();
        let empty = parse_codex_reset_credits(br#"{"available_count":0,"credits":[]}"#, now);
        assert_eq!(empty.unwrap().expires_at.len(), 0);
        assert!(parse_codex_reset_credits(b"<html>", now).is_none());
        // 新序列化的字段对旧缓存是可选的
        let quota: SubscriptionQuota = serde_json::from_str(
            r#"{"tool":"codex","credentialStatus":"valid","credentialMessage":null,
                "success":true,"tiers":[],"extraUsage":null,"error":null,"queriedAt":1}"#,
        )
        .unwrap();
        assert!(quota.reset_credits.is_none());
        assert!(quota.credits_balance.is_none());
    }

    #[test]
    fn codex_plan_label_follows_codex_status_names() {
        let label = |raw: &str| {
            let body: CodexUsageResponse = serde_json::from_str(raw).unwrap();
            body.plan_type.as_deref().and_then(codex_plan_label)
        };
        assert_eq!(label(r#"{"plan_type":"pro"}"#).as_deref(), Some("Pro 200"));
        assert_eq!(
            label(r#"{"plan_type":"prolite"}"#).as_deref(),
            Some("Pro 100")
        );
        assert_eq!(
            label(r#"{"plan_type":"team"}"#).as_deref(),
            Some("Business")
        );
        assert_eq!(
            label(r#"{"plan_type":"business"}"#).as_deref(),
            Some("Enterprise")
        );
        assert_eq!(
            label(r#"{"plan_type":"enterprise_cbp_usage_based"}"#).as_deref(),
            Some("Enterprise")
        );
        assert_eq!(
            label(r#"{"plan_type":"free_workspace"}"#).as_deref(),
            Some("Free Workspace")
        );
        assert_eq!(label(r#"{"plan_type":"unknown"}"#), None);
        assert_eq!(label(r#"{"rate_limit":null}"#), None);
    }

    #[test]
    fn codex_credits_balance_from_usage_response() {
        let parse = |raw: &str| {
            let body: CodexUsageResponse = serde_json::from_str(raw).unwrap();
            body.credits.as_ref().and_then(parse_codex_credits_balance)
        };
        // 实测形状:balance 是字符串
        assert_eq!(
            parse(
                r#"{"credits":{"has_credits":true,"unlimited":false,"overage_limit_reached":false,
                    "balance":"62500","approx_local_messages":[15625,81250]}}"#
            ),
            Some(62500.0)
        );
        assert_eq!(
            parse(r#"{"credits":{"has_credits":true,"unlimited":false,"balance":42.5}}"#),
            Some(42.5)
        );
        // 0、不限量、没有、缺字段、形状不对:都不显示,也不让额度解析失败
        assert_eq!(
            parse(r#"{"credits":{"has_credits":true,"unlimited":false,"balance":"0"}}"#),
            None
        );
        assert_eq!(
            parse(r#"{"credits":{"has_credits":true,"unlimited":true,"balance":"10"}}"#),
            None
        );
        assert_eq!(
            parse(r#"{"credits":{"has_credits":false,"unlimited":false,"balance":"10"}}"#),
            None
        );
        assert_eq!(parse(r#"{"credits":{"has_credits":true}}"#), None);
        assert_eq!(parse(r#"{"credits":"weird","rate_limit":null}"#), None);
        assert_eq!(parse(r#"{}"#), None);
    }

    /// 和 codex-rs `compute_store_key` 同一算法:路径不存在时按原样算,存在时先规范化
    /// (符号链接和它指向的目录是同一个账户)。
    #[cfg(target_os = "macos")]
    #[test]
    fn codex_keychain_account_matches_codex() {
        assert_eq!(
            codex_keychain_account(std::path::Path::new("/nonexistent/codex-home")),
            "cli|b5d85b424b15c4d9"
        );

        let dir = tempfile::TempDir::new().unwrap();
        let real = dir.path().join("codex-home");
        std::fs::create_dir(&real).unwrap();
        let link = dir.path().join("link");
        std::os::unix::fs::symlink(&real, &link).unwrap();
        assert_eq!(codex_keychain_account(&link), codex_keychain_account(&real));
    }

    #[test]
    fn claude_expired_access_token_with_live_refresh_token_is_refresh_pending() {
        let past = now_millis() - 60_000;
        let future = now_millis() + 3_600_000;
        let status = |entry: serde_json::Value| {
            parse_claude_credentials_json(
                &serde_json::json!({ "claudeAiOauth": entry }).to_string(),
            )
            .1
        };

        assert!(matches!(
            status(serde_json::json!({
                "accessToken": "a", "expiresAt": past,
                "refreshToken": "r", "refreshTokenExpiresAt": future
            })),
            CredentialStatus::RefreshPending
        ));
        // 旧版凭据没有刷新令牌的过期时间:有刷新令牌就按能刷新算。
        assert!(matches!(
            status(serde_json::json!({
                "accessToken": "a", "expiresAt": past, "refreshToken": "r"
            })),
            CredentialStatus::RefreshPending
        ));
        // 刷新令牌也过期了 / 根本没有:真的要重新登录。
        assert!(matches!(
            status(serde_json::json!({
                "accessToken": "a", "expiresAt": past,
                "refreshToken": "r", "refreshTokenExpiresAt": past
            })),
            CredentialStatus::Expired
        ));
        assert!(matches!(
            status(serde_json::json!({ "accessToken": "a", "expiresAt": past })),
            CredentialStatus::Expired
        ));
        assert!(matches!(
            status(serde_json::json!({
                "accessToken": "a", "expiresAt": future, "refreshToken": "r"
            })),
            CredentialStatus::Valid
        ));
    }

    fn scoped_limit(model: &str, percent: f64) -> serde_json::Value {
        serde_json::json!({
            "kind": "weekly_scoped",
            "group": "weekly",
            "percent": percent,
            "resets_at": "2026-09-12T00:00:00Z",
            "is_active": true,
            "scope": { "model": { "id": null, "display_name": model }, "surface": null }
        })
    }

    #[test]
    fn claude_quota_preserves_legacy_windows_and_extra_usage() {
        let quota = parse_claude_quota(&serde_json::json!({
            "five_hour": { "utilization": 12.0, "resets_at": "2026-09-09T15:00:00Z" },
            "seven_day": { "utilization": 25.0, "resets_at": null },
            "seven_day_opus": { "utilization": 8.0 },
            "seven_day_sonnet": null,
            "other_window": { "utilization": 4.0 },
            "extra_usage": { "is_enabled": true, "monthly_limit": 100.0,
                "used_credits": 9.0, "utilization": 9.0, "currency": "USD" }
        }));
        assert!(quota.success);
        assert_eq!(quota.tool, "claude");
        assert_eq!(
            quota
                .tiers
                .iter()
                .map(|t| (t.name.as_str(), t.utilization))
                .collect::<Vec<_>>(),
            vec![
                (TIER_FIVE_HOUR, 12.0),
                (TIER_SEVEN_DAY, 25.0),
                (TIER_SEVEN_DAY_OPUS, 8.0),
                ("other_window", 4.0)
            ]
        );
        assert_eq!(
            quota.tiers[0].resets_at.as_deref(),
            Some("2026-09-09T15:00:00Z")
        );
        let extra = quota.extra_usage.unwrap();
        assert!(extra.is_enabled);
        assert_eq!(extra.used_credits, Some(9.0));
        assert_eq!(extra.monthly_limit, Some(100.0));
        assert_eq!(extra.currency.as_deref(), Some("USD"));
    }

    #[test]
    fn claude_quota_adds_fable_from_limits_array() {
        let quota = parse_claude_quota(&serde_json::json!({
            "five_hour": { "utilization": 12.0 },
            "seven_day": { "utilization": 25.0 },
            "seven_day_opus": null,
            "seven_day_sonnet": null,
            "limits": [scoped_limit("Fable", 37.5)]
        }));
        assert_eq!(quota.tiers.len(), 3);
        let tier = &quota.tiers[2];
        assert_eq!(tier.name, TIER_SEVEN_DAY_FABLE);
        assert_eq!(tier.utilization, 37.5);
        assert_eq!(tier.resets_at.as_deref(), Some("2026-09-12T00:00:00Z"));
        // 前端与缓存使用同一份 camelCase 数据,无需额外字段。
        let serialized = serde_json::to_value(&quota).unwrap();
        assert_eq!(serialized["tiers"][2]["resetsAt"], "2026-09-12T00:00:00Z");
    }

    #[test]
    fn claude_quota_scoped_windows_override_legacy_and_deduplicate() {
        let mut fable = scoped_limit("  fAbLe  ", 0.0);
        fable["is_active"] = serde_json::json!(false);
        fable["resets_at"] = serde_json::Value::Null;
        let quota = parse_claude_quota(&serde_json::json!({
            "seven_day_fable": { "utilization": 80.0, "resets_at": "2026-09-11T00:00:00Z" },
            "seven_day_opus": { "utilization": 20.0 },
            "seven_day_sonnet": { "utilization": 30.0 },
            "limits": [scoped_limit("Sonnet", 5.0), fable, scoped_limit("Fable", 90.0), scoped_limit("Opus", 6.0)]
        }));
        assert_eq!(
            quota
                .tiers
                .iter()
                .map(|t| (t.name.as_str(), t.utilization))
                .collect::<Vec<_>>(),
            vec![
                (TIER_SEVEN_DAY_FABLE, 0.0),
                (TIER_SEVEN_DAY_OPUS, 6.0),
                (TIER_SEVEN_DAY_SONNET, 5.0)
            ]
        );
        assert_eq!(quota.tiers[0].resets_at, None);
    }

    #[test]
    fn claude_quota_skips_invalid_or_unrelated_scoped_rows() {
        let valid = scoped_limit("Fable", 37.0);
        let mut invalid = vec![serde_json::Value::Null, serde_json::json!("invalid")];
        for (pointer, value) in [
            ("/kind", serde_json::json!("spend")),
            ("/group", serde_json::json!("daily")),
            ("/percent", serde_json::Value::Null),
            ("/percent", serde_json::json!("37")),
            ("/percent", serde_json::json!(-1)),
            ("/resets_at", serde_json::json!(123)),
            ("/scope/model/display_name", serde_json::Value::Null),
            ("/scope/model/display_name", serde_json::json!("Unknown")),
            ("/scope/surface", serde_json::json!("claude_code")),
        ] {
            let mut row = valid.clone();
            *row.pointer_mut(pointer).unwrap() = value;
            invalid.push(row);
        }
        let mut body = serde_json::json!({
            "five_hour": { "utilization": 12.0 },
            "seven_day_fable": { "utilization": 8.0 },
            "limits": invalid
        });
        let fallback = parse_claude_quota(&body);
        assert_eq!(fallback.tiers.len(), 2);
        assert_eq!(fallback.tiers[1].utilization, 8.0);
        body["limits"].as_array_mut().unwrap().push(valid);
        let quota = parse_claude_quota(&body);
        assert_eq!(quota.tiers.len(), 2);
        assert_eq!(quota.tiers[0].utilization, 12.0);
        assert_eq!(quota.tiers[1].utilization, 37.0);
    }

    #[test]
    fn claude_quota_does_not_invent_missing_fable_usage() {
        for limits in [
            serde_json::Value::Null,
            serde_json::json!([]),
            serde_json::json!({}),
        ] {
            let quota = parse_claude_quota(&serde_json::json!({
                "five_hour": { "utilization": 12.0 },
                "limits": limits
            }));
            assert_eq!(quota.tiers.len(), 1);
            assert_eq!(quota.tiers[0].name, TIER_FIVE_HOUR);
        }
        let quota =
            parse_claude_quota(&serde_json::json!({ "limits": [scoped_limit("Fable", 100.0)] }));
        assert_eq!(quota.tiers.len(), 1);
        assert_eq!(quota.tiers[0].name, TIER_SEVEN_DAY_FABLE);
        assert_eq!(quota.tiers[0].utilization, 100.0);
    }

    #[test]
    fn window_seconds_map_to_expected_tier_names() {
        // 官方特例窗口
        assert_eq!(window_seconds_to_tier_name(18000), TIER_FIVE_HOUR);
        assert_eq!(window_seconds_to_tier_name(604800), TIER_SEVEN_DAY);
        // Codex 免费方案的次要窗口是 30 天(30 * 24 * 3600 = 2_592_000 秒)。
        // 前端 TIER_I18N_KEYS 与 tray 月分组都需要认得 "30_day",见 #3651。
        assert_eq!(window_seconds_to_tier_name(2_592_000), TIER_THIRTY_DAY);
        // 其他窗口按小时/天回退命名
        assert_eq!(window_seconds_to_tier_name(3600), "1_hour");
        assert_eq!(window_seconds_to_tier_name(86400), "1_day");
    }
}