package com.erp.systemtool.controller;

import com.erp.systemtool.service.MonitoringService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Monitoring Controller
 * 
 * Provides REST API for system monitoring and health checks.
 * Migrations from PHP MonitoringService.
 */
@RestController
@RequestMapping("/api/system/monitoring")
@RequiredArgsConstructor
public class MonitoringController {

    private final MonitoringService monitoringService;

    /**
     * Get API statistics
     */
    @GetMapping("/api-stats")
    public ResponseEntity<Map<String, Object>> getApiStats(
            @RequestParam(required = false, defaultValue = "all") String endpoint,
            @RequestParam(required = false, defaultValue = "60") Integer minutes) {
        
        Map<String, Object> stats = monitoringService.getApiStats(endpoint, minutes);
        
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "data", stats,
            "message", "success"
        ));
    }

    /**
     * Check queue backlog
     */
    @GetMapping("/queue-backlog")
    public ResponseEntity<Map<String, Object>> getQueueBacklog() {
        var backlogs = monitoringService.checkQueueBacklog();
        
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "data", backlogs,
            "message", "success"
        ));
    }

    /**
     * Send test alert
     */
    @PostMapping("/alert")
    public ResponseEntity<Map<String, Object>> sendAlert(
            @RequestBody Map<String, String> request) {
        
        String level = request.getOrDefault("level", "warning");
        String title = request.getOrDefault("title", "Test Alert");
        String message = request.getOrDefault("message", "This is a test alert");
        
        monitoringService.alert(level, title, message);
        
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "message", "告警已发送"
        ));
    }

    /**
     * Get system health status
     */
    @GetMapping("/health")
    public ResponseEntity<Map<String, Object>> getHealth() {
        Map<String, Object> health = monitoringService.getHealthStatus();
        
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "data", health,
            "message", "success"
        ));
    }

    /**
     * Record API request (for testing/internal use)
     */
    @PostMapping("/record")
    public ResponseEntity<Map<String, Object>> recordRequest(
            @RequestBody Map<String, Object> request) {
        
        String endpoint = (String) request.get("endpoint");
        long duration = ((Number) request.get("duration")).longValue();
        int statusCode = ((Number) request.get("statusCode")).intValue();
        
        monitoringService.recordApiRequest(endpoint, duration, statusCode);
        
        return ResponseEntity.ok(Map.of(
            "code", 200,
            "message", "Request recorded"
        ));
    }
}
