package com.eactive.eai.custom.inflow;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.ResponseEntity;
import org.springframework.test.util.ReflectionTestUtils;

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

/**
 * ClientInflowTargetBucketController 단위 테스트.
 */
class ClientInflowTargetBucketControllerTest {

    private ClientInflowTargetBucketController controller;
    private ClientInflowTargetBucketService mockService;

    @BeforeEach
    void setUp() {
        controller  = new ClientInflowTargetBucketController();
        mockService = mock(ClientInflowTargetBucketService.class);
        ReflectionTestUtils.setField(controller, "bucketService", mockService);
    }

    private TargetBucketStatusDTO makeDTO(String id, String type) {
        TargetBucketStatusDTO dto = new TargetBucketStatusDTO();
        dto.setTargetId(id);
        dto.setTargetType(type);
        dto.setActivate(true);
        dto.setBuckets(Collections.emptyList());
        return dto;
    }

    @SuppressWarnings("unchecked")
    private Map<String, Object> body(ResponseEntity<?> response) {
        return (Map<String, Object>) response.getBody();
    }

    // =========================================================================
    // 단일 조회
    // =========================================================================

    @Test
    void getClientBucketStatus_found_successTrueAndDataPresent() {
        TargetBucketStatusDTO dto = makeDTO("CLIENT_A", "CLIENT");
        when(mockService.getClientBucketStatus("CLIENT_A")).thenReturn(dto);

        ResponseEntity<?> response = controller.getClientBucketStatus("CLIENT_A");
        Map<String, Object> body = body(response);

        assertEquals(200, response.getStatusCodeValue());
        assertEquals(Boolean.TRUE, body.get("success"));
        assertSame(dto, body.get("data"));
        assertFalse(body.containsKey("message"));
    }

    @Test
    void getClientBucketStatus_notFound_successFalseWithClientId() {
        when(mockService.getClientBucketStatus("CLIENT_X")).thenReturn(null);

        ResponseEntity<?> response = controller.getClientBucketStatus("CLIENT_X");
        Map<String, Object> body = body(response);

        assertEquals(Boolean.FALSE, body.get("success"));
        assertTrue(body.get("message").toString().contains("CLIENT_X"));
    }

    // =========================================================================
    // 전체 조회
    // =========================================================================

    @Test
    void getAllClientBucketStatus_returnsListAndCount() {
        List<TargetBucketStatusDTO> list = Arrays.asList(
                makeDTO("CLIENT_A", "CLIENT"),
                makeDTO("CLIENT_B", "CLIENT"),
                makeDTO("CLIENT_C", "CLIENT")
        );
        when(mockService.getAllClientBucketStatus()).thenReturn(list);

        ResponseEntity<?> response = controller.getAllClientBucketStatus();
        Map<String, Object> body = body(response);

        assertEquals(Boolean.TRUE, body.get("success"));
        assertSame(list, body.get("data"));
        assertEquals(3, body.get("count"));
    }

    @Test
    void getAllClientBucketStatus_emptyList_countZero() {
        when(mockService.getAllClientBucketStatus()).thenReturn(Collections.emptyList());

        Map<String, Object> body = body(controller.getAllClientBucketStatus());

        assertEquals(Boolean.TRUE, body.get("success"));
        assertEquals(0, body.get("count"));
    }
}
