import csv
from pathlib import Path
from agents import function_tool

STOCK = Path(__file__).parent.parent / "raw_data" / "stock.csv"

@function_tool
def check_stock(title: str, branch: str) -> dict:
    """Copies on hand for one title at one branch."""
    for row in csv.DictReader(STOCK.open()):
        if row["title"] == title and row["branch"] == branch:
            return {"copies": int(row["copies"]), "counted_at": row["counted_at"]}
    return {"error": "No stock record for that title and branch."}
