from fastapi import FastAPI, Depends, HTTPException
from .auth import init_db, verify_api_key
from .models import Event, BatchEvents
from .enrichment import enrich_event
from .influx import write_event
import logging

logging.basicConfig(level=logging.INFO)
init_db()

app = FastAPI(title="Signal - Roblox Telemetry")

@app.get("/health")
async def health():
    return {"status": "ok"}

@app.post("/api/log")
async def ingest_event(payload: Event | BatchEvents, game: str = Depends(verify_api_key)):
    if isinstance(payload, BatchEvents):
        for event in payload.events:
            enriched = enrich_event(event, game)
            write_event(enriched)
    else:
        enriched = enrich_event(payload, game)
        write_event(enriched)
    return {"success": True}