//! Inventory API handlers for user UI.
//!
//! Handles inventory listing and item management.

use axum::extract::Path;
use axum::Json;
use uuid::Uuid;

use chattyness_db::{models::InventoryResponse, queries::inventory};
use chattyness_error::AppError;

use crate::auth::{AuthUser, RlsConn};

/// Get user's full inventory.
///
/// GET /api/inventory
pub async fn get_inventory(
    rls_conn: RlsConn,
    AuthUser(user): AuthUser,
) -> Result<Json<InventoryResponse>, AppError> {
    let mut conn = rls_conn.acquire().await;

    let items = inventory::list_user_inventory(&mut *conn, user.id).await?;

    Ok(Json(InventoryResponse { items }))
}

/// Drop an item from inventory.
///
/// DELETE /api/inventory/{item_id}
pub async fn drop_item(
    rls_conn: RlsConn,
    AuthUser(user): AuthUser,
    Path(item_id): Path<Uuid>,
) -> Result<Json<serde_json::Value>, AppError> {
    let mut conn = rls_conn.acquire().await;

    inventory::drop_inventory_item(&mut *conn, user.id, item_id).await?;

    Ok(Json(serde_json::json!({ "success": true })))
}
