# Created by aaronkueh on 9/30/2025
# aom/web_server.py
from __future__ import annotations

from pathlib import Path
import os
from aom.definitions import CONFIG_DIR
from fastapi import FastAPI
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles

# Reuse your existing API app
from aom.aom_api import app as api_app

# ---- Shared static dir ----
STATIC_DIR = Path(__file__).parent / "webui"
STATIC_DIR.mkdir(parents=True, exist_ok=True)


# ---- Dark app (port 8082) ----
app_dark = FastAPI(title="AOM Web UI (Dark) + API", version="1.0.0")
app_dark.mount("/api", api_app)
app_dark.mount("/assets", StaticFiles(directory=STATIC_DIR), name="assets")
app_dark.mount("/config", StaticFiles(directory=CONFIG_DIR), name="config")

@app_dark.get("/", include_in_schema=False)
def index_dark():
    return FileResponse(STATIC_DIR / "index_dark.html")

# ---- Light app (port 8081) ----
app_light = FastAPI(title="AOM Web UI (Light) + API", version="1.0.0")
app_light.mount("/api", api_app)
app_light.mount("/assets", StaticFiles(directory=STATIC_DIR), name="assets")
app_light.mount("/config", StaticFiles(directory=CONFIG_DIR), name="config")

@app_light.get("/", include_in_schema=False)
def index_light():
    return FileResponse(STATIC_DIR / "index_light.html")


# ---- Normal app (port 8080) ----
app = FastAPI(title="AOM Web + API", version="1.0.0")
app.mount("/api", api_app)
app.mount("/assets", StaticFiles(directory=STATIC_DIR), name="assets")
app.mount("/config", StaticFiles(directory=CONFIG_DIR), name="config")

@app.get("/", include_in_schema=False)
def index():
    return FileResponse(STATIC_DIR / "index.html")

if __name__ == "__main__":
    import asyncio
    import uvicorn

    async def dual_main():
        cfg_dark = uvicorn.Config("aom.web_server:app_dark", host="0.0.0.0", port=8082, reload=False, loop="asyncio")
        cfg_light = uvicorn.Config("aom.web_server:app_light", host="0.0.0.0", port=8081, reload=False, loop="asyncio")
        cfg_main = uvicorn.Config("aom.web_server:app", host="0.0.0.0", port=8080, reload=False, loop="asyncio")

        server_dark = uvicorn.Server(cfg_dark)
        server_light = uvicorn.Server(cfg_light)
        server_main = uvicorn.Server(cfg_main)

        await asyncio.gather(
            server_dark.serve(),
            server_light.serve(),
            server_main.serve()
        )

    print(" ")
    print("                                      _oo0oo_")
    print("                                     o8888888o")
    print("                                     88\" . \"88")
    print("                                     (| -_- |)")
    print("                                     0\\  =  /0")
    print("                                   ___/‘---’\\___")
    print("                                .‘ \\|       |/  ‘.")
    print("                               / \\|||  :   |||/   \\")
    print("                              / _||||| -卍- |||||_  \\")
    print("                             |   | \\\\\\  -  /// |   |")
    print("                             | \\_|  ‘‘\\---/‘‘  |_/ |")
    print("                             \\  .-\\__  ‘-‘  ___/-. /")
    print("                           ___‘. .‘  /--.--\\  ‘. .’___")
    print("                        .\"\" ‘<  ‘.___\\_<|>_/___.’ >’ \"\".")
    print("                       | | :  `- \\‘.;‘\\ _ /‘;.’/ - ` : | |")
    print("                       \\  \\ ‘_.   \\_ __\\ /__ _/   .-’ /  /")
    print("                   =====‘-.____‘.___ \\_____/___.-’___.-’===== ")
    print("                                      `=---=‘")
    print(" ")
    print("                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    print(" ")
    print("                            佛祖保佑         代码无BUG")
    print(" ")
    asyncio.run(dual_main())
