package com.gcsc.connection.monitoring.controller;

import com.gcsc.connection.common.dto.ApiResponse;
import com.gcsc.connection.monitoring.dto.DashboardStatsResponse;
import com.gcsc.connection.monitoring.dto.ErrorTrendResponse;
import com.gcsc.connection.monitoring.dto.HeartbeatStatusResponse;
import com.gcsc.connection.monitoring.dto.HourlyTrendResponse;
import com.gcsc.connection.monitoring.dto.RequestLogResponse;
import com.gcsc.connection.monitoring.dto.ServiceRatioResponse;
import com.gcsc.connection.monitoring.dto.TenantRatioResponse;
import com.gcsc.connection.monitoring.dto.TopApiResponse;
import com.gcsc.connection.monitoring.service.DashboardService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 대시보드 API - 모니터링 요약/통계 데이터 제공
 */
@RestController
@RequestMapping("/api/dashboard")
@RequiredArgsConstructor
public class DashboardController {

    private final DashboardService dashboardService;
    private final HeartbeatService heartbeatService;

    /**
     * 대시보드 요약 통계 조회
     */
    @GetMapping("/summary")
    public ResponseEntity<ApiResponse<DashboardStatsResponse>> getSummary() {
        DashboardStatsResponse summary = dashboardService.getSummary();
        return ResponseEntity.ok(ApiResponse.ok(summary));
    }

    /**
     * 시간별 요청 추이 조회
     */
    @GetMapping("/hourly-trend")
    public ResponseEntity<ApiResponse<List<HourlyTrendResponse>>> getHourlyTrend() {
        List<HourlyTrendResponse> trend = dashboardService.getHourlyTrend();
        return ResponseEntity.ok(ApiResponse.ok(trend));
    }

    /**
     * 서비스별 요청 비율 조회
     */
    @GetMapping("/service-ratio")
    public ResponseEntity<ApiResponse<List<ServiceRatioResponse>>> getServiceRatio() {
        List<ServiceRatioResponse> ratio = dashboardService.getServiceRatio();
        return ResponseEntity.ok(ApiResponse.ok(ratio));
    }

    /**
     * 에러율 추이 조회 (시간별, 서비스별)
     */
    @GetMapping("/error-trend")
    public ResponseEntity<ApiResponse<List<ErrorTrendResponse>>> getErrorTrend() {
        List<ErrorTrendResponse> trend = dashboardService.getErrorTrend();
        return ResponseEntity.ok(ApiResponse.ok(trend));
    }

    /**
     * 상위 API 랭킹 조회
     */
    @GetMapping("/top-apis")
    public ResponseEntity<ApiResponse<List<TopApiResponse>>> getTopApis(
            @RequestParam(defaultValue = "10") int limit) {
        List<TopApiResponse> topApis = dashboardService.getTopApis(limit);
        return ResponseEntity.ok(ApiResponse.ok(topApis));
    }

    /**
     * 테넌트별 요청 비율 조회
     */
    @GetMapping("/tenant-request-ratio")
    public ResponseEntity<ApiResponse<List<TenantRatioResponse>>> getTenantRequestRatio() {
        List<TenantRatioResponse> ratio = dashboardService.getTenantRequestRatio();
        return ResponseEntity.ok(ApiResponse.ok(ratio));
    }

    /**
     * 테넌트별 사용자 비율 조회
     */
    @GetMapping("/tenant-user-ratio")
    public ResponseEntity<ApiResponse<List<TenantRatioResponse>>> getTenantUserRatio() {
        List<TenantRatioResponse> ratio = dashboardService.getTenantUserRatio();
        return ResponseEntity.ok(ApiResponse.ok(ratio));
    }

    /**
     * 최근 요청 로그 조회
     */
    @GetMapping("/recent-logs")
    public ResponseEntity<ApiResponse<List<RequestLogResponse>>> getRecentLogs() {
        List<RequestLogResponse> logs = dashboardService.getRecentLogs();
        return ResponseEntity.ok(ApiResponse.ok(logs));
    }

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