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-requests/{request_id}/answers",
    params(
        ("project_name" = String, Path),
        ("request_id" = i64, Path),
    ),
    responses(
        (status = 200, description = "Get join request answers", body = ApiResponse<service::project::join_answers::JoinAnswersListResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
),
    tag = "Project"
)]
pub async fn project_join_answers(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, i64)>,
) -> Result<HttpResponse, ApiError> {
    let (project_name, request_id) = path.into_inner();
    let resp = service
        .project_get_join_answers(project_name, request_id, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    post,
    path = "/api/projects/{project_name}/join-requests/{request_id}/answers",
    params(
        ("project_name" = String, Path),
        ("request_id" = i64, Path),
    ),
    request_body = Vec<service::project::join_answers::AnswerRequest>,
    responses(
        (status = 200, description = "Submit join request answers"),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "Project"
)]
pub async fn project_submit_join_answers(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, i64)>,
    body: web::Json<Vec<service::project::join_answers::AnswerRequest>>,
) -> Result<HttpResponse, ApiError> {
    let (project_name, request_id) = path.into_inner();
    service
        .project_submit_join_answers(project_name, request_id, body.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(serde_json::json!({ "success": true })).to_response())
}
