from fastapi import FastAPI
from backend.database.session import SessionLocal
from backend.database.models import Alert

app = FastAPI()


@app.get("/alerts.json")
def alerts():
    with SessionLocal() as db:
        rows = db.query(Alert).filter(Alert.active == True).all()

        return {
            "type": "FeatureCollection",
            "features": [
                {
                    "type": "Feature",
                    "id": r.id,
                    "geometry": None,  # add later if you store polygons
                    "properties": {
                        "event": r.get("event"),
                        "category": r.category,
                        "tier": r.tier,
                        "headline": r.headline,
                        "description": r.description,
                        "instruction": r.instruction,
                        "effective": str(r.effective),
                        "expires": str(r.expires),
                    },
                }
                for r in rows
            ],
        }
