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.VppDrEvent;
import com.usky.vpp.domain.VppDrEvaluation;
import com.usky.vpp.domain.VppDrStrategy;
import com.usky.vpp.service.VppDrInvitationService;
import com.usky.vpp.service.VppDrService;
import com.usky.vpp.service.VppDrSubsidyPredictionService;
import com.usky.vpp.service.vo.DrClearingRequest;
import com.usky.vpp.service.vo.DrEventDetailVO;
import com.usky.vpp.service.vo.DrEventRequest;
import com.usky.vpp.service.vo.DrInterveneRequest;
import com.usky.vpp.service.vo.DrInvitationNotifyRequest;
import com.usky.vpp.service.vo.DrInvitationReplyRequest;
import com.usky.vpp.service.vo.DrInvitationRequest;
import com.usky.vpp.service.vo.DrInvitationVO;
import com.usky.vpp.service.vo.DrParticipateRequest;
import com.usky.vpp.service.vo.DrStrategyRequest;
import com.usky.vpp.service.vo.DrStrategyVO;
import com.usky.vpp.service.vo.DrSubsidyPredictionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 需求响应接口
 * 网关前缀: /prod-api/service-vpp/dr
 */
@RestController
@RequestMapping("/dr")
public class DrController {

    @Autowired
    private VppDrService vppDrService;
    @Autowired
    private VppDrInvitationService vppDrInvitationService;
    @Autowired
    private VppDrSubsidyPredictionService vppDrSubsidyPredictionService;

    @GetMapping(value = "/event")
    public ApiResult<CommonPage<VppDrEvent>> pageEvent(@RequestParam(required = false) Map<String, Object> params) {
        return ApiResult.success(vppDrService.pageEvent(params));
    }

    @GetMapping(value = "/event/{id}")
    public ApiResult<DrEventDetailVO> getEvent(@PathVariable("id") Long id) {
        return ApiResult.success(vppDrService.getEvent(id));
    }

    @PostMapping(value = "/event")
    public ApiResult<VppDrEvent> createEvent(@RequestBody DrEventRequest body) {
        return ApiResult.success(vppDrService.createEvent(body));
    }

    @PutMapping(value = "/event/{id}")
    public ApiResult<Void> updateEvent(@PathVariable("id") Long id, @RequestBody DrEventRequest body) {
        vppDrService.updateEvent(id, body);
        return ApiResult.success();
    }

    @DeleteMapping(value = "/event/{id}")
    public ApiResult<Void> deleteEvent(@PathVariable("id") Long id) {
        vppDrService.deleteEvent(id);
        return ApiResult.success();
    }

    @GetMapping(value = "/event/{id}/capability")
    public ApiResult<Object> assessCapability(@PathVariable("id") Long id) {
        return ApiResult.success(vppDrService.assessCapability(id));
    }

    @PostMapping(value = "/event/{id}/participate")
    public ApiResult<Void> participate(@PathVariable("id") Long id, @RequestBody DrParticipateRequest body) {
        vppDrService.participate(id, body);
        return ApiResult.success();
    }

    @PostMapping(value = "/event/{id}/clearing")
    public ApiResult<Void> clearing(@PathVariable("id") Long id, @RequestBody DrClearingRequest body) {
        vppDrService.clearing(id, body);
        return ApiResult.success();
    }

    @GetMapping(value = "/event/{id}/monitor")
    public ApiResult<Object> monitorExecution(@PathVariable("id") Long id) {
        return ApiResult.success(vppDrService.monitorExecution(id));
    }

    @PostMapping(value = "/event/{id}/intervene")
    public ApiResult<Void> intervene(@PathVariable("id") Long id, @RequestBody DrInterveneRequest body) {
        vppDrService.intervene(id, body);
        return ApiResult.success();
    }

    @PostMapping(value = "/event/{id}/ack-clearing")
    public ApiResult<Void> acknowledgeClearing(@PathVariable("id") Long id) {
        vppDrService.acknowledgeClearing(id);
        return ApiResult.success();
    }

    @PostMapping(value = "/event/{id}/complete")
    public ApiResult<Void> completeEvent(@PathVariable("id") Long id) {
        vppDrService.completeEvent(id);
        return ApiResult.success();
    }

    @GetMapping(value = "/event/{id}/evaluation")
    public ApiResult<VppDrEvaluation> getEvaluation(@PathVariable("id") Long id) {
        return ApiResult.success(vppDrService.getEvaluation(id));
    }

    @GetMapping(value = "/strategy")
    public ApiResult<CommonPage<VppDrStrategy>> pageStrategy(@RequestParam(required = false) Map<String, Object> params) {
        return ApiResult.success(vppDrService.pageStrategy(params));
    }

    @GetMapping(value = "/strategy/{id}")
    public ApiResult<DrStrategyVO> getStrategy(@PathVariable("id") Long id) {
        return ApiResult.success(vppDrService.getStrategy(id));
    }

    @PostMapping(value = "/strategy")
    public ApiResult<VppDrStrategy> createStrategy(@RequestBody DrStrategyRequest body) {
        return ApiResult.success(vppDrService.createStrategy(body));
    }

    @PutMapping(value = "/strategy/{id}")
    public ApiResult<Void> updateStrategy(@PathVariable("id") Long id, @RequestBody DrStrategyRequest body) {
        vppDrService.updateStrategy(id, body);
        return ApiResult.success();
    }

    @DeleteMapping(value = "/strategy/{id}")
    public ApiResult<Void> deleteStrategy(@PathVariable("id") Long id) {
        vppDrService.deleteStrategy(id);
        return ApiResult.success();
    }

    @GetMapping(value = "/invitation")
    public ApiResult<CommonPage<DrInvitationVO>> pageInvitation(@RequestParam(required = false) Map<String, Object> params) {
        return ApiResult.success(vppDrInvitationService.pageInvitation(params));
    }

    @GetMapping(value = "/invitation/{id}")
    public ApiResult<DrInvitationVO> getInvitation(@PathVariable("id") Long id) {
        return ApiResult.success(vppDrInvitationService.getInvitation(id));
    }

    @PostMapping(value = "/invitation")
    public ApiResult<DrInvitationVO> createInvitation(@RequestBody DrInvitationRequest body) {
        return ApiResult.success(vppDrInvitationService.createInvitation(body));
    }

    @PutMapping(value = "/invitation/{id}")
    public ApiResult<Void> updateInvitation(@PathVariable("id") Long id, @RequestBody DrInvitationRequest body) {
        vppDrInvitationService.updateInvitation(id, body);
        return ApiResult.success();
    }

    @DeleteMapping(value = "/invitation/{id}")
    public ApiResult<Void> deleteInvitation(@PathVariable("id") Long id) {
        vppDrInvitationService.deleteInvitation(id);
        return ApiResult.success();
    }

    @PostMapping(value = "/invitation/{id}/notify-sms")
    public ApiResult<DrInvitationVO> notifyInvitationSms(@PathVariable("id") Long id,
                                                         @RequestBody(required = false) DrInvitationNotifyRequest body) {
        return ApiResult.success(vppDrInvitationService.notifySms(id, body));
    }

    @PostMapping(value = "/invitation/{id}/reply")
    public ApiResult<DrInvitationVO> replyInvitation(@PathVariable("id") Long id,
                                                       @RequestBody DrInvitationReplyRequest body) {
        return ApiResult.success(vppDrInvitationService.replyInvitation(id, body));
    }

    // ==================== 补贴金额预测 ====================

    /**
     * 分页查询补贴金额预测
     * siteName  站点名称（模糊匹配）
     * startTime 响应开始时间起始
     * endTime   响应开始时间截止
     * current   页码
     * size      页大小
     */
    @GetMapping(value = "/subsidyPrediction")
    public ApiResult<CommonPage<DrSubsidyPredictionVO>> pageSubsidyPrediction(
            @RequestParam(value = "siteName", required = false) String siteName,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
            @RequestParam(value = "size", required = false, defaultValue = "20") Integer size) {
        return ApiResult.success(vppDrSubsidyPredictionService.page(siteName, startTime, endTime, current, size));
    }
}