use crate::client::upgrade::Tier;
use crate::server::{AppError, AppState, AuthUser};
use axum::{extract::State, Json};
use serde::Serialize;

#[derive(Serialize)]
pub struct AccountInfo {
    pub user_id: String,
    pub email: String,
    pub tier: String,
    pub created_at: String,
    pub quota_tokens: Option<u64>,
    pub vault_fact_limit: Option<usize>,
    pub cloud_sync_enabled: bool,
    pub team_vault_enabled: bool,
    pub upgrade_url: &'static str,
}

pub async fn me(
    State(state): State<AppState>,
    AuthUser(user_id): AuthUser,
) -> Result<Json<AccountInfo>, AppError> {
    let user = state
        .db
        .get_user(&user_id)
        .ok_or_else(|| crate::server::AppError(anyhow::anyhow!("user not found")))?;
    let tier = Tier::parse_tier(&user.tier);
    Ok(Json(AccountInfo {
        user_id: user.id,
        email: user.email,
        tier: user.tier,
        created_at: user.created_at,
        quota_tokens: tier.cloud_token_quota(),
        vault_fact_limit: tier.vault_fact_limit(),
        cloud_sync_enabled: tier.cloud_sync_enabled(),
        team_vault_enabled: tier.team_vault_enabled(),
        upgrade_url: tier.upgrade_url(),
    }))
}
