use actix_web::{HttpResponse, web};
use serde::{Deserialize, Serialize};
use service::{
    AppService, Pagination,
    issues::{
        issue::{CreateIssue, UpdateIssue},
        types::{IssueFilter, IssueResponse},
    },
};
use session::Session;

use crate::error::ApiError;

fn ok_json<T: Serialize>(data: T) -> Result<HttpResponse, ApiError> {
    Ok(HttpResponse::Ok().json(data))
}

fn ok() -> Result<HttpResponse, ApiError> {
    Ok(HttpResponse::Ok().finish())
}

#[derive(Deserialize, utoipa::IntoParams)]
pub struct IssuePath {
    pub wk: String,
    pub number: i64,
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/issues",
    params(
        ("wk" = String, Path, description = "Workspace name"),
    ),
    request_body = CreateIssue,
    responses(
        (status = 200, body = IssueResponse),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Workspace not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn create_issue(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<String>,
    params: web::Json<CreateIssue>,
) -> Result<HttpResponse, ApiError> {
    let wk = path.into_inner();
    let data = service
        .issue_create(&session, &wk, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    get,
    path = "/api/v1/workspace/{wk}/issues",
    params(
        ("wk" = String, Path, description = "Workspace name"),
        IssueFilter,
        Pagination,
    ),
    responses(
        (status = 200, body = Vec<IssueResponse>),
        (status = 401, description = "Unauthorized"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn list_issues(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<String>,
    filter: web::Query<IssueFilter>,
    pagination: web::Query<Pagination>,
) -> Result<HttpResponse, ApiError> {
    let wk = path.into_inner();
    let data = service
        .issue_list(&session, &wk, filter.into_inner(), pagination.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    get,
    path = "/api/v1/workspace/{wk}/issues/{number}",
    params(IssuePath),
    responses(
        (status = 200, body = IssueResponse),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Issue not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn get_issue(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<IssuePath>,
) -> Result<HttpResponse, ApiError> {
    let IssuePath { wk, number } = path.into_inner();
    let data = service.issue_get(&session, &wk, number).await?;
    ok_json(data)
}
#[utoipa::path(
    put,
    path = "/api/v1/workspace/{wk}/issues/{number}",
    params(IssuePath),
    request_body = UpdateIssue,
    responses(
        (status = 200, body = IssueResponse),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Issue not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn update_issue(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<IssuePath>,
    params: web::Json<UpdateIssue>,
) -> Result<HttpResponse, ApiError> {
    let IssuePath { wk, number } = path.into_inner();
    let data = service
        .issue_update(&session, &wk, number, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    delete,
    path = "/api/v1/workspace/{wk}/issues/{number}",
    params(IssuePath),
    responses(
        (status = 200, description = "Issue deleted"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
        (status = 404, description = "Issue not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn delete_issue(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<IssuePath>,
) -> Result<HttpResponse, ApiError> {
    let IssuePath { wk, number } = path.into_inner();
    service.issue_delete(&session, &wk, number).await?;
    ok()
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/issues/{number}/close",
    params(IssuePath),
    responses(
        (status = 200, body = IssueResponse),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Issue not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn close_issue(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<IssuePath>,
) -> Result<HttpResponse, ApiError> {
    let IssuePath { wk, number } = path.into_inner();
    let data = service.issue_close(&session, &wk, number).await?;
    ok_json(data)
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/issues/{number}/reopen",
    params(IssuePath),
    responses(
        (status = 200, body = IssueResponse),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Issue not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn reopen_issue(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<IssuePath>,
) -> Result<HttpResponse, ApiError> {
    let IssuePath { wk, number } = path.into_inner();
    let data = service.issue_reopen(&session, &wk, number).await?;
    ok_json(data)
}
