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

#[derive(Debug, serde::Deserialize, IntoParams)]
pub struct RoomListQuery {
    pub only_public: Option<bool>,
}

#[utoipa::path(
    get,
    path = "/api/project_room/{project_name}/rooms",
    params(
        ("project_name" = String, Path),
        ("only_public" = Option<bool>, Query),
    ),
    responses(
        (status = 200, description = "List rooms", body = ApiResponse<Vec<room::RoomResponse>>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "Room"
)]
pub async fn room_list(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<String>,
    query: web::Query<RoomListQuery>,
) -> 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_list(project_name, query.only_public, &ctx)
        .await
        .map_err(ApiError::from)?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/rooms/{room_id}",
    params(
        ("room_id" = Uuid, Path),
    ),
    responses(
        (status = 200, description = "Get room", body = ApiResponse<room::RoomResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "Room"
)]
pub async fn room_get(
    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_get(room_id, &ctx)
        .await
        .map_err(ApiError::from)?;
    Ok(ApiResponse::ok(resp).to_response())
}

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

#[utoipa::path(
    patch,
    path = "/api/rooms/{room_id}",
    params(
        ("room_id" = Uuid, Path),
    ),
    request_body = room::RoomUpdateRequest,
    responses(
        (status = 200, description = "Update room", body = ApiResponse<room::RoomResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
),
    tag = "Room"
)]
pub async fn room_update(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<Uuid>,
    body: web::Json<room::RoomUpdateRequest>,
) -> 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_update(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}",
    params(
        ("room_id" = Uuid, Path),
    ),
    responses(
        (status = 200, description = "Delete room"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
),
    tag = "Room"
)]
pub async fn room_delete(
    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);
    service
        .room
        .room_delete(room_id, &ctx)
        .await
        .map_err(ApiError::from)?;
    Ok(ApiResponse::ok(true).to_response())
}
