package com.usky.vpp.controller.web;

import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.vpp.service.VppDrMonitorService;
import com.usky.vpp.service.vo.DrMonitorCurveVO;
import com.usky.vpp.service.vo.DrMonitorRecordVO;
import com.usky.vpp.service.vo.DrMonitorSummaryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 需求响应监测（响应记录）
 * <p>网关前缀: /prod-api/service-vpp/dr-monitor</p>
 */
@RestController
@RequestMapping("/drMonitor")
public class DrMonitorController {

    @Autowired
    private VppDrMonitorService drMonitorService;

    /**
     * 顶部 KPI：响应次数 / 响应容量(kW) / 历史达标率
     * <p>必传站点；先查站点下 is_support_peak=1 资源点，再按资源点平均。</p>
     */
    @GetMapping("/summary")
    public ApiResult<DrMonitorSummaryVO> summary(@RequestParam("startDate") String startDate,
                                                 @RequestParam("endDate") String endDate,
                                                 @RequestParam("siteIds") List<Long> siteIds) {
        return ApiResult.success(drMonitorService.getSummary(startDate, endDate, siteIds));
    }

    /**
     * 响应记录分页（字段对齐 vpp_dr_event）
     * <p>必传站点；仅统计可调峰资源点参与过的事件。</p>
     */
    @GetMapping("/records")
    public ApiResult<CommonPage<DrMonitorRecordVO>> records(@RequestParam("startDate") String startDate,
                                                            @RequestParam("endDate") String endDate,
                                                            @RequestParam("siteIds") List<Long> siteIds,
                                                            @RequestParam(required = false) Map<String, Object> params) {
        return ApiResult.success(drMonitorService.pageRecords(startDate, endDate, siteIds, params));
    }

    /**
     * 折线图：基线/实际负荷 + 申报/实际响应容量
     * <p>必传站点；仅 is_support_peak=1 资源点；曲线按资源点加权平均。</p>
     */
    @GetMapping("/records/{id}/curve")
    public ApiResult<DrMonitorCurveVO> curve(@PathVariable("id") Long id,
                                             @RequestParam("siteIds") List<Long> siteIds) {
        return ApiResult.success(drMonitorService.getCurve(id, siteIds));
    }

    // private List<Long> parseSiteIds(List<String> raw) {
    //     if (raw == null || raw.isEmpty()) {
    //         return Collections.emptyList();
    //     }
    //     List<Long> ids = new ArrayList<>();
    //     for (String item : raw) {
    //         if (!StringUtils.hasText(item)) {
    //             continue;
    //         }
    //         Arrays.stream(item.split(","))
    //                 .map(String::trim)
    //                 .filter(StringUtils::hasText)
    //                 .forEach(token -> ids.add(Long.parseLong(token)));
    //     }
    //     return ids.stream().distinct().collect(Collectors.toList());
    // }
}
