use actix_web::{HttpResponse, web};
use serde::{Deserialize, Serialize};
use service::{
    AppService,
    issues::{
        milestone::{CreateMilestone, SetIssueMilestone, UpdateMilestone},
        types::MilestoneResponse,
    },
};
use session::Session;
use uuid::Uuid;

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 MilestoneIdPath {
    pub wk: String,
    pub milestone_id: Uuid,
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/milestones",
    params(
        ("wk" = String, Path, description = "Workspace name"),
    ),
    request_body = CreateMilestone,
    responses(
        (status = 200, body = MilestoneResponse),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn create_milestone(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<String>,
    params: web::Json<CreateMilestone>,
) -> Result<HttpResponse, ApiError> {
    let wk = path.into_inner();
    let data = service
        .milestone_create(&session, &wk, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    get,
    path = "/api/v1/workspace/{wk}/milestones",
    params(
        ("wk" = String, Path, description = "Workspace name"),
    ),
    responses(
        (status = 200, body = Vec<MilestoneResponse>),
        (status = 401, description = "Unauthorized"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn list_milestones(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let wk = path.into_inner();
    let data = service.milestone_list(&session, &wk).await?;
    ok_json(data)
}
#[utoipa::path(
    put,
    path = "/api/v1/workspace/{wk}/milestones/{milestone_id}",
    params(MilestoneIdPath),
    request_body = UpdateMilestone,
    responses(
        (status = 200, body = MilestoneResponse),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
        (status = 404, description = "Milestone not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn update_milestone(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<MilestoneIdPath>,
    params: web::Json<UpdateMilestone>,
) -> Result<HttpResponse, ApiError> {
    let MilestoneIdPath { wk, milestone_id } = path.into_inner();
    let data = service
        .milestone_update(&session, &wk, milestone_id, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    delete,
    path = "/api/v1/workspace/{wk}/milestones/{milestone_id}",
    params(MilestoneIdPath),
    responses(
        (status = 200, description = "Milestone deleted"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
        (status = 404, description = "Milestone not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn delete_milestone(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<MilestoneIdPath>,
) -> Result<HttpResponse, ApiError> {
    let MilestoneIdPath { wk, milestone_id } = path.into_inner();
    service
        .milestone_delete(&session, &wk, milestone_id)
        .await?;
    ok()
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/issues/{number}/milestone",
    params(
        ("wk" = String, Path, description = "Workspace name"),
        ("number" = i64, Path, description = "Issue number"),
    ),
    request_body = SetIssueMilestone,
    responses(
        (status = 200, body = Option<MilestoneResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Issue or milestone not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn set_issue_milestone(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<(String, i64)>,
    params: web::Json<SetIssueMilestone>,
) -> Result<HttpResponse, ApiError> {
    let (wk, number) = path.into_inner();
    let data = service
        .issue_set_milestone(&session, &wk, number, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    delete,
    path = "/api/v1/workspace/{wk}/issues/{number}/milestone",
    params(
        ("wk" = String, Path, description = "Workspace name"),
        ("number" = i64, Path, description = "Issue number"),
    ),
    responses(
        (status = 200, description = "Milestone cleared"),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Issue not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn clear_issue_milestone(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<(String, i64)>,
) -> Result<HttpResponse, ApiError> {
    let (wk, number) = path.into_inner();
    service.issue_clear_milestone(&session, &wk, number).await?;
    ok()
}
