use super::{JoinRequestQuery, PageQuery};
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",
    params(
        ("project_name" = String, Path),
        ("status" = Option<String>, Query),
        ("page" = Option<u64>, Query),
        ("per_page" = Option<u64>, Query),
    ),
    responses(
        (status = 200, description = "List join requests", body = ApiResponse<service::project::join_request::JoinRequestListResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
),
    tag = "Project"
)]
pub async fn project_join_requests(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<String>,
    query: web::Query<JoinRequestQuery>,
) -> Result<HttpResponse, ApiError> {
    let project_name = path.into_inner();
    let resp = service
        .project_get_join_requests(
            project_name,
            query.status.clone(),
            query.page,
            query.per_page,
            &session,
        )
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

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

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

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

#[utoipa::path(
    get,
    path = "/api/projects/me/join-requests",
    responses(
        (status = 200, description = "List my join requests", body = ApiResponse<service::project::join_request::JoinRequestListResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "Project"
)]
pub async fn project_my_join_requests(
    service: web::Data<AppService>,
    session: Session,
    query: web::Query<PageQuery>,
) -> Result<HttpResponse, ApiError> {
    let resp = service
        .project_get_my_join_requests(query.page, query.per_page, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}
