"""
Servizio reindirizzato: api.{domain}/marine/* (Traefik strips /marine prefix)
"""

import os
from contextlib import asynccontextmanager

from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

load_dotenv()

from routers import catalog, datasets, jobs


@asynccontextmanager
async def lifespan(app: FastAPI):
    api_url = os.getenv("API_SERVICE_URL", "http://api:3003")
    yield


app = FastAPI(
    title="MEB Marine Service",
    description="Copernicus Marine data download and dataset management for the MEB platform",
    version="1.0.0",
    docs_url="/docs",
    redoc_url="/redoc",
    lifespan=lifespan,
    root_path=""
)

app.add_middleware(
    CORSMiddleware,
    allow_origin_regex=r"https?://.*\.(localhost|mebboat\.it)(:\d+)?$",
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.include_router(catalog.router)
app.include_router(jobs.router)
app.include_router(datasets.router)


@app.get("/", tags=["health"])
async def root():
    return {"service": "MEB Marine Service", "version": "1.0.0", "docs": "/docs"}


@app.get("/health", tags=["health"])
async def health():
    return {"status": "healthy"}
