from flask import Flask, request

app = Flask(__name__)

_mode = "normal"
_annotations: list = []


def _fail():
    return _mode == "error"


@app.route("/annotations", methods=["POST"])
def annotations():
    if _fail():
        return "", 503
    _annotations.append(request.get_json(silent=True))
    return "", 200


@app.route("/auth/refresh", methods=["POST"])
def auth_refresh():
    if _fail():
        return "", 503
    return {"token": "refreshed-test-token"}


@app.route("/mock/config", methods=["POST"])
def mock_config():
    global _mode
    body = request.get_json(silent=True) or {}
    mode = body.get("mode", "normal")
    if mode not in ("normal", "error"):
        return "", 400
    _mode = mode
    return "", 200


@app.route("/mock/reset", methods=["POST"])
def mock_reset():
    global _mode, _annotations
    _mode = "normal"
    _annotations.clear()
    return "", 200


@app.route("/mock/status", methods=["GET"])
def mock_status():
    return {
        "mode": _mode,
        "annotation_count": len(_annotations),
        "annotations": list(_annotations),
    }


@app.route("/mock/annotations", methods=["GET"])
def mock_annotations_list():
    return {"annotations": list(_annotations)}
