# -*- coding: utf-8 -*-
"""
小程序客户路由 —— 客户详情（CUST-1）、客户服务记录（CUST-2）。

端点清单：
- GET  /api/xcx/customers/{customer_id}          — 客户详情（CUST-1）
- GET  /api/xcx/customers/{customer_id}/records   — 客户服务记录（CUST-2）

所有端点均需 JWT（approved 状态）。
"""

from __future__ import annotations

from fastapi import APIRouter, Depends, Query

from app.auth.dependencies import CurrentUser
from app.middleware.permission import require_approved, require_permission
from app.schemas.xcx_customers import (
    CustomerDetailResponse,
    CustomerRecordsResponse,
    CustomerConsumptionRecordsResponse,
)
from app.services import customer_service
from app.trace.decorators import trace_service

router = APIRouter(prefix="/api/xcx/customers", tags=["小程序客户"])


@router.get("/{customer_id}", response_model=CustomerDetailResponse)
@trace_service("获取客户详情", "Get customer detail")
async def get_customer_detail(
    customer_id: int,
    # CHANGE 2026-03-27 | 权限改造 W4：客户详情跟客户看板走
    user: CurrentUser = Depends(require_permission("view_board_customer")),
):
    """客户详情（CUST-1）。"""
    return await customer_service.get_customer_detail(
        customer_id, user.site_id
    )


@router.get("/{customer_id}/records", response_model=CustomerRecordsResponse)
@trace_service("获取客户服务记录", "Get customer records")
async def get_customer_records(
    customer_id: int,
    year: int = Query(...),
    month: int = Query(..., ge=1, le=12),
    table: str | None = Query(None),
    page: int = Query(1, ge=1),
    page_size: int = Query(20, ge=1, le=100),
    user: CurrentUser = Depends(require_permission("view_board_customer")),
):
    """客户服务记录（CUST-2）。"""
    return await customer_service.get_customer_records(
        customer_id, user.site_id, user.user_id,
        year, month, table, page, page_size,
    )


@router.get("/{customer_id}/consumption-records", response_model=CustomerConsumptionRecordsResponse)
@trace_service("获取客户消费记录", "Get customer consumption records")
async def get_customer_consumption_records(
    customer_id: int,
    year: int = Query(...),
    month: int = Query(..., ge=1, le=12),
    user: CurrentUser = Depends(require_permission("view_board_customer")),
):
    """客户消费记录（CUST-3）。"""
    return await customer_service.get_customer_consumption_records(
        customer_id, user.site_id, year, month,
    )
