use actix_web::{HttpResponse, web};
use serde::{Deserialize, Serialize};
use service::{
    AppService, Pagination,
    workspace::{
        member::{AddWorkspaceMember, UpdateWorkspaceMember},
        types::WorkspaceMemberResponse,
    },
};
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 MemberPath {
    pub wk: String,
    pub username: String,
}
#[utoipa::path(
    get,
    path = "/api/v1/workspace/{wk}/members",
    params(
        ("wk" = String, Path, description = "Workspace name"),
        Pagination,
    ),
    responses(
        (status = 200, body = Vec<WorkspaceMemberResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Workspace not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn list_members(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<String>,
    pagination: web::Query<Pagination>,
) -> Result<HttpResponse, ApiError> {
    let wk = path.into_inner();
    let data = service
        .workspace_members(&session, &wk, pagination.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/members",
    params(
        ("wk" = String, Path, description = "Workspace name"),
    ),
    request_body = AddWorkspaceMember,
    responses(
        (status = 200, body = WorkspaceMemberResponse),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn add_member(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<String>,
    params: web::Json<AddWorkspaceMember>,
) -> Result<HttpResponse, ApiError> {
    let wk = path.into_inner();
    let data = service
        .workspace_add_member(&session, &wk, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    put,
    path = "/api/v1/workspace/{wk}/members/{username}",
    params(MemberPath),
    request_body = UpdateWorkspaceMember,
    responses(
        (status = 200, body = WorkspaceMemberResponse),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn update_member(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<MemberPath>,
    params: web::Json<UpdateWorkspaceMember>,
) -> Result<HttpResponse, ApiError> {
    let MemberPath { wk, username } = path.into_inner();
    let data = service
        .workspace_update_member(&session, &wk, &username, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    delete,
    path = "/api/v1/workspace/{wk}/members/{username}",
    params(MemberPath),
    responses(
        (status = 200, description = "Member removed"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn remove_member(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<MemberPath>,
) -> Result<HttpResponse, ApiError> {
    let MemberPath { wk, username } = path.into_inner();
    service
        .workspace_remove_member(&session, &wk, &username)
        .await?;
    ok()
}
