from fastapi import APIRouter, HTTPException from app.core.database import execute_query from typing import Dict, Any, List import logging logger = logging.getLogger(__name__) router = APIRouter() @router.get("/stats", response_model=Dict[str, Any]) async def get_dashboard_stats(): """ Get aggregated statistics for the dashboard """ try: logger.info("📊 Fetching dashboard stats...") # 1. Customer Counts logger.info("Fetching customer count...") customer_res = execute_query("SELECT COUNT(*) as count FROM customers WHERE deleted_at IS NULL", fetchone=True) customer_count = customer_res['count'] if customer_res else 0 # 2. Contact Counts logger.info("Fetching contact count...") contact_res = execute_query("SELECT COUNT(*) as count FROM contacts", fetchone=True) contact_count = contact_res['count'] if contact_res else 0 # 3. Vendor Counts logger.info("Fetching vendor count...") vendor_res = execute_query("SELECT COUNT(*) as count FROM vendors", fetchone=True) vendor_count = vendor_res['count'] if vendor_res else 0 # 4. Recent Customers (Real "Activity") logger.info("Fetching recent customers...") recent_customers = execute_query(""" SELECT id, name, created_at, 'customer' as type FROM customers WHERE deleted_at IS NULL ORDER BY created_at DESC LIMIT 5 """) # 5. Vendor Categories Distribution logger.info("Fetching vendor distribution...") vendor_categories = execute_query(""" SELECT category, COUNT(*) as count FROM vendors GROUP BY category """) logger.info("✅ Dashboard stats fetched successfully") return { "counts": { "customers": customer_count, "contacts": contact_count, "vendors": vendor_count }, "recent_activity": recent_customers or [], "vendor_distribution": vendor_categories or [], "system_status": "online" } except Exception as e: logger.error(f"❌ Error fetching dashboard stats: {e}", exc_info=True) raise HTTPException(status_code=500, detail=str(e))