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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 클라이언트 버킷 상태 모니터링 API.
 *
 * ClientDualInflowControlManager 가 활성화된 환경에서만 정상 응답한다.
 *
 * GET /manage/inflow/client/bucket-status             → 전체 클라이언트 버킷 상태
 * GET /manage/inflow/client/{clientId}/bucket-status  → 특정 클라이언트 버킷 상태
 */
@RestController
@RequestMapping("/manage/inflow")
public class ClientInflowTargetBucketController {

    @Autowired
    private ClientInflowTargetBucketService bucketService;

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

    @GetMapping("/client/{clientId}/bucket-status")
    public ResponseEntity<?> getClientBucketStatus(@PathVariable String clientId) {
        TargetBucketStatusDTO dto = bucketService.getClientBucketStatus(clientId);
        return single(dto, "클라이언트를 찾을 수 없습니다: " + clientId);
    }

    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);
    }
}
