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

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

@router.get("/")
def get_robots():
    conn = db.connect_to_db()
    try:
        return db.get_robots(conn)
    except Exception as e:
        logger.error(f"Error fetching robots: {e}", exc_info=True)
        return []
    finally:
        conn.close()

@router.get("/{robot_id}")
def get_robot(robot_id: int):
    conn = db.connect_to_db()
    try:
        return db.get_robot_by_id(conn, robot_id)
    except Exception as e:
        logger.error(f"Error fetching robot {robot_id}: {e}", exc_info=True)
        return None
    finally:
        conn.close()

# TODO: Add worklog + history to view when in historical mode in FE