package incheon.product.common.theme.web;

import incheon.product.common.theme.service.CommonThemeService;
import incheon.product.common.theme.vo.ThemeCategoryVO;
import incheon.product.common.theme.vo.ThemeLayerVO;
import incheon.product.common.theme.vo.ThemeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 공통 주제도 뷰 컨트롤러. 2D/3D 공통 주제도 JSP 화면을 렌더링한다.
 *
 * <p>URL 패턴: {@code /product/{dim}/theme/{themeId}.do}
 * <ul>
 *   <li>{dim} = g2d: 2D 주제도 화면 → product/geoview2d/themeMap</li>
 *   <li>{dim} = g3d: 3D 주제도 화면 → product/geoview3d/map</li>
 * </ul>
 *
 * <p>기존 geoview3d ThemeViewController(/product/g3d/theme/main.do)와 공존한다.
 * 기존 URL은 그대로 동작하며, 이 컨트롤러는 BL-001 시나리오의
 * themeId 기반 직접 진입 경로를 제공한다.
 */
@Slf4j
@Controller
@RequestMapping("/product/{dim}/theme")
public class CommonThemeViewController {

    @Resource(name = "commonThemeService")
    private CommonThemeService commonThemeService;

    /** dim별 JSP 뷰 경로 */
    private static final Map<String, String> VIEW_MAP = Map.of(
            "g2d", "product/geoview2d/themeMap",
            "g3d", "product/geoview3d/map"
    );

    /** dim별 페이지 타이틀 */
    private static final Map<String, String> TITLE_MAP = Map.of(
            "g2d", "GeoView-2D",
            "g3d", "GeoView-3D"
    );

    /**
     * 특정 주제도 화면을 렌더링한다.
     *
     * @param dim     차원 (g2d | g3d)
     * @param themeId 주제도 식별자
     * @param model   JSP 모델
     * @return JSP 뷰 이름
     */
    @GetMapping("/{themeId}.do")
    public String themeView(@PathVariable String dim,
                            @PathVariable String themeId,
                            Model model) {

        String viewName = VIEW_MAP.get(dim);
        if (viewName == null) {
            log.warn("지원하지 않는 dim 값: {}", dim);
            return "error/404";
        }

        ThemeVO theme = commonThemeService.getTheme(themeId);
        List<ThemeCategoryVO> categories = commonThemeService.getCategories();
        List<ThemeLayerVO> layers = commonThemeService.getLayersByThemeId(themeId);

        Map<String, List<ThemeLayerVO>> layerMap = layers.stream()
                .collect(Collectors.groupingBy(l -> l.getName() != null ? l.getName() : "미분류"));

        model.addAttribute("dim", dim);
        model.addAttribute("startTheme", themeId);
        model.addAttribute("theme", theme);
        model.addAttribute("categories", categories);
        model.addAttribute("themeLayers", layers);
        model.addAttribute("layerMap", layerMap);
        model.addAttribute("headerTitle", theme != null ? theme.getName() : "주제도");
        model.addAttribute("pageTitle", TITLE_MAP.getOrDefault(dim, "GeoView"));

        return viewName;
    }
}
