use axum::extract::{Path, State};
use axum::Json;
use serde::Serialize;

use crate::state::AppState;

#[derive(Serialize)]
pub struct UsageResponse {
    pub paste_count: i64,
    pub storage_bytes: i64,
}

pub async fn user_usage(
    State(state): State<AppState>,
    Path(sub): Path<String>,
) -> Json<UsageResponse> {
    let (paste_count, storage_bytes) = sqlx::query_as::<_, (i64, i64)>(
        "SELECT COUNT(*), COALESCE(SUM(size_bytes), 0) FROM pastes WHERE user_id = $1",
    )
    .bind(&sub)
    .fetch_one(&state.db)
    .await
    .unwrap_or((0, 0));

    Json(UsageResponse {
        paste_count,
        storage_bytes,
    })
}
