package incheon.product.geoview3d.simulation.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import incheon.com.cmm.exception.RestApiExceptionHandler;
import incheon.product.geoview3d.simulation.service.SimulationService;
import incheon.product.geoview3d.simulation.vo.SubwayVO;
import incheon.product.geoview3d.simulation.vo.TilesetClippingVO;
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.MediaType;
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.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * SimulationApiController MockMvc 테스트.
 * raw String 응답, null → 404 처리를 검증한다.
 */
@ExtendWith(MockitoExtension.class)
class SimulationApiControllerTest {

    @InjectMocks
    private SimulationApiController controller;

    @Mock
    private SimulationService simulationService;

    private MockMvc mockMvc;

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

    @Test
    @DisplayName("시뮬레이션 JSON — raw String 응답 (DefaultApiResponse 미사용)")
    void getSimulationJsonReturnsRawString() throws Exception {
        String json = "{\"type\":\"flood\",\"data\":[1,2,3]}";
        when(simulationService.getSimulationJson("flood-01")).thenReturn(json);

        mockMvc.perform(get("/api/v1/product/g3d/simulation/flood-01"))
                .andExpect(status().isOk())
                .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
                .andExpect(content().json(json));
    }

    @Test
    @DisplayName("클리핑 조회 성공")
    void getClippingFound() throws Exception {
        TilesetClippingVO clipping = new TilesetClippingVO();
        clipping.setClipId("clip-001");
        clipping.setClipName("테스트 클리핑");
        when(simulationService.getClipping("clip-001")).thenReturn(clipping);

        mockMvc.perform(get("/api/v1/product/g3d/simulation/clipping/clip-001"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.clipId").value("clip-001"));
    }

    @Test
    @DisplayName("클리핑 null → 404")
    void getClippingNotFound() throws Exception {
        when(simulationService.getClipping("nonexistent")).thenReturn(null);

        mockMvc.perform(get("/api/v1/product/g3d/simulation/clipping/nonexistent"))
                .andExpect(status().isNotFound())
                .andExpect(jsonPath("$.code").value(404));
    }

    @Test
    @DisplayName("지하철 null → 404")
    void getSubwayNotFound() throws Exception {
        when(simulationService.getSubway("없는노선")).thenReturn(null);

        mockMvc.perform(get("/api/v1/product/g3d/simulation/subway")
                        .param("subwayName", "없는노선"))
                .andExpect(status().isNotFound())
                .andExpect(jsonPath("$.code").value(404));
    }
}
