use actix_web::{HttpResponse, web};
use serde::{Deserialize, Serialize};
use service::{
    AppService,
    workspace::join::{
        ApproveWorkspaceJoinApply, CreateWorkspaceJoinApply,
        ListWorkspaceJoinApply, UpdateWorkspaceJoinStrategy,
        WorkspaceJoinApplyResponse, WorkspaceJoinApprovalResponse,
        WorkspaceJoinStrategyResponse,
    },
};
use session::Session;

use crate::error::ApiError;

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

#[derive(Deserialize, utoipa::IntoParams)]
pub struct ApplyApprovePath {
    pub wk: String,
    pub username: String,
}
#[utoipa::path(
    get,
    path = "/api/v1/workspace/{wk}/join-strategy",
    params(
        ("wk" = String, Path, description = "Workspace name"),
    ),
    responses(
        (status = 200, body = WorkspaceJoinStrategyResponse),
        (status = 404, description = "Workspace not found"),
    )
)]
pub async fn join_strategy(
    service: web::Data<AppService>,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let wk = path.into_inner();
    let data = service.workspace_join_strategy(&wk).await?;
    ok_json(data)
}
#[utoipa::path(
    put,
    path = "/api/v1/workspace/{wk}/join-strategy",
    params(
        ("wk" = String, Path, description = "Workspace name"),
    ),
    request_body = UpdateWorkspaceJoinStrategy,
    responses(
        (status = 200, body = WorkspaceJoinStrategyResponse),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
        (status = 404, description = "Workspace not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn update_join_strategy(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<String>,
    params: web::Json<UpdateWorkspaceJoinStrategy>,
) -> Result<HttpResponse, ApiError> {
    let wk = path.into_inner();
    let data = service
        .workspace_update_join_strategy(&session, &wk, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/join/apply",
    params(
        ("wk" = String, Path, description = "Workspace name"),
    ),
    request_body = CreateWorkspaceJoinApply,
    responses(
        (status = 200, body = WorkspaceJoinApplyResponse),
        (status = 401, description = "Unauthorized"),
        (status = 409, description = "Already a member or pending application"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn apply_join(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<String>,
    params: web::Json<CreateWorkspaceJoinApply>,
) -> Result<HttpResponse, ApiError> {
    let wk = path.into_inner();
    let data = service
        .workspace_apply_join(&session, &wk, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    get,
    path = "/api/v1/workspace/join/my-applies",
    responses(
        (status = 200, body = Vec<WorkspaceJoinApplyResponse>),
        (status = 401, description = "Unauthorized"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn my_join_applies(
    session: Session,
    service: web::Data<AppService>,
) -> Result<HttpResponse, ApiError> {
    let data = service.workspace_my_join_applies(&session).await?;
    ok_json(data)
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/join/cancel",
    params(
        ("wk" = String, Path, description = "Workspace name"),
    ),
    responses(
        (status = 200, body = WorkspaceJoinApplyResponse),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Application not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn cancel_join(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let wk = path.into_inner();
    let data = service.workspace_cancel_join_apply(&session, &wk).await?;
    ok_json(data)
}
#[utoipa::path(
    get,
    path = "/api/v1/workspace/{wk}/join/applies",
    params(
        ("wk" = String, Path, description = "Workspace name"),
        ListWorkspaceJoinApply,
    ),
    responses(
        (status = 200, body = Vec<WorkspaceJoinApplyResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn list_join_applies(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<String>,
    query: web::Query<ListWorkspaceJoinApply>,
) -> Result<HttpResponse, ApiError> {
    let wk = path.into_inner();
    let data = service
        .workspace_join_applies(&session, &wk, query.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/join/applies/{username}/approve",
    params(ApplyApprovePath),
    request_body = ApproveWorkspaceJoinApply,
    responses(
        (status = 200, body = WorkspaceJoinApprovalResponse),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
        (status = 404, description = "Application not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn approve_join(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<ApplyApprovePath>,
    params: web::Json<ApproveWorkspaceJoinApply>,
) -> Result<HttpResponse, ApiError> {
    let ApplyApprovePath { wk, username } = path.into_inner();
    let data = service
        .workspace_approve_join_apply(
            &session,
            &wk,
            &username,
            params.into_inner(),
        )
        .await?;
    ok_json(data)
}
