"""B8回收站查询和单条文档恢复HTTP接口。"""

from __future__ import annotations

from flask import request

from dms.api.v1.blueprint import api_v1
from dms.common.errors import InvalidArgumentError
from dms.common.response import success_response
from dms.security.decorators import admin_required, bearer_auth_required
from dms.services.document_query_service import parse_string_id
from dms.services.recycle_bin_service import (
    list_recycle_bin_documents,
    restore_document,
)


def _json_body() -> dict:
    if not request.is_json:
        raise InvalidArgumentError("请求体必须是JSON对象")
    payload = request.get_json(silent=True)
    if not isinstance(payload, dict):
        raise InvalidArgumentError("请求体必须是JSON对象")
    return payload


@api_v1.get("/recycle-bin/documents")
@bearer_auth_required
@admin_required
def recycle_bin_documents_route():
    return success_response(list_recycle_bin_documents(request.args))


@api_v1.post("/recycle-bin/documents/<id>/restore")
@bearer_auth_required
@admin_required
def restore_recycle_bin_document_route(id: str):
    return success_response(
        restore_document(parse_string_id(id), _json_body())
    )


__all__ = [
    "recycle_bin_documents_route",
    "restore_recycle_bin_document_route",
]
