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

#[utoipa::path(
    post,
    path = "/api/workspaces",
    request_body = WorkspaceInitParams,
    responses(
        (status = 200, description = "Create workspace", body = ApiResponse<service::workspace::info::WorkspaceInfoResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 409, description = "Slug or name already exists"),
    ),
    tag = "Workspace"
)]
pub async fn workspace_create(
    service: web::Data<AppService>,
    session: Session,
    body: web::Json<WorkspaceInitParams>,
) -> Result<HttpResponse, ApiError> {
    let ws = service.workspace_init(&session, body.into_inner()).await?;
    let resp = service.workspace_info(&session, ws.slug.clone()).await?;
    Ok(ApiResponse::ok(resp).to_response())
}
