package com.eactive.eai.custom.inflow;

import java.util.HashMap;
import java.util.List;
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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eactive.eai.manage.inflow.TargetMetricDTO;

/**
 * 클라이언트 유량제어 메트릭 API.
 *
 * ClientDualInflowControlManager 가 활성화된 환경에서만 정상 응답한다.
 *
 * GET  /manage/inflow/client/metric                       → 전체 클라이언트 메트릭
 * GET  /manage/inflow/client/{clientId}/metric            → 특정 클라이언트 메트릭
 * POST /manage/inflow/client/metric/reset                 → 전체 클라이언트 메트릭 초기화
 * POST /manage/inflow/client/{clientId}/metric/reset      → 특정 클라이언트 메트릭 초기화
 */
@RestController
@RequestMapping("/manage/inflow")
public class ClientInflowTargetMetricController {

    @Autowired
    private ClientInflowTargetMetricService metricService;

    @GetMapping("/client/metric")
    public ResponseEntity<?> getAllClientMetrics() {
        List<TargetMetricDTO> list = metricService.getAllClientMetrics();
        return okList(list);
    }

    @GetMapping("/client/{clientId}/metric")
    public ResponseEntity<?> getClientMetric(@PathVariable String clientId) {
        TargetMetricDTO dto = metricService.getClientMetric(clientId);
        return okSingle(dto, "클라이언트를 찾을 수 없습니다: " + clientId);
    }

    @PostMapping("/client/metric/reset")
    public ResponseEntity<?> resetAllClientMetrics() {
        metricService.resetAllClientMetrics();
        return okReset("전체 클라이언트 메트릭 초기화 완료");
    }

    @PostMapping("/client/{clientId}/metric/reset")
    public ResponseEntity<?> resetClientMetric(@PathVariable String clientId) {
        boolean ok = metricService.resetClientMetric(clientId);
        return okResetSingle(ok, clientId, "클라이언트");
    }

    private ResponseEntity<?> okList(List<TargetMetricDTO> list) {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", list);
        result.put("count", list.size());
        return ResponseEntity.ok(result);
    }

    private ResponseEntity<?> okSingle(TargetMetricDTO dto, String notFoundMsg) {
        if (dto == null) {
            Map<String, Object> error = new HashMap<>();
            error.put("success", false);
            error.put("message", notFoundMsg);
            return ResponseEntity.ok(error);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", dto);
        return ResponseEntity.ok(result);
    }

    private ResponseEntity<?> okReset(String message) {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", message);
        return ResponseEntity.ok(result);
    }

    private ResponseEntity<?> okResetSingle(boolean ok, String targetId, String targetLabel) {
        Map<String, Object> result = new HashMap<>();
        result.put("success", ok);
        result.put("message", ok
            ? "초기화 완료: " + targetId
            : targetLabel + "를 찾을 수 없습니다: " + targetId);
        return ResponseEntity.ok(result);
    }
}
