package com.gcsc.connection.monitoring.controller;

import com.gcsc.connection.common.dto.ApiResponse;
import com.gcsc.connection.monitoring.dto.HeartbeatStatusResponse;
import com.gcsc.connection.monitoring.dto.HealthHistoryResponse;
import com.gcsc.connection.monitoring.dto.ServiceStatusDetailResponse;
import com.gcsc.connection.monitoring.service.HeartbeatService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 헬스체크(하트비트) 모니터링 API
 */
@RestController
@RequestMapping("/api/heartbeat")
@RequiredArgsConstructor
public class HeartbeatController {

    private final HeartbeatService heartbeatService;

    /**
     * 전체 서비스 헬스 상태 조회
     */
    @GetMapping("/status")
    public ResponseEntity<ApiResponse<List<HeartbeatStatusResponse>>> getAllServiceStatus() {
        List<HeartbeatStatusResponse> statuses = heartbeatService.getAllServiceStatus();
        return ResponseEntity.ok(ApiResponse.ok(statuses));
    }

    /**
     * 전체 서비스 상태 상세 (status page)
     */
    @GetMapping("/status/detail")
    public ResponseEntity<ApiResponse<List<ServiceStatusDetailResponse>>> getAllStatusDetail() {
        List<ServiceStatusDetailResponse> details = heartbeatService.getAllServiceStatusDetail();
        return ResponseEntity.ok(ApiResponse.ok(details));
    }

    /**
     * 단일 서비스 상태 상세 (status page)
     */
    @GetMapping("/{serviceId}/status/detail")
    public ResponseEntity<ApiResponse<ServiceStatusDetailResponse>> getServiceStatusDetail(
            @PathVariable Long serviceId) {
        ServiceStatusDetailResponse detail = heartbeatService.getServiceStatusDetail(serviceId);
        return ResponseEntity.ok(ApiResponse.ok(detail));
    }

    /**
     * 서비스 헬스체크 이력 조회
     */
    @GetMapping("/{serviceId}/history")
    public ResponseEntity<ApiResponse<List<HealthHistoryResponse>>> getHealthHistory(
            @PathVariable Long serviceId) {
        List<HealthHistoryResponse> history = heartbeatService.getHealthHistory(serviceId);
        return ResponseEntity.ok(ApiResponse.ok(history));
    }

    /**
     * 수동 헬스체크 실행
     */
    @PostMapping("/{serviceId}/check")
    public ResponseEntity<ApiResponse<HeartbeatStatusResponse>> checkService(
            @PathVariable Long serviceId) {
        HeartbeatStatusResponse result = heartbeatService.checkService(serviceId);
        return ResponseEntity.ok(ApiResponse.ok(result));
    }
}
