package com.usky.vpp.controller.web;

import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.vpp.domain.VppResourcePoint;
import com.usky.vpp.service.VppResourceBoardService;
import com.usky.vpp.service.VppResourceOverviewService;
import com.usky.vpp.service.VppResourcePointService;
import com.usky.vpp.service.vo.AlarmLevelStatVO;
import com.usky.vpp.service.vo.EnergyTodayVO;
import com.usky.vpp.service.vo.ResourceOverviewRealtimeCurveVO;
import com.usky.vpp.service.vo.ResourceBoardDetailVO;
import com.usky.vpp.service.vo.ResourceBoardItemVO;
import com.usky.vpp.service.vo.ResourcePointListVO;
import com.usky.vpp.service.vo.ResourcePointRequest;
import com.usky.vpp.service.vo.ResourceTypeStatVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 资源管理接口
 * <p>网关前缀: /prod-api/service-vpp/resource</p>
 */
@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Autowired
    private VppResourcePointService vppResourcePointService;
    @Autowired
    private VppResourceBoardService vppResourceBoardService;
    @Autowired
    private VppResourceOverviewService vppResourceOverviewService;

    /**
     * 资源看板 - 实时运行数据列表
     * <p>展示所有资源点的实时功率、电压、电流、储能SOC、充电桩充电功率；
     * 支持按 resourceType（资源类型）、province/city/district（区域）、status（normal/abnormal）筛选；
     * abnormal=true 的资源需前端红色高亮。</p>
     *
     * @param params resourceType, province, city, district, status, pageNum, pageSize
     */
    @GetMapping("/board")
    public ApiResult<CommonPage<ResourceBoardItemVO>> resourceBoard(@RequestParam(required = false) Map<String, Object> params) {
        return ApiResult.success(vppResourceBoardService.pageBoard(params));
    }

    /**
     * 资源看板 - 资源点运行详情（异常资源点击查看）
     */
    @GetMapping("/board/{resourceId}")
    public ApiResult<ResourceBoardDetailVO> resourceBoardDetail(@PathVariable("resourceId") Long resourceId) {
        return ApiResult.success(vppResourceBoardService.getBoardDetail(resourceId));
    }

    /**
     * 资源概览 - 按资源类型统计
     * <p>统计各类型资源总数、总装机容量、总可调容量（is_control=1）。</p>
     */
    @GetMapping("/overview/type-stats")
    public ApiResult<List<ResourceTypeStatVO>> resourceOverviewTypeStats() {
        return ApiResult.success(vppResourceOverviewService.getTypeStats());
    }

    /**
     * 资源概览 - 今日电能概况
     * <p>总发电量、总用电量、绿电消纳比例（TSDB 今日 eday/epp 汇总）。</p>
     */
    @GetMapping("/overview/energy-today")
    public ApiResult<EnergyTodayVO> resourceOverviewEnergyToday() {
        return ApiResult.success(vppResourceOverviewService.getEnergyToday());
    }

    /**
     * 首页 - 实时曲线
     * <p>租户下设备按时间点 TSDB 汇总：实时负荷、光伏出力、储能 SOC。</p>
     *
     * @param startTime 开始时间 yyyy-MM-dd HH:mm:ss，默认当日 00:00:00
     * @param endTime   结束时间 yyyy-MM-dd HH:mm:ss，默认当前时刻
     */
    @GetMapping("/overview/realtime-curves")
    public ApiResult<ResourceOverviewRealtimeCurveVO> resourceOverviewRealtimeCurves(
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime) {
        return ApiResult.success(vppResourceOverviewService.getRealtimeCurves(startTime, endTime));
    }

//    /**
//     * 资源概览 - 实时告警统计
//     * <p>按紧急/重要/一般统计告警数量，含跳转告警列表路径。</p>
//     */
//    @GetMapping("/overview/alarm-stats")
//    public ApiResult<List<AlarmLevelStatVO>> resourceOverviewAlarmStats() {
//        return ApiResult.success(vppResourceOverviewService.getAlarmStats());
//    }

    /**
     * 资源点管理 - 分页列表
     * <p>支持按 siteName（站点名称）、deviceName（设备名称）、resourceType、siteId 搜索。</p>
     */
    @GetMapping
    public ApiResult<CommonPage<ResourcePointListVO>> pageResource(@RequestParam(required = false) Map<String, Object> params) {
        return ApiResult.success(vppResourcePointService.pageResourcePointList(params));
    }

    /**
     * 资源点管理 - 详情
     */
    @GetMapping("/{id}")
    public ApiResult<VppResourcePoint> getResource(@PathVariable("id") Long id) {
        return ApiResult.success(vppResourcePointService.getResourcePoint(id));
    }

    /**
     * 资源点管理 - 编辑
     */
    @PutMapping("/{id}")
    public ApiResult<Void> updateResource(@PathVariable("id") Long id, @RequestBody ResourcePointRequest body) {
        vppResourcePointService.updateResourcePoint(id, body);
        return ApiResult.success();
    }

    /**
     * 资源点管理 - 删除（软删除）
     */
    @DeleteMapping("/{id}")
    public ApiResult<Void> deleteResource(@PathVariable("id") Long id) {
        vppResourcePointService.deleteResourcePoint(id);
        return ApiResult.success();
    }
}
