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

#[utoipa::path(
    get,
    path = "/api/issue/{project}/issues/{number}/assignees",
    params(
        ("project" = String, Path),
        ("number" = i64, Path),
    ),
    responses(
        (status = 200, description = "List issue assignees", body = ApiResponse<Vec<service::issue::IssueAssigneeResponse>>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "Issues"
)]
pub async fn issue_assignee_list(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, i64)>,
) -> Result<HttpResponse, ApiError> {
    let (project, issue_number) = path.into_inner();
    let resp = service
        .issue_assignee_list(project, issue_number, &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    post,
    path = "/api/issue/{project}/issues/{number}/assignees",
    params(
        ("project" = String, Path),
        ("number" = i64, Path),
    ),
    request_body = service::issue::IssueAssignUserRequest,
    responses(
        (status = 200, description = "Add assignee to issue", body = ApiResponse<service::issue::IssueAssigneeResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
),
    tag = "Issues"
)]
pub async fn issue_assignee_add(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, i64)>,
    body: web::Json<service::issue::IssueAssignUserRequest>,
) -> Result<HttpResponse, ApiError> {
    let (project, issue_number) = path.into_inner();
    let resp = service
        .issue_assignee_add(project, issue_number, body.into_inner(), &session)
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    delete,
    path = "/api/issue/{project}/issues/{number}/assignees/{assignee_id}",
    params(
        ("project" = String, Path),
        ("number" = i64, Path),
        ("assignee_id" = String, Path),
    ),
    responses(
        (status = 200, description = "Remove assignee from issue"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden"),
        (status = 404, description = "Not found"),
),
    tag = "Issues"
)]
pub async fn issue_assignee_remove(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<(String, i64, String)>,
) -> Result<HttpResponse, ApiError> {
    let (project, issue_number, assignee_id) = path.into_inner();
    let assignee_uuid = uuid::Uuid::parse_str(&assignee_id)
        .map_err(|_| service::error::AppError::BadRequest("Invalid UUID".to_string()))?;
    service
        .issue_assignee_remove(project, issue_number, assignee_uuid, &session)
        .await?;
    Ok(ApiResponse::ok(serde_json::json!({ "success": true })).to_response())
}
