package com.eactive.eai.manage.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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 어댑터/인터페이스/클라이언트 버킷 상태 모니터링 API.
 *
 * DualInflowControlManager 가 활성화된 환경에서만 정상 응답한다.
 * 비활성화 환경에서는 success=false 또는 빈 목록을 반환한다.
 *
 * GET /manage/inflow/adapter/bucket-status             → 전체 어댑터 버킷 상태
 * GET /manage/inflow/adapter/{adapterId}/bucket-status → 특정 어댑터 버킷 상태
 * GET /manage/inflow/interface/bucket-status           → 전체 인터페이스 버킷 상태
 * GET /manage/inflow/interface/{interfaceId}/bucket-status → 특정 인터페이스 버킷 상태
 */
@RestController
@RequestMapping("/manage/inflow")
public class InflowTargetBucketController {

    @Autowired
    private InflowTargetBucketService bucketService;

    // =========================================================================
    // 어댑터
    // =========================================================================

    @GetMapping("/adapter/bucket-status")
    public ResponseEntity<?> getAllAdapterBucketStatus() {
        List<TargetBucketStatusDTO> list = bucketService.getAllAdapterBucketStatus();
        return ok(list);
    }

    @GetMapping("/adapter/{adapterId}/bucket-status")
    public ResponseEntity<?> getAdapterBucketStatus(@PathVariable String adapterId) {
        TargetBucketStatusDTO dto = bucketService.getAdapterBucketStatus(adapterId);
        return single(dto, "어댑터를 찾을 수 없습니다: " + adapterId);
    }

    // =========================================================================
    // 인터페이스
    // =========================================================================

    @GetMapping("/interface/bucket-status")
    public ResponseEntity<?> getAllInterfaceBucketStatus() {
        List<TargetBucketStatusDTO> list = bucketService.getAllInterfaceBucketStatus();
        return ok(list);
    }

    @GetMapping("/interface/{interfaceId}/bucket-status")
    public ResponseEntity<?> getInterfaceBucketStatus(@PathVariable String interfaceId) {
        TargetBucketStatusDTO dto = bucketService.getInterfaceBucketStatus(interfaceId);
        return single(dto, "인터페이스를 찾을 수 없습니다: " + interfaceId);
    }

    // =========================================================================
    // Helpers
    // =========================================================================

    private ResponseEntity<?> ok(List<TargetBucketStatusDTO> 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<?> single(TargetBucketStatusDTO 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);
    }
}
