from fastapi import APIRouter, Depends
from sqlalchemy import select, func
from app.core.database import get_db
from app.core.dependencies import require_permission
from app.models.customer import Customer
from app.models.transaction import Transaction
router=APIRouter(prefix="/dashboard",tags=["Dashboard"])

@router.get("")
async def dashboard(user=Depends(require_permission("reports.view")),db=Depends(get_db)):
    customers=await db.scalar(select(func.count(Customer.id)).where(Customer.brand_id==user.brand_id))
    tx_count=await db.scalar(select(func.count(Transaction.id)).where(Transaction.brand_id==user.brand_id))
    revenue=await db.scalar(select(func.coalesce(func.sum(Transaction.amount),0)).where(Transaction.brand_id==user.brand_id))
    points=await db.scalar(select(func.coalesce(func.sum(Transaction.points_earned),0)).where(Transaction.brand_id==user.brand_id))
    return {"success":True,"message":"Dashboard","data":{"total_customers":customers or 0,"active_customers":customers or 0,"total_transactions":tx_count or 0,"total_revenue":float(revenue or 0),"points_issued":points or 0}}
