package com.erp.dashboard.controller;

import com.erp.common.core.model.ApiResponse;
import com.erp.dashboard.dto.DashboardRequest;
import com.erp.dashboard.dto.DashboardResponse;
import com.erp.dashboard.service.DashboardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * Dashboard控制器
 * 
 * 提供租户仪表盘相关API
 */
@Slf4j
@RestController
@RequestMapping("/api/dashboard")
@RequiredArgsConstructor
@Tag(name = "Dashboard", description = "租户仪表盘接口")
public class DashboardController {

    private final DashboardService dashboardService;

    /**
     * 获取Dashboard首页数据
     * GET /api/dashboard
     *
     * @param recentDays 近N天数据（默认7天，可选1-90）
     * @return Dashboard响应数据
     */
    @GetMapping
    @Operation(summary = "获取Dashboard首页数据", description = "获取租户仪表盘首页数据，包括订单统计、API调用统计、配额使用情况、预警信息和近期订单")
    public ApiResponse<DashboardResponse> getDashboard(
            @Parameter(description = "查询近N天数据，默认7天，最大90天")
            @RequestParam(required = false) Integer recentDays) {
        
        // 从请求上下文获取租户ID（这里简化处理，实际应从Security Context获取）
        Long tenantId = getCurrentTenantId();
        
        DashboardResponse response = dashboardService.getDashboardData(tenantId, recentDays);
        
        return ApiResponse.success(response);
    }

    /**
     * 获取当前租户ID
     * 
     * 实际实现中应从JWT Token或Security Context中获取
     * 这里暂时返回固定值或从请求头获取
     */
    private Long getCurrentTenantId() {
        // TODO: 从Security Context获取当前租户ID
        // 暂时使用请求头中的X-Tenant-Id
        return 1L;
    }
}
