from tools.base import tool
from services.supabase_client import get_supabase
@tool
async def get_coffee_spend(
user_id: str,
months: int = 3,
) -> dict:
"""
Returns the user's total spend at coffee shops over the last N months.
Args:
user_id: The authenticated user's UUID.
months: Number of months to look back (default 3).
Returns:
total: Total amount spent (SGD).
top_merchants: List of top coffee merchants with amounts.
"""
supabase = get_supabase()
cutoff = (datetime.now() - timedelta(days=months * 30)).isoformat()
rows = supabase.table("transactions") \
.select("amount, merchant") \
.eq("user_id", user_id) \
.eq("category", "Coffee & Beverages") \
.gte("created_at", cutoff) \
.execute()
total = sum(r["amount"] for r in rows.data)
# ... aggregate by merchant
return {"total": total, "top_merchants": merchants}