package incheon.product.geoview3d.theme.web;

import incheon.product.geoview3d.theme.service.ThemeService;
import incheon.product.geoview3d.theme.vo.ThemeCategoryVO;
import incheon.product.geoview3d.theme.vo.ThemeLayerVO;
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.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import java.util.List;

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.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

/**
 * ThemeViewController MockMvc 테스트.
 * 3D 주제도 메인 페이지, name 파라미터별 헤더 타이틀을 검증한다.
 */
@ExtendWith(MockitoExtension.class)
class ThemeViewControllerTest {

    @InjectMocks
    private ThemeViewController controller;

    @Mock
    private ThemeService themeService;

    private MockMvc mockMvc;

    @BeforeEach
    void setup() {
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
    }

    @Test
    @DisplayName("name 미지정 → headerTitle '3D 주제도'")
    void mainWithDefaultName() throws Exception {
        when(themeService.getCategories()).thenReturn(List.of());
        when(themeService.getLayers()).thenReturn(List.of());

        mockMvc.perform(get("/product/g3d/theme/main.do"))
                .andExpect(status().isOk())
                .andExpect(view().name("product/geoview3d/map"))
                .andExpect(model().attribute("headerTitle", "3D 주제도"))
                .andExpect(model().attribute("pageTitle", "GeoView-3D"));
    }

    @Test
    @DisplayName("name=건물 → headerTitle '건물'")
    void mainWithCustomName() throws Exception {
        when(themeService.getCategories()).thenReturn(List.of());
        when(themeService.getLayers()).thenReturn(List.of());

        mockMvc.perform(get("/product/g3d/theme/main.do")
                        .param("name", "건물"))
                .andExpect(status().isOk())
                .andExpect(model().attribute("headerTitle", "건물"));
    }

    @Test
    @DisplayName("themeMap 그룹핑 — categoryName별 분류")
    void mainPopulatesThemeMap() throws Exception {
        ThemeLayerVO layer = new ThemeLayerVO();
        layer.setCategoryName("도시");
        when(themeService.getCategories()).thenReturn(List.of(new ThemeCategoryVO()));
        when(themeService.getLayers()).thenReturn(List.of(layer));

        mockMvc.perform(get("/product/g3d/theme/main.do"))
                .andExpect(status().isOk())
                .andExpect(model().attributeExists("themeMap"))
                .andExpect(model().attributeExists("categories"));
    }
}
