use actix_web::{HttpResponse, web};
use serde::{Deserialize, Serialize};
use service::{
    AppService, Pagination,
    git::protect::{CreateProtect, ProtectResponse, UpdateProtect},
};
use session::Session;

use crate::error::ApiError;

use super::repo::WkRepoPath;

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 ProtectPath {
    pub wk: String,
    pub repo: String,
    pub protect_id: uuid::Uuid,
}
#[utoipa::path(
    get,
    path = "/api/v1/workspace/{wk}/repos/{repo}/protect",
    params(WkRepoPath, Pagination),
    responses(
        (status = 200, body = Vec<ProtectResponse>),
        (status = 401, description = "Unauthorized"),
    ),
    security(("session" = []))
)]
pub async fn list_protects(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<WkRepoPath>,
    pagination: web::Query<Pagination>,
) -> Result<HttpResponse, ApiError> {
    let WkRepoPath { wk, repo } = path.into_inner();
    let data = service
        .repo_protect_list(&session, &wk, &repo, pagination.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    post,
    path = "/api/v1/workspace/{wk}/repos/{repo}/protect",
    params(WkRepoPath),
    request_body = CreateProtect,
    responses(
        (status = 200, body = ProtectResponse),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
    ),
    security(("session" = []))
)]
pub async fn create_protect(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<WkRepoPath>,
    params: web::Json<CreateProtect>,
) -> Result<HttpResponse, ApiError> {
    let WkRepoPath { wk, repo } = path.into_inner();
    let data = service
        .repo_protect_create(&session, &wk, &repo, params.into_inner())
        .await?;
    ok_json(data)
}
#[utoipa::path(
    put,
    path = "/api/v1/workspace/{wk}/repos/{repo}/protect/{protect_id}",
    params(ProtectPath),
    request_body = UpdateProtect,
    responses(
        (status = 200, body = ProtectResponse),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
        (status = 404, description = "Rule not found"),
    ),
    security(("session" = []))
)]
pub async fn update_protect(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<ProtectPath>,
    params: web::Json<UpdateProtect>,
) -> Result<HttpResponse, ApiError> {
    let ProtectPath {
        wk,
        repo,
        protect_id,
    } = path.into_inner();
    let data = service
        .repo_protect_update(
            &session,
            &wk,
            &repo,
            protect_id,
            params.into_inner(),
        )
        .await?;
    ok_json(data)
}
#[utoipa::path(
    delete,
    path = "/api/v1/workspace/{wk}/repos/{repo}/protect/{protect_id}",
    params(ProtectPath),
    responses(
        (status = 200, description = "Rule deleted"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Permission denied"),
        (status = 404, description = "Rule not found"),
    ),
    security(("session" = []))
)]
pub async fn delete_protect(
    session: Session,
    service: web::Data<AppService>,
    path: web::Path<ProtectPath>,
) -> Result<HttpResponse, ApiError> {
    let ProtectPath {
        wk,
        repo,
        protect_id,
    } = path.into_inner();
    service
        .repo_protect_delete(&session, &wk, &repo, protect_id)
        .await?;
    ok()
}
