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/project_room/{project_name}/room-categories",
    params(
        ("project_name" = String, Path),
    ),
    responses(
        (status = 200, description = "List room categories", body = ApiResponse<Vec<room::RoomCategoryResponse>>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
    ),
    tag = "Room"
)]
pub async fn category_list(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let project_name = 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_category_list(project_name, &ctx)
        .await
        .map_err(ApiError::from)?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    post,
    path = "/api/project_room/{project_name}/room-categories",
    params(
        ("project_name" = String, Path),
    ),
    request_body = room::RoomCategoryCreateRequest,
    responses(
        (status = 200, description = "Create room category", body = ApiResponse<room::RoomCategoryResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
    ),
    tag = "Room"
)]
pub async fn category_create(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<String>,
    body: web::Json<room::RoomCategoryCreateRequest>,
) -> Result<HttpResponse, ApiError> {
    let project_name = 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_category_create(project_name, body.into_inner(), &ctx)
        .await
        .map_err(ApiError::from)?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    patch,
    path = "/api/room-categories/{category_id}",
    params(
        ("category_id" = Uuid, Path),
    ),
    request_body = room::RoomCategoryUpdateRequest,
    responses(
        (status = 200, description = "Update room category", body = ApiResponse<room::RoomCategoryResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
    ),
    tag = "Room"
)]
pub async fn category_update(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<Uuid>,
    body: web::Json<room::RoomCategoryUpdateRequest>,
) -> Result<HttpResponse, ApiError> {
    let category_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_category_update(category_id, body.into_inner(), &ctx)
        .await
        .map_err(ApiError::from)?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    delete,
    path = "/api/room-categories/{category_id}",
    params(
        ("category_id" = Uuid, Path),
    ),
    responses(
        (status = 200, description = "Delete room category"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
    ),
    tag = "Room"
)]
pub async fn category_delete(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<Uuid>,
) -> Result<HttpResponse, ApiError> {
    let category_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_category_delete(category_id, &ctx)
        .await
        .map_err(ApiError::from)?;
    Ok(ApiResponse::ok(true).to_response())
}
