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

#[utoipa::path(
    get,
    path = "/api/projects/{project_name}/join-settings",
    params(("project_name" = String, Path)),
    responses(
        (status = 200, description = "Get join settings", body = ApiResponse<service::project::join_settings::JoinSettingsResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "Project"
)]
pub async fn project_join_settings(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let project_name = path.into_inner();
    let resp = service
        .project_get_join_settings(project_name, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    patch,
    path = "/api/projects/{project_name}/join-settings",
    params(("project_name" = String, Path)),
    request_body = service::project::join_settings::UpdateJoinSettingsRequest,
    responses(
        (status = 200, description = "Update join settings", body = ApiResponse<service::project::join_settings::JoinSettingsResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
),
    tag = "Project"
)]
pub async fn project_update_join_settings(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<String>,
    body: web::Json<service::project::join_settings::UpdateJoinSettingsRequest>,
) -> Result<HttpResponse, ApiError> {
    let project_name = path.into_inner();
    let resp = service
        .project_update_join_settings(project_name, body.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}
