package incheon.product.common.theme.web;

import incheon.com.cmm.api.DefaultApiResponse;
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.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 공통 주제도 API. 2D/3D 공통으로 주제도/카테고리/레이어 데이터를 제공한다.
 *
 * <p>URL 패턴: {@code /api/v1/product/{dim}/themes/**}
 * <ul>
 *   <li>{dim} = g2d: 2D 주제도</li>
 *   <li>{dim} = g3d: 3D 주제도</li>
 * </ul>
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/product/{dim}/themes")
public class CommonThemeApiController {

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

    /**
     * 주제도 목록을 조회한다.
     */
    @GetMapping
    public DefaultApiResponse<List<ThemeVO>> getThemes(@PathVariable String dim) {
        return DefaultApiResponse.success(commonThemeService.getThemes());
    }

    /**
     * 카테고리 목록을 조회한다.
     */
    @GetMapping("/categories")
    public DefaultApiResponse<List<ThemeCategoryVO>> getCategories(@PathVariable String dim) {
        return DefaultApiResponse.success(commonThemeService.getCategories());
    }

    /**
     * 주제도 상세를 조회한다.
     */
    @GetMapping("/{themeId}")
    public ResponseEntity<DefaultApiResponse<ThemeVO>> getTheme(@PathVariable String dim,
                                                                  @PathVariable String themeId) {
        ThemeVO theme = commonThemeService.getTheme(themeId);
        if (theme == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND)
                    .body(DefaultApiResponse.error(404, "주제도를 찾을 수 없습니다: " + themeId, "Not Found"));
        }
        return ResponseEntity.ok(DefaultApiResponse.success(theme));
    }

    /**
     * 주제도별 레이어 목록을 조회한다.
     */
    @GetMapping("/{themeId}/layers")
    public DefaultApiResponse<List<ThemeLayerVO>> getLayers(@PathVariable String dim,
                                                             @PathVariable String themeId) {
        return DefaultApiResponse.success(commonThemeService.getLayersByThemeId(themeId));
    }
}
