package com.erp.tenant.controller.tenant;

import com.erp.common.core.model.ApiResponse;
import com.erp.tenant.config.TenantContext;
import com.erp.tenant.dto.tenant.DashboardResponse;
import com.erp.tenant.service.tenant.TenantDashboardService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 租户端Dashboard控制器
 * 功能：租户端首页/Dashboard
 */
@Slf4j
@RestController
@RequestMapping("/api/tenant/dashboard")
@RequiredArgsConstructor
public class TenantDashboardController {

    private final TenantDashboardService dashboardService;

    /**
     * 获取Dashboard首页数据
     * GET /api/tenant/dashboard
     *
     * @param recentDays 近期天数，默认7天
     */
    @GetMapping
    public ApiResponse<DashboardResponse> getDashboard(
            @RequestParam(required = false, defaultValue = "7") Integer recentDays) {

        Long tenantId = TenantContext.getCurrentTenantId();
        if (tenantId == null) {
            return ApiResponse.error(401, "未获取到租户信息");
        }

        DashboardResponse dashboard = dashboardService.getDashboard(tenantId, recentDays);
        return ApiResponse.success(dashboard);
    }

    /**
     * 获取预警摘要
     * GET /api/tenant/dashboard/alerts
     */
    @GetMapping("/alerts")
    public ApiResponse<?> getAlertSummary() {
        Long tenantId = TenantContext.getCurrentTenantId();
        if (tenantId == null) {
            return ApiResponse.error(401, "未获取到租户信息");
        }
        return ApiResponse.success(dashboardService.buildAlerts(tenantId));
    }
}
