from flask import Blueprint, g, make_response, jsonify, abort, request

from src.lib.artist import get_unapproved_links_with_artists
from src.lib.artist import approve_unapproved_link_request, reject_unapproved_link_request
from src.types.account import Account

moderator_bp = Blueprint("administrator", __name__)


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

    if account.role != "moderator" and account.role != "administrator":
        return abort(code=404)


@moderator_bp.get("/moderator/tasks/creator_links")
def get_creator_links():
    links = get_unapproved_links_with_artists()

    response = make_response(jsonify(links), 200)

    return response


@moderator_bp.post("/moderator/creator_link_requests/<int:request_id>/approve")
def approve_request(request_id: int):
    approve_unapproved_link_request(request_id)
    response = make_response(jsonify({"response": "approved"}), 200)

    return response


@moderator_bp.post("/moderator/creator_link_requests/<int:request_id>/reject")
def reject_request(request_id: int):
    reject_unapproved_link_request(request_id)
    response = make_response(jsonify({"response": "rejected"}), 200)

    return response
