from flask import Blueprint, g, abort

from src.lib.api import create_api_v2_not_found_error_response
from src.types.account import Account

from .accounts import get_accounts_count, get_account_list
from .account import get_target_account_overview, change_target_account_details

administrator_bp = Blueprint("administrator", __name__, url_prefix="/administrator")


@administrator_bp.before_request
def check_credentials():
    account: Account = g.get("account")

    if account.role != "administrator":
        return abort(create_api_v2_not_found_error_response())


administrator_bp.get("/accounts")(get_accounts_count)
administrator_bp.get("/accounts/<page>")(get_account_list)
administrator_bp.get("/account/<account_id>")(get_target_account_overview)
administrator_bp.patch("/account/<account_id>")(change_target_account_details)
