# services/sales/app/api/analytics.py
"""
Sales Analytics API - Reporting, statistics, and insights
"""

from fastapi import APIRouter, Depends, HTTPException, Query, Path
from typing import Optional, Dict, Any
from uuid import UUID
from datetime import datetime
import structlog

from app.services.sales_service import SalesService
from shared.routing import RouteBuilder
from shared.auth.access_control import analytics_tier_required
from shared.auth.decorators import get_current_user_dep

route_builder = RouteBuilder('sales')
router = APIRouter(tags=["sales-analytics"])
logger = structlog.get_logger()


def get_sales_service():
    """Dependency injection for SalesService"""
    return SalesService()


@router.get(
    route_builder.build_analytics_route("summary")
)
@analytics_tier_required
async def get_sales_analytics(
    tenant_id: UUID = Path(..., description="Tenant ID"),
    start_date: Optional[datetime] = Query(None, description="Start date filter"),
    end_date: Optional[datetime] = Query(None, description="End date filter"),
    current_user: Dict[str, Any] = Depends(get_current_user_dep),
    sales_service: SalesService = Depends(get_sales_service)
):
    """Get sales analytics summary for a tenant (Professional+ tier required)"""
    try:
        analytics = await sales_service.get_sales_analytics(tenant_id, start_date, end_date)

        logger.info("Retrieved sales analytics", tenant_id=tenant_id)
        return analytics

    except Exception as e:
        logger.error("Failed to get sales analytics", error=str(e), tenant_id=tenant_id)
        raise HTTPException(status_code=500, detail=f"Failed to get sales analytics: {str(e)}")
