package com.example.demo.controller;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.service.PollingQueryService;

/**
 * 轮询查询测试控制器
 * 用于测试和监控轮询查询功能
 */
@RestController
@RequestMapping("/api/polling")
public class PollingTestController {

    @Autowired
    private PollingQueryService pollingQueryService;


    /**
     * 获取轮询查询统计信息
     */
    @GetMapping("/stats")
    public ResponseEntity<Map<String, Object>> getPollingStats() {
        Map<String, Object> response = new HashMap<>();
        try {
            String stats = pollingQueryService.getPollingStats();
            response.put("success", true);
            response.put("message", "轮询查询统计信息");
            response.put("stats", stats);
            response.put("timestamp", System.currentTimeMillis());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取统计信息失败: " + e.getMessage());
            return ResponseEntity.status(500).body(response);
        }
    }

    /**
     * 手动触发轮询查询
     */
    @PostMapping("/trigger")
    public ResponseEntity<Map<String, Object>> triggerPolling() {
        Map<String, Object> response = new HashMap<>();
        try {
            pollingQueryService.manualPollingQuery();
            response.put("success", true);
            response.put("message", "手动触发轮询查询成功");
            response.put("timestamp", System.currentTimeMillis());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "手动触发轮询查询失败: " + e.getMessage());
            return ResponseEntity.status(500).body(response);
        }
    }

    /**
     * 检查轮询查询配置
     */
    @GetMapping("/config")
    public ResponseEntity<Map<String, Object>> getPollingConfig() {
        Map<String, Object> response = new HashMap<>();
        try {
            Map<String, Object> config = new HashMap<>();
            config.put("pollingInterval", "120000ms (2分钟)");
            config.put("scheduledMethod", "TaskStatusPollingService.pollTaskStatuses()");
            config.put("scheduledMethod2", "TaskQueueScheduler.checkTaskStatuses()");
            config.put("scheduledMethod3", "PollingQueryService.executePollingQuery()");
            config.put("enabled", true);
            
            response.put("success", true);
            response.put("message", "轮询查询配置信息");
            response.put("config", config);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取配置信息失败: " + e.getMessage());
            return ResponseEntity.status(500).body(response);
        }
    }
}
