use db::models::ai::AiUsageLogRepository;
use sqlx::PgPool;
use uuid::Uuid;

/// Log an AI usage event. Errors are logged but not propagated, because usage
/// logging should never break the user-facing response.
#[allow(clippy::too_many_arguments)]
pub async fn log_usage(
    pool: &PgPool,
    user_id: Uuid,
    role_code: Option<&str>,
    feature_code: &str,
    model_alias: &str,
    credits_charged: i32,
    input_tokens: Option<i32>,
    output_tokens: Option<i32>,
    total_tokens: Option<i32>,
    status: &str,
    request_id: Option<&str>,
    error_message: Option<&str>,
) {
    if let Err(e) = AiUsageLogRepository::create(
        pool,
        user_id,
        role_code,
        feature_code,
        model_alias,
        credits_charged,
        input_tokens,
        output_tokens,
        total_tokens,
        status,
        request_id,
        error_message,
    )
    .await
    {
        tracing::error!("Failed to log AI usage for user {}: {}", user_id, e);
    }
}
