"""
Soleprint - Overview and routing hub.

Development workflow and documentation system
👣 Mapping development footprints

Systems:
    💉 Artery (artery) - Todo lo vital
    🗺️ Atlas (atlas) - Documentación accionable
    🎛️ Station (station) - Monitores, Entornos y Herramientas

Routes:
    /                   → index
    /health             → health check
    /api/data/artery  → artery data
    /api/data/atlas  → atlas data
    /api/data/station  → station data
    /artery/*         → proxy to artery service
    /atlas/*         → proxy to atlas service
    /station/*         → proxy to station service
"""

import os
from pathlib import Path

# Import data functions
from dataloader import get_artery_data, get_atlas_data, get_station_data
from fastapi import FastAPI, Request
from fastapi.responses import RedirectResponse
from fastapi.templating import Jinja2Templates

app = FastAPI(title="Soleprint", version="0.1.0")

templates = Jinja2Templates(directory=Path(__file__).parent)

# Service URLs (internal for API calls)
ARTERY_URL = os.getenv("ARTERY_URL", "http://localhost:12001")
ATLAS_URL = os.getenv("ATLAS_URL", "http://localhost:12002")
STATION_URL = os.getenv("STATION_URL", "http://localhost:12003")

# External URLs (for frontend links, falls back to internal)
ARTERY_EXTERNAL_URL = os.getenv("ARTERY_EXTERNAL_URL", ARTERY_URL)
ATLAS_EXTERNAL_URL = os.getenv("ATLAS_EXTERNAL_URL", ATLAS_URL)
STATION_EXTERNAL_URL = os.getenv("STATION_EXTERNAL_URL", STATION_URL)


@app.get("/health")
def health():
    return {
        "status": "ok",
        "service": "soleprint",
        "subsystems": {
            "artery": ARTERY_URL,
            "atlas": ATLAS_URL,
            "station": STATION_URL,
        },
    }


# === Data API ===


@app.get("/api/data/artery")
def api_artery_data():
    """Data for artery service."""
    return get_artery_data()


@app.get("/api/data/atlas")
def api_atlas_data():
    """Data for atlas service."""
    return get_atlas_data()


@app.get("/api/data/station")
def api_station_data():
    """Data for station service."""
    return get_station_data()


@app.get("/")
def index(request: Request):
    return templates.TemplateResponse(
        "index.html",
        {
            "request": request,
            "artery": ARTERY_EXTERNAL_URL,
            "atlas": ATLAS_EXTERNAL_URL,
            "station": STATION_EXTERNAL_URL,
        },
    )


# === Cross-system redirects ===
# These allow soleprint to act as a hub, redirecting to subsystem routes


@app.get("/artery")
@app.get("/artery/{path:path}")
def artery_redirect(path: str = ""):
    """Redirect to artery service."""
    return RedirectResponse(url=f"{ARTERY_EXTERNAL_URL}/{path}")


@app.get("/atlas")
@app.get("/atlas/{path:path}")
def atlas_redirect(path: str = ""):
    """Redirect to atlas service."""
    return RedirectResponse(url=f"{ATLAS_EXTERNAL_URL}/{path}")


@app.get("/station")
@app.get("/station/{path:path}")
def station_redirect(path: str = ""):
    """Redirect to station service."""
    return RedirectResponse(url=f"{STATION_EXTERNAL_URL}/{path}")


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(
        "main:app",
        host="0.0.0.0",
        port=int(os.getenv("PORT", "12000")),
        reload=os.getenv("DEV", "").lower() in ("1", "true"),
    )
