from __future__ import annotations

from pathlib import Path
from typing import Any

from flask import Flask, jsonify, send_from_directory
from flask_cors import CORS
from werkzeug.middleware.proxy_fix import ProxyFix

from .db import ensure_schema
from .routes import access_requests, account, admin_access, ai, auth_config, health, lab, monero


def create_app() -> Flask:
    app = Flask(__name__, static_folder="../frontend/dist", static_url_path="")
    app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_port=1)
    CORS(app, resources={r"/api/*": {"origins": "*"}})

    ensure_schema()

    health.register(app)
    auth_config.register(app)
    account.register(app)
    access_requests.register(app)
    admin_access.register(app)
    monero.register(app)
    lab.register(app)
    ai.register(app)

    @app.route("/", defaults={"path": ""})
    @app.route("/<path:path>")
    def serve_frontend(path: str) -> Any:
        dist_path = Path(app.static_folder)
        index_path = dist_path / "index.html"

        if dist_path.exists() and index_path.exists():
            target = dist_path / path
            if path and target.exists():
                return send_from_directory(app.static_folder, path)
            return send_from_directory(app.static_folder, "index.html")

        return jsonify(
            {
                "message": "Frontend not built yet. Run `npm install && npm run build` inside frontend/, then restart Flask.",
                "available_endpoints": ["/api/healthz", "/api/monero/get_info"],
            }
        )

    return app
