package com.zksy.web.controller.dispatch;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.common.annotation.Anonymous;
import com.zksy.common.annotation.Log;
import com.zksy.common.core.domain.AjaxResult;
import com.zksy.common.enums.BusinessType;
import com.zksy.dispatch.domain.DisEvent;
import com.zksy.dispatch.domain.DisEventTrack;
import com.zksy.dispatch.domain.DisEventTransfer;
import com.zksy.dispatch.dto.DisEventPageDTO;
import com.zksy.dispatch.dto.DisEventVO;
import com.zksy.dispatch.dto.EventStatsVO;
import com.zksy.dispatch.service.DisEventService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 应急事件统一档案接口（全生命周期闭环）
 * 接报 -> 受理 -> 核实 -> 转接/报送 -> 跟踪/催办 -> 解除 -> 关闭
 */
@Slf4j
@RestController
@RequestMapping("/api/dispatch/event")
@Api(tags = "应急事件统一档案（闭环）")
public class DisEventController {

    @Autowired
    private DisEventService service;

    @GetMapping("/findByPage")
    @ApiOperation("统一事件分页查询（多条件+状态过滤）")
    @Anonymous
    public AjaxResult findByPage(DisEventPageDTO dto) {
        Page<DisEventVO> page = service.findByPage(dto);
        return AjaxResult.success(page);
    }

    @GetMapping("/getDetail/{id}")
    @ApiOperation("事件详情（主表信息+各环节明细）")
    @Anonymous
    public AjaxResult getDetail(@PathVariable Long id) {
        return AjaxResult.success(service.getDetail(id));
    }

    @GetMapping("/stats")
    @ApiOperation("多维条件统计（事件总数/损失合计/平均损失/重大及以上事件数）")
    @Anonymous
    public AjaxResult stats(DisEventPageDTO dto) {
        EventStatsVO stats = service.stats(dto);
        return AjaxResult.success(stats);
    }

    @PostMapping("/saveReport")
    @ApiOperation("新增接报（自动生成事件编号，创建统一事件主表）")
    @Log(title = "新增事件接报", businessType = BusinessType.INSERT)
    public AjaxResult saveReport(@RequestBody DisEvent event) {
        DisEvent saved = service.saveReport(event);
        return AjaxResult.success("接报成功", saved);
    }

    @PostMapping("/accept")
    @ApiOperation("受理（待受理 -> 待核实）")
    @Log(title = "受理事件", businessType = BusinessType.UPDATE)
    public AjaxResult accept(@RequestBody DisEvent event) {
        service.accept(event.getId());
        return AjaxResult.success("受理成功");
    }

    @PostMapping("/verify")
    @ApiOperation("核实（更新主表初判信息，不实事件为终止态）")
    @Log(title = "核实事件", businessType = BusinessType.UPDATE)
    public AjaxResult verify(@RequestBody DisEvent event) {
        service.verify(event);
        return AjaxResult.success("核实成功");
    }

    @PostMapping("/transfer")
    @ApiOperation("转接（已核实 -> 处置中，更新责任单位）")
    @Log(title = "转接事件", businessType = BusinessType.UPDATE)
    public AjaxResult transfer(@RequestBody DisEventTransfer transfer) {
        service.transfer(transfer);
        return AjaxResult.success("转接成功");
    }

    @PostMapping("/report")
    @ApiOperation("报送（更新报送层级与附件）")
    @Log(title = "报送事件", businessType = BusinessType.UPDATE)
    public AjaxResult report(@RequestBody DisEventTransfer transfer) {
        service.report(transfer);
        return AjaxResult.success("报送成功");
    }

    @PostMapping("/track")
    @ApiOperation("跟踪（写跟踪单与日志，更新回访时间）")
    @Log(title = "跟踪事件", businessType = BusinessType.INSERT)
    public AjaxResult track(@RequestBody DisEventTrack track) {
        service.track(track);
        return AjaxResult.success("跟踪记录保存成功");
    }

    @PostMapping("/release")
    @ApiOperation("解除（处置中 -> 已解除）")
    @Log(title = "解除事件", businessType = BusinessType.UPDATE)
    public AjaxResult release(@RequestBody DisEvent event) {
        service.release(event.getId());
        return AjaxResult.success("事件已解除");
    }

    @PostMapping("/close")
    @ApiOperation("关闭（已解除 -> 已关闭，记录关闭原因/损失）")
    @Log(title = "关闭事件", businessType = BusinessType.UPDATE)
    public AjaxResult close(@RequestBody DisEvent event) {
        service.close(event);
        return AjaxResult.success("事件已关闭");
    }

    @PutMapping("/updateById")
    @ApiOperation("编辑事件基本信息（编号/状态不可改）")
    @Log(title = "修改事件", businessType = BusinessType.UPDATE)
    public AjaxResult updateById(@RequestBody DisEvent entity) {
        service.updateEvent(entity);
        return AjaxResult.success("修改成功");
    }

    @DeleteMapping("/deleteById")
    @ApiOperation("删除事件")
    @Log(title = "删除事件", businessType = BusinessType.DELETE)
    public AjaxResult deleteById(@RequestParam Long id) {
        boolean deleted = service.removeById(id);
        return deleted ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }
}
