use crate::{ApiResponse, error::ApiError};
use actix_web::{HttpResponse, Result, web};
use service::AppService;
use session::Session;

#[utoipa::path(
    post,
    path = "/api/users/me/keys",
    request_body = service::user::ssh_key::AddSshKeyParams,
    responses(
        (status = 200, description = "Add SSH key", body = ApiResponse<service::user::ssh_key::SshKeyResponse>),
        (status = 401, description = "Unauthorized"),
),
    tag = "User"
)]
pub async fn add_ssh_key(
    service: web::Data<AppService>,
    session: Session,
    body: web::Json<service::user::ssh_key::AddSshKeyParams>,
) -> Result<HttpResponse, ApiError> {
    let resp = service
        .user_add_ssh_key(&session, body.into_inner())
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/users/me/keys",
    responses(
        (status = 200, description = "List SSH keys", body = ApiResponse<service::user::ssh_key::SshKeyListResponse>),
        (status = 401, description = "Unauthorized"),
),
    tag = "User"
)]
pub async fn list_ssh_keys(
    service: web::Data<AppService>,
    session: Session,
) -> Result<HttpResponse, ApiError> {
    let resp = service.user_list_ssh_keys(&session).await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    get,
    path = "/api/users/me/keys/{key_id}",
    params(("key_id" = i64, Path)),
    responses(
        (status = 200, description = "Get SSH key", body = ApiResponse<service::user::ssh_key::SshKeyResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "User"
)]
pub async fn get_ssh_key(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<i64>,
) -> Result<HttpResponse, ApiError> {
    let resp = service
        .user_get_ssh_key(&session, path.into_inner())
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    patch,
    path = "/api/users/me/keys/{key_id}",
    params(("key_id" = i64, Path)),
    request_body = service::user::ssh_key::UpdateSshKeyParams,
    responses(
        (status = 200, description = "Update SSH key", body = ApiResponse<service::user::ssh_key::SshKeyResponse>),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "User"
)]
pub async fn update_ssh_key(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<i64>,
    body: web::Json<service::user::ssh_key::UpdateSshKeyParams>,
) -> Result<HttpResponse, ApiError> {
    let resp = service
        .user_update_ssh_key(&session, path.into_inner(), body.into_inner())
        .await?;
    Ok(ApiResponse::ok(resp).to_response())
}

#[utoipa::path(
    delete,
    path = "/api/users/me/keys/{key_id}",
    params(("key_id" = i64, Path)),
    responses(
        (status = 200, description = "Delete SSH key"),
        (status = 401, description = "Unauthorized"),
        (status = 404, description = "Not found"),
),
    tag = "User"
)]
pub async fn delete_ssh_key(
    service: web::Data<AppService>,
    session: Session,
    path: web::Path<i64>,
) -> Result<HttpResponse, ApiError> {
    service
        .user_delete_ssh_key(&session, path.into_inner())
        .await?;
    Ok(ApiResponse::ok(serde_json::json!({ "success": true })).to_response())
}
