use crate::{ApiResponse, error::ApiError};
use actix_web::{HttpResponse, Result, web};
use room::ws_context::WsUserContext;
use service::AppService;
use session::Session;
use uuid::Uuid;

#[utoipa::path(
    get,
    path = "/api/rooms/{room_id}/ai",
    params(
        ("room_id" = Uuid, Path),
    ),
    responses(
        (status = 200, description = "List room AI configurations", body = ApiResponse<Vec<room::RoomAiResponse>>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
    ),
    tag = "Room"
)]
pub async fn ai_list(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<Uuid>,
) -> Result<HttpResponse, ApiError> {
    let room_id = path.into_inner();
    let user_id = session
        .user()
        .ok_or_else(|| ApiError::from(service::error::AppError::Unauthorized))?;
    let ctx = WsUserContext::new(user_id);
    let resp = service
        .room
        .room_ai_list(room_id, &ctx)
        .await
        .map_err(ApiError::from)?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    put,
    path = "/api/rooms/{room_id}/ai",
    params(
        ("room_id" = Uuid, Path),
    ),
    request_body = room::RoomAiUpsertRequest,
    responses(
        (status = 200, description = "Upsert room AI configuration", body = ApiResponse<room::RoomAiResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
    ),
    tag = "Room"
)]
pub async fn ai_upsert(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<Uuid>,
    body: web::Json<room::RoomAiUpsertRequest>,
) -> Result<HttpResponse, ApiError> {
    let room_id = path.into_inner();
    let user_id = session
        .user()
        .ok_or_else(|| ApiError::from(service::error::AppError::Unauthorized))?;
    let ctx = WsUserContext::new(user_id);
    let resp = service
        .room
        .room_ai_upsert(room_id, body.into_inner(), &ctx)
        .await
        .map_err(ApiError::from)?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    delete,
    path = "/api/rooms/{room_id}/ai/{model_id}",
    params(
        ("room_id" = Uuid, Path),
        ("model_id" = Uuid, Path),
    ),
    responses(
        (status = 200, description = "Delete room AI configuration"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
    ),
    tag = "Room"
)]
pub async fn ai_delete(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(Uuid, Uuid)>,
) -> Result<HttpResponse, ApiError> {
    let (room_id, model_id) = path.into_inner();
    let user_id = session
        .user()
        .ok_or_else(|| ApiError::from(service::error::AppError::Unauthorized))?;
    let ctx = WsUserContext::new(user_id);
    service
        .room
        .room_ai_delete(room_id, model_id, &ctx)
        .await
        .map_err(ApiError::from)?;
    Ok(ApiResponse::ok(true).to_response())
}
