package com.hotwj.platform.workbench.controller;

import com.hotwj.platform.resourceManagement.company.service.ISysUserLoginPortService;
import com.hotwj.platform.workbench.domain.vo.*;
import com.hotwj.platform.workbench.service.IGovWorkbenchService;
import com.hotwj.platform.workbench.service.IHotHiddenDangerDailyStatsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 政府端首页
 *
 * @author shihongwei
 * @date 2026-02-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workbench/gov")
@Tag(name = "政府端首页", description = "政府端首页统计模块")
public class GovWorkbenchController extends BaseController {

    private final IGovWorkbenchService govWorkbenchService;
    private final IHotHiddenDangerDailyStatsService dailyStatsService;

    /**
     * 获取政府端首页统计
     */
    @GetMapping("/statistics")
    @Operation(summary = "获取政府端首页统计")
    public R<GovHomeStatisticsVo> getHomeStatistics() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null || !ISysUserLoginPortService.GOVERNMENT_PORT.equals(loginUser.getLoginPort())) {
            return R.fail("非政府端用户无权访问");
        }
        return R.ok(govWorkbenchService.getHomeStatistics());
    }

    /**
     * 获取隐患统计总览
     */
    @GetMapping("/statistics/hidden-danger/summary")
    @Operation(summary = "获取隐患统计总览")
    public R<GovHiddenDangerSummaryVo> getHiddenDangerSummary(
            @RequestParam(required = false) Long companyId,
            @RequestParam(required = false) String regionCode) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null || !ISysUserLoginPortService.GOVERNMENT_PORT.equals(loginUser.getLoginPort())) {
            return R.fail("非政府端用户无权访问");
        }
        if (companyId != null) {
            return R.ok(govWorkbenchService.getCompanyHiddenDangerSummary(companyId));
        }
        return R.ok(govWorkbenchService.getHiddenDangerSummary(regionCode));
    }

    /**
     * 获取隐患明细列表
     */
    @GetMapping("/statistics/hidden-danger/list")
    @Operation(summary = "获取隐患明细列表")
    public TableDataInfo<GovHiddenDangerDetailVo> getHiddenDangerList(
            @RequestParam("type") String type,
            @RequestParam(required = false) Long companyId,
            PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null || !ISysUserLoginPortService.GOVERNMENT_PORT.equals(loginUser.getLoginPort())) {
            return TableDataInfo.build();
        }
        return govWorkbenchService.getHiddenDangerList(type, companyId, pageQuery);
    }

    @GetMapping("/statistics/hidden-danger/count-by-month")
    @Operation(summary = "获取隐患按日统计")
    public R<List<HiddenDangerCountVo>> getHiddenDangerCountByMonth(
            @RequestParam(required = false) Long companyId,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null || !ISysUserLoginPortService.GOVERNMENT_PORT.equals(loginUser.getLoginPort())) {
            return R.fail("非政府端用户无权访问");
        }
        return R.ok(govWorkbenchService.getHiddenDangerCountByMonth(companyId, startDate, endDate));
    }

    @GetMapping("/statistics/company/top")
    @Operation(summary = "获取隐患总数最高的企业TOP N (实时计算)")
    public R<List<GovCompanyRiskStatsVo>> getTopRiskCompanies(
            @RequestParam(defaultValue = "10") int limit) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null || !ISysUserLoginPortService.GOVERNMENT_PORT.equals(loginUser.getLoginPort())) {
            return R.fail("非政府端用户无权访问");
        }
        return R.ok(govWorkbenchService.getTopRiskCompanies(limit));
    }

    @GetMapping("/statistics/company/list")
    @Operation(summary = "分页获取企业隐患统计列表 (实时计算)")
    public GovCompanyRiskTableDataInfo getCompanyRiskList(
            @RequestParam(required = false) String companyName,
            PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null || !ISysUserLoginPortService.GOVERNMENT_PORT.equals(loginUser.getLoginPort())) {
            return new GovCompanyRiskTableDataInfo();
        }
        return govWorkbenchService.getCompanyRiskList(companyName, pageQuery);
    }

    @GetMapping("/statistics/training/stats")
    @Operation(summary = "获取学习折线图数据")
    public R<List<GovTrainingStatsVo>> getTrainingStats(
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String beginTime,
            @RequestParam(required = false) String endTime) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null || !ISysUserLoginPortService.GOVERNMENT_PORT.equals(loginUser.getLoginPort())) {
            return R.fail("非政府端用户无权访问");
        }
        return R.ok(govWorkbenchService.getTrainingStats(type, beginTime, endTime));
    }

    @GetMapping("/statistics/accident/level-stats")
    @Operation(summary = "获取事故等级统计")
    public R<List<GovAccidentStatsVo>> getAccidentLevelStats() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null || !ISysUserLoginPortService.GOVERNMENT_PORT.equals(loginUser.getLoginPort())) {
            return R.fail("非政府端用户无权访问");
        }
        return R.ok(govWorkbenchService.getAccidentLevelStats());
    }

    @GetMapping("/daily-stats/trigger")
    @Operation(summary = "手动触发隐患日统计（内部使用）")
    public R<Void> triggerDailyStats() {
        dailyStatsService.generateDailyStats();
        return R.ok();
    }
}
