package com.zksy.web.controller.hidden;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.domain.HazardHiddenAccount;
import com.zksy.base.dto.HazardPageInDTO;
import com.zksy.base.service.HazardHiddenAccountService;
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 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/hazard")
@Api(tags = "隐患台账管理")
public class HazardController {

    @Autowired
    private HazardHiddenAccountService service;

    @GetMapping("/page")
    @ApiOperation("隐患分页查询")
    @Anonymous
    public AjaxResult page(HazardPageInDTO dto) {
        Page<HazardHiddenAccount> result = service.findByPage(dto);
        return AjaxResult.success(result);
    }

    @GetMapping("/getById/{id}")
    @ApiOperation("根据ID查询隐患")
    @Anonymous
    public AjaxResult getById(@PathVariable String id) {
        HazardHiddenAccount entity = service.getById(id);
        return entity != null ? AjaxResult.success(entity) : AjaxResult.error("隐患信息不存在");
    }

    @PostMapping("/save")
    @ApiOperation("新增隐患")
    @Log(title = "新增隐患台账", businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody HazardHiddenAccount entity) {
        boolean saved = service.saveWithCheck(entity);
        return saved ? AjaxResult.success("新增成功", saved) : AjaxResult.error("新增失败");
    }

    @PutMapping("/updateById")
    @ApiOperation("修改隐患")
    @Log(title = "修改隐患台账", businessType = BusinessType.UPDATE)
    public AjaxResult updateById(@RequestBody HazardHiddenAccount entity) {
        boolean updated = service.updateWithCheck(entity);
        return updated ? AjaxResult.success("修改成功", updated) : AjaxResult.error("修改失败");
    }

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

    @PutMapping("/submit/{hazardId}")
    @ApiOperation("隐患报送（同时自动创建工单）")
    @Log(title = "隐患报送", businessType = BusinessType.UPDATE)
    public AjaxResult submit(@PathVariable String hazardId) {
        Long orderId = service.submitHazard(hazardId);

        if (orderId != null) {
            return AjaxResult.success("报送成功，已自动创建工单", orderId);
        }
        return AjaxResult.success("报送成功");
    }

    @GetMapping("/map-points")
    @ApiOperation("获取未闭环隐患点位（隐患一张图）")
    @Anonymous
    public AjaxResult getMapPoints() {
        return AjaxResult.success(service.getActiveMapPoints());
    }

    @GetMapping("/statistics/type")
    @ApiOperation("按隐患类型统计")
    @Anonymous
    public AjaxResult statisticsByType(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        return AjaxResult.success(service.statisticsByType(startDate, endDate));
    }

    @GetMapping("/statistics/status")
    @ApiOperation("按整改状态统计")
    @Anonymous
    public AjaxResult statisticsByStatus(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        return AjaxResult.success(service.statisticsByStatus(startDate, endDate));
    }

    @GetMapping("/statistics/overview")
    @ApiOperation("隐患统计概览（总数、已整改、按等级分布）")
    @Anonymous
    public AjaxResult statisticsOverview(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        return AjaxResult.success(service.statisticsOverview(startDate, endDate));
    }

    @GetMapping("/statistics/level")
    @ApiOperation("按隐患等级统计")
    @Anonymous
    public AjaxResult statisticsByLevel(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        return AjaxResult.success(service.statisticsByLevel(startDate, endDate));
    }

    @GetMapping("/statistics/region")
    @ApiOperation("按上报单位（区域）统计")
    @Anonymous
    public AjaxResult statisticsByRegion(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        return AjaxResult.success(service.statisticsByRegion(startDate, endDate));
    }

    @GetMapping("/statistics/type-timeline")
    @ApiOperation("隐患类型时间线统计（按天汇总，每条类型一条折线）")
    @Anonymous
    public AjaxResult statisticsTypeTimeline(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        return AjaxResult.success(service.statisticsTypeTimeline(startDate, endDate));
    }

    @GetMapping("/statistics/status-timeline")
    @ApiOperation("整改状态时间线统计（按天汇总，每个状态一组柱）")
    @Anonymous
    public AjaxResult statisticsStatusTimeline(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        return AjaxResult.success(service.statisticsStatusTimeline(startDate, endDate));
    }

    @GetMapping("/export")
    @ApiOperation("隐患导出Excel")
    @Anonymous
    public AjaxResult exportData() {
        // TODO: 实现Excel导出
        return AjaxResult.success("导出功能待实现");
    }
}
