from fastapi import APIRouter, Query
import database as db
from typing import Any
from logger import get_logger
logger = get_logger(__name__)

router = APIRouter(prefix="/alarms", tags=["alarms"])

@router.get("/")
def get_alarms(
    id: int | None = Query(None),
    before: int | None = Query(None)
) -> list[dict[Any, Any]]:
    conn = db.connect_to_db()
    try:
        return db.get_alarms(conn, id=id, before=before)
    except Exception as e:
        logger.error(f"Error fetching alarms (id={id}, before={before}): {e}", exc_info=True)
        return []
    finally:
        conn.close()