use crate::{ApiResponse, error::ApiError};
use actix_web::{HttpResponse, Result, web};
use service::AppService;
use session::Session;

#[utoipa::path(
    get,
    path = "/api/workspaces/me",
    responses(
        (status = 200, description = "List my workspaces", body = ApiResponse<service::workspace::info::WorkspaceListResponse>),
        (status = 401, description = "Unauthorized"),
    ),
    tag = "Workspace"
)]
pub async fn workspace_list(
    service: web::Data<AppService>,
    session: Session,
) -> Result<HttpResponse, ApiError> {
    let resp = service.workspace_list(&session).await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/workspaces/{slug}",
    params(("slug" = String, Path)),
    responses(
        (status = 200, description = "Get workspace info", body = ApiResponse<service::workspace::info::WorkspaceInfoResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Workspace not found"),
    ),
    tag = "Workspace"
)]
pub async fn workspace_info(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let slug = path.into_inner();
    let resp = service.workspace_info(&session, slug).await?;
    Ok(ApiResponse::ok(resp).to_response())
}
