package incheon.product.common.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import incheon.product.common.geo3d.GeoView3DProperties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * ViewerConfigApiController MockMvc 테스트.
 * Properties → JSON 매핑 구조를 검증한다.
 */
@ExtendWith(MockitoExtension.class)
class ViewerConfigApiControllerTest {

    @InjectMocks
    private ViewerConfigApiController controller;

    @Mock
    private GeoViewProperties geoViewProperties;

    @Mock
    private GeoView3DProperties geoView3DProperties;

    private MockMvc mockMvc;

    @BeforeEach
    void setup() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());
        mockMvc = MockMvcBuilders.standaloneSetup(controller)
                .setMessageConverters(
                        new StringHttpMessageConverter(),
                        new MappingJackson2HttpMessageConverter(mapper))
                .build();
    }

    @Test
    @DisplayName("3D 뷰어 설정 — camera + constraints 구조")
    void getViewer3DConfig() throws Exception {
        GeoView3DProperties.Viewer viewer = new GeoView3DProperties.Viewer();
        when(geoView3DProperties.getViewer()).thenReturn(viewer);

        mockMvc.perform(get("/api/v1/product/config/viewer3d"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.camera.longitude").value(126.70486))
                .andExpect(jsonPath("$.data.camera.latitude").value(37.45247))
                .andExpect(jsonPath("$.data.constraints.minZoomDistance").value(150.0))
                .andExpect(jsonPath("$.data.constraints.maxZoomDistance").value(500000.0));
    }

    @Test
    @DisplayName("2D 뷰어 설정 — coordinate 구조")
    void getViewer2DConfig() throws Exception {
        GeoViewProperties.Coordinate coord = new GeoViewProperties.Coordinate();
        when(geoViewProperties.getCoordinate()).thenReturn(coord);

        mockMvc.perform(get("/api/v1/product/config/viewer2d"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.coordinate.defaultSrid").value(5181))
                .andExpect(jsonPath("$.data.coordinate.serviceSrid").value(3857));
    }
}
