use actix_web::{HttpResponse, web};
use serde::{Deserialize, Serialize};
use service::{
    AppService,
    workspace::{
        group::{CreateWorkspaceGroup, UpdateWorkspaceGroup},
        types::{WorkspaceGroupResponse, 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 GroupPath {
    pub wk: String,
    pub group_name: String,
}

#[derive(Deserialize, utoipa::IntoParams)]
pub struct GroupMemberPath {
    pub wk: String,
    pub group_name: String,
    pub username: String,
}
#[utoipa::path(
    get,
    path = "/api/v1/workspace/{wk}/groups",
    params(
        ("wk" = String, Path, description = "Workspace name"),
    ),
    responses(
        (status = 200, body = Vec<WorkspaceGroupResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Workspace not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn list_groups(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<String>,
) -> Result<HttpResponse, ApiError> {
    let wk = path.into_inner();
    let data = service.workspace_groups(&session, &wk).await?;
    ok_json(data)
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/groups",
    params(
        ("wk" = String, Path, description = "Workspace name"),
    ),
    request_body = CreateWorkspaceGroup,
    responses(
        (status = 200, body = WorkspaceGroupResponse),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn create_group(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<String>,
    params: web::Json<CreateWorkspaceGroup>,
) -> Result<HttpResponse, ApiError> {
    let wk = path.into_inner();
    let data = service
        .workspace_create_group(&session, &wk, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    put,
    path = "/api/v1/workspace/{wk}/groups/{group_name}",
    params(GroupPath),
    request_body = UpdateWorkspaceGroup,
    responses(
        (status = 200, body = WorkspaceGroupResponse),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
        (status = 404, description = "Group not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn update_group(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<GroupPath>,
    params: web::Json<UpdateWorkspaceGroup>,
) -> Result<HttpResponse, ApiError> {
    let GroupPath { wk, group_name } = path.into_inner();
    let data = service
        .workspace_update_group(&session, &wk, &group_name, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    delete,
    path = "/api/v1/workspace/{wk}/groups/{group_name}",
    params(GroupPath),
    responses(
        (status = 200, description = "Group deleted"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
        (status = 404, description = "Group not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn delete_group(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<GroupPath>,
) -> Result<HttpResponse, ApiError> {
    let GroupPath { wk, group_name } = path.into_inner();
    service
        .workspace_delete_group(&session, &wk, &group_name)
        .await?;
    ok()
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/groups/{group_name}/members/{username}",
    params(GroupMemberPath),
    responses(
        (status = 200, description = "Member added to group"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn add_group_member(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<GroupMemberPath>,
) -> Result<HttpResponse, ApiError> {
    let GroupMemberPath {
        wk,
        group_name,
        username,
    } = path.into_inner();
    service
        .workspace_add_group_member(&session, &wk, &group_name, &username)
        .await?;
    ok()
}
#[utoipa::path(
    delete,
    path = "/api/v1/workspace/{wk}/groups/{group_name}/members/{username}",
    params(GroupMemberPath),
    responses(
        (status = 200, description = "Member removed from group"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn remove_group_member(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<GroupMemberPath>,
) -> Result<HttpResponse, ApiError> {
    let GroupMemberPath {
        wk,
        group_name,
        username,
    } = path.into_inner();
    service
        .workspace_remove_group_member(&session, &wk, &group_name, &username)
        .await?;
    ok()
}
#[utoipa::path(
    get,
    path = "/api/v1/workspace/{wk}/groups/{group_name}/members",
    params(GroupPath),
    responses(
        (status = 200, body = Vec<WorkspaceMemberResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Group not found"),
    ),
    security(
        ("session" = [])
    )
)]
pub async fn list_group_members(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<GroupPath>,
) -> Result<HttpResponse, ApiError> {
    let GroupPath { wk, group_name } = path.into_inner();
    let data = service
        .workspace_group_members(&session, &wk, &group_name)
        .await?;
    ok_json(data)
}
