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

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

@router.get("/")
def get_cellsites() -> list[dict[Any, Any]]: # type: ignore
    conn = db.connect_to_db()
    try:
        with conn:
            return db.get_cellsites(conn)
    except Exception as e:
        logger.error(f"Error fetching cellsites: {e}", exc_info=True)
        return []
    finally:
        conn.close()


@router.get("/{id}")
def get_cellsite(id: int) -> list[dict[str, Any]]: # type: ignore
    conn = db.connect_to_db()
    try:
        with conn:
            return db.get_cellsite(conn, id)
    except Exception as e:
        logger.error(f"Error fetching cellsite {id}: {e}", exc_info=True)
        return []
    finally:
        conn.close()
