import os
from flask import send_from_directory
from flask_app import app, STATIC_DIR

# ── Import all controllers (they register routes on the app) ──
from controllers import *  # noqa: F401, E402

# ── Serve the frontend ──
@app.route("/")
def index():
    return send_from_directory(STATIC_DIR, "index.html")

@app.route("/<path:path>")
def serve_static(path):
    """Serve any static file; fall back to index.html for client-side navigation."""
    full_path = os.path.join(STATIC_DIR, path)
    if os.path.exists(full_path):
        return send_from_directory(STATIC_DIR, path)
    return send_from_directory(STATIC_DIR, "index.html")

if __name__ == "__main__":
    host = os.getenv("FLASK_HOST", "0.0.0.0")
    port = int(os.getenv("FLASK_PORT", 8080))
    debug = os.getenv("FLASK_DEBUG", "True").lower() == "true"
    use_reloader = os.getenv("FLASK_USE_RELOADER", "False").lower() == "true"

    print(f"\n{'='*45}")
    print(f"{'='*45}\n")

    app.run(debug=debug, use_reloader=use_reloader, host=host, port=port)
