package com.arms.api.admin.monitoring.controller;

import com.arms.api.admin.monitoring.model.vo.*;
import com.arms.api.admin.monitoring.service.EsMonitoringService;
import com.arms.api.util.response.CommonResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.arms.api.util.response.CommonResponse.success;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/engine/admin/monitoring")
@Tag(name = "ES Monitoring", description = "Elasticsearch 모니터링 API")
public class EsMonitoringController {

    private final EsMonitoringService esMonitoringService;

    @GetMapping("/health")
    @Operation(summary = "클러스터 상태 조회", description = "Elasticsearch 클러스터의 상태를 조회합니다. (green/yellow/red)")
    public ResponseEntity<CommonResponse.ApiResult<ClusterHealthVO>> getClusterHealth() {
        ClusterHealthVO result = esMonitoringService.getClusterHealth();
        return ResponseEntity.ok(success(result));
    }

    @GetMapping("/nodes")
    @Operation(summary = "노드 상태 조회", description = "모든 노드의 JVM, CPU, 디스크 사용량을 조회합니다.")
    public ResponseEntity<CommonResponse.ApiResult<List<NodeStatsVO>>> getNodeStats() {
        List<NodeStatsVO> result = esMonitoringService.getNodeStats();
        return ResponseEntity.ok(success(result));
    }

    @GetMapping("/thread-pool")
    @Operation(summary = "Thread Pool 상태 조회", description = "write, search thread pool의 상태를 조회합니다. rejected > 0 이면 장애!")
    public ResponseEntity<CommonResponse.ApiResult<List<ThreadPoolStatsVO>>> getThreadPoolStats() {
        List<ThreadPoolStatsVO> result = esMonitoringService.getThreadPoolStats();
        return ResponseEntity.ok(success(result));
    }

    @GetMapping("/indexing")
    @Operation(summary = "인덱싱/검색 통계 조회", description = "인덱싱 및 검색 성능 통계를 조회합니다.")
    public ResponseEntity<CommonResponse.ApiResult<IndexingStatsVO>> getIndexingStats() {
        IndexingStatsVO result = esMonitoringService.getIndexingStats();
        return ResponseEntity.ok(success(result));
    }

    @GetMapping("/summary")
    @Operation(summary = "전체 모니터링 요약", description = "모든 모니터링 지표를 종합하여 장애 여부를 판단합니다.")
    public ResponseEntity<CommonResponse.ApiResult<EsMonitoringSummaryVO>> getMonitoringSummary() {
        EsMonitoringSummaryVO result = esMonitoringService.getMonitoringSummary();
        return ResponseEntity.ok(success(result));
    }

    @GetMapping("/is-healthy")
    @Operation(summary = "장애 여부 확인", description = "빠른 health check. true=정상, false=장애")
    public ResponseEntity<CommonResponse.ApiResult<Boolean>> isHealthy() {
        Boolean result = esMonitoringService.isHealthy();
        return ResponseEntity.ok(success(result));
    }
}