package com.controllers;

import com.common.entity.AnalysisAnalysisRule;
import com.common.entity.AnalysisTaskHistory;
import com.common.mapper.AnalysisTaskHistoryMapper;
import com.common.service.AnalysisRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 分析规则控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/analysis")
public class AnalysisRuleController {

    @Autowired
    private AnalysisRuleService analysisRuleService;

    @Autowired
    private AnalysisTaskHistoryMapper taskHistoryMapper;

    /**
     * 手动触发实时分析
     */
    @PostMapping("/realtime/execute")
    public Map<String, Object> executeRealtimeAnalysis() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Map<String, Object>> results = analysisRuleService.executeRealtimeAnalysis();
            result.put("code", 200);
            result.put("message", "实时分析执行成功");
            result.put("data", results);
        } catch (Exception e) {
            log.error("执行实时分析失败", e);
            result.put("code", 500);
            result.put("message", "执行实时分析失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 手动触发离线分析
     */
    @PostMapping("/offline/execute")
    public Map<String, Object> executeOfflineAnalysis() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Map<String, Object>> results = analysisRuleService.executeOfflineAnalysis();
            result.put("code", 200);
            result.put("message", "离线分析执行成功");
            result.put("data", results);
        } catch (Exception e) {
            log.error("执行离线分析失败", e);
            result.put("code", 500);
            result.put("message", "执行离线分析失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 查询活动规则
     */
    @GetMapping("/rules")
    public Map<String, Object> getActiveRules(@RequestParam String runMode) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<AnalysisAnalysisRule> rules = analysisRuleService.getActiveRules(runMode);
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", rules);
        } catch (Exception e) {
            log.error("查询活动规则失败", e);
            result.put("code", 500);
            result.put("message", "查询活动规则失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 停止规则
     */
    @PostMapping("/rules/{ruleId}/stop")
    public Map<String, Object> stopRule(@PathVariable String ruleId) {
        Map<String, Object> result = new HashMap<>();
        try {
            analysisRuleService.stopRule(ruleId);
            result.put("code", 200);
            result.put("message", "规则已停止");
        } catch (Exception e) {
            log.error("停止规则失败", e);
            result.put("code", 500);
            result.put("message", "停止规则失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 查询规则执行历史
     */
    @GetMapping("/rules/{ruleId}/history")
    public Map<String, Object> getRuleHistory(
            @PathVariable String ruleId,
            @RequestParam(defaultValue = "10") int limit) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<AnalysisTaskHistory> historyList = taskHistoryMapper.selectRecentByRuleId(ruleId, limit);
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", historyList);
        } catch (Exception e) {
            log.error("查询规则执行历史失败", e);
            result.put("code", 500);
            result.put("message", "查询规则执行历史失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 健康检查
     */
    @GetMapping("/health")
    public Map<String, Object> health() {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "服务正常");
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }
}
