"""
Dashboard router — HWUI-001
All endpoints scoped to the authenticated merchant via get_current_merchant().
"""
from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session

from src.apps.dashboard.schemas import (
    DateRangeEnum,
    DashboardSummaryResponse,
    RevenueTrendResponse,
    PaymentMethodsChartResponse,
    TransactionStatusChartResponse,
    TopCustomersChartResponse,
    ActionItemsResponse,
)
from src.apps.dashboard import services
from src.apps.auth.utils.auth import get_current_merchant
from src.core.database import get_db

router = APIRouter()


@router.get("/summary", response_model=DashboardSummaryResponse)
async def dashboard_summary(
    range: DateRangeEnum = Query(DateRangeEnum.LAST_7_DAYS),
    merchant=Depends(get_current_merchant),
    db: Session = Depends(get_db),
):
    return services.get_dashboard_summary(db=db, merchant_id=merchant.id, range_val=range)


@router.get("/charts/revenue-trend", response_model=RevenueTrendResponse)
async def revenue_trend(
    range: DateRangeEnum = Query(DateRangeEnum.LAST_7_DAYS),
    merchant=Depends(get_current_merchant),
    db: Session = Depends(get_db),
):
    return services.get_revenue_trend(db=db, merchant_id=merchant.id, range_val=range)


@router.get("/charts/payment-methods", response_model=PaymentMethodsChartResponse)
async def payment_methods_chart(
    range: DateRangeEnum = Query(DateRangeEnum.LAST_7_DAYS),
    merchant=Depends(get_current_merchant),
    db: Session = Depends(get_db),
):
    return services.get_payment_methods_chart(db=db, merchant_id=merchant.id, range_val=range)


@router.get("/charts/transaction-status", response_model=TransactionStatusChartResponse)
async def transaction_status_chart(
    range: DateRangeEnum = Query(DateRangeEnum.LAST_7_DAYS),
    merchant=Depends(get_current_merchant),
    db: Session = Depends(get_db),
):
    return services.get_transaction_status_chart(db=db, merchant_id=merchant.id, range_val=range)


@router.get("/charts/top-customers", response_model=TopCustomersChartResponse)
async def top_customers_chart(
    range: DateRangeEnum = Query(DateRangeEnum.LAST_7_DAYS),
    limit: int = Query(10, ge=1, le=50),
    merchant=Depends(get_current_merchant),
    db: Session = Depends(get_db),
):
    return services.get_top_customers_chart(
        db=db, merchant_id=merchant.id, range_val=range, limit=limit
    )


@router.get("/action-items", response_model=ActionItemsResponse)
async def action_items(
    range: DateRangeEnum = Query(DateRangeEnum.LAST_7_DAYS),
    merchant=Depends(get_current_merchant),
    db: Session = Depends(get_db),
):
    return services.get_action_items(db=db, merchant_id=merchant.id, range_val=range)
