package shop.alien.storeplatform.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.vo.StoreDictionaryVo;
import shop.alien.storeplatform.service.BusinessSectionService;

import java.util.List;

/**
 * 经营板块管理控制器（平台端）
 * 提供经营板块的三级分类查询接口
 * 
 * <p>业务说明：
 * <ul>
 *   <li>一级分类：经营板块（特色美食、酒店/民宿、KTV等）</li>
 *   <li>二级分类：各板块下的子分类</li>
 *   <li>三级分类：各二级分类下的细分类</li>
 * </ul>
 * </p>
 *
 * @author alien-cloud
 * @date 2025-12-01
 */
@Api(tags = {"商户平台-经营板块管理"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/businessSection")
@RequiredArgsConstructor
public class BusinessSectionController {

    private final BusinessSectionService businessSectionService;

    /**
     * 查询经营板块一级分类列表
     * 返回所有一级分类（特色美食、酒店/民宿、KTV、洗浴汗蒸等）
     *
     * @return R<List<StoreDictionaryVo>> 一级分类列表
     * @author alien-cloud
     * @date 2025-12-01
     */
    @ApiOperation("查询经营板块一级分类列表")
    @ApiOperationSupport(order = 1)
    @GetMapping("/getFirstLevelList")
    public R<List<StoreDictionaryVo>> getFirstLevelList() {
        log.info("BusinessSectionController.getFirstLevelList - 查询一级分类列表");
        try {
            List<StoreDictionaryVo> result = businessSectionService.getFirstLevelList();
            log.info("BusinessSectionController.getFirstLevelList - 查询成功，共 {} 条数据", result.size());
            return R.data(result);
        } catch (Exception e) {
            log.error("BusinessSectionController.getFirstLevelList ERROR: {}", e.getMessage(), e);
            return R.fail("查询失败：" + e.getMessage());
        }
    }

    /**
     * 查询经营板块二级分类列表
     * 根据一级分类ID查询对应的二级分类
     *
     * @param parentDictId 一级分类的字典ID（dict_id）
     * @return R<List<StoreDictionaryVo>> 二级分类列表
     * @author alien-cloud
     * @date 2025-12-01
     */
    @ApiOperation("查询经营板块二级分类列表")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentDictId", value = "一级分类字典ID（如：1-特色美食，2-酒店/民宿）", 
                    dataType = "String", paramType = "query", required = true, example = "1")
    })
    @GetMapping("/getSecondLevelList")
    public R<List<StoreDictionaryVo>> getSecondLevelList(
            @RequestParam("parentDictId") String parentDictId) {
        log.info("BusinessSectionController.getSecondLevelList - 查询二级分类列表，parentDictId={}", parentDictId);
        try {
            List<StoreDictionaryVo> result = businessSectionService.getSecondLevelList(parentDictId);
            log.info("BusinessSectionController.getSecondLevelList - 查询成功，共 {} 条数据", result.size());
            return R.data(result);
        } catch (Exception e) {
            log.error("BusinessSectionController.getSecondLevelList ERROR: parentDictId={}, error={}", 
                    parentDictId, e.getMessage(), e);
            return R.fail("查询失败：" + e.getMessage());
        }
    }

    /**
     * 查询经营板块三级分类列表
     * 根据二级分类ID查询对应的三级分类
     *
     * @param parentDictId 二级分类的字典ID（dict_id）
     * @return R<List<StoreDictionaryVo>> 三级分类列表
     * @author alien-cloud
     * @date 2025-12-01
     */
    @ApiOperation("查询经营板块三级分类列表")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentDictId", value = "二级分类字典ID", 
                    dataType = "String", paramType = "query", required = true, example = "11")
    })
    @GetMapping("/getThirdLevelList")
    public R<List<StoreDictionaryVo>> getThirdLevelList(
            @RequestParam("parentDictId") String parentDictId) {
        log.info("BusinessSectionController.getThirdLevelList - 查询三级分类列表，parentDictId={}", parentDictId);
        try {
            List<StoreDictionaryVo> result = businessSectionService.getThirdLevelList(parentDictId);
            log.info("BusinessSectionController.getThirdLevelList - 查询成功，共 {} 条数据", result.size());
            return R.data(result);
        } catch (Exception e) {
            log.error("BusinessSectionController.getThirdLevelList ERROR: parentDictId={}, error={}", 
                    parentDictId, e.getMessage(), e);
            return R.fail("查询失败：" + e.getMessage());
        }
    }

    /**
     * 查询经营板块树形结构（三级联动）
     * 返回完整的三级分类树形结构，包含所有层级的子节点
     *
     * @return R<List<StoreDictionaryVo>> 树形结构数据（包含一级、二级、三级）
     * @author alien-cloud
     * @date 2025-12-01
     */
    @ApiOperation("查询经营板块树形结构（三级联动）")
    @ApiOperationSupport(order = 4)
    @GetMapping("/getTreeStructure")
    public R<List<StoreDictionaryVo>> getTreeStructure() {
        log.info("BusinessSectionController.getTreeStructure - 查询三级树形结构");
        try {
            List<StoreDictionaryVo> result = businessSectionService.getTreeStructure();
            log.info("BusinessSectionController.getTreeStructure - 查询成功");
            return R.data(result);
        } catch (Exception e) {
            log.error("BusinessSectionController.getTreeStructure ERROR: {}", e.getMessage(), e);
            return R.fail("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据字典ID查询完整路径
     * 返回从一级到当前级别的完整分类路径
     *
     * @param dictId 字典ID
     * @return R<List<StoreDictionaryVo>> 分类路径（从一级到当前级）
     * @author alien-cloud
     * @date 2025-12-01
     */
    @ApiOperation("根据字典ID查询完整路径")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictId", value = "字典ID", 
                    dataType = "String", paramType = "query", required = true, example = "1")
    })
    @GetMapping("/getFullPath")
    public R<List<StoreDictionaryVo>> getFullPath(
            @RequestParam("dictId") String dictId) {
        log.info("BusinessSectionController.getFullPath - 查询完整路径，dictId={}", dictId);
        try {
            List<StoreDictionaryVo> result = businessSectionService.getFullPath(dictId);
            log.info("BusinessSectionController.getFullPath - 查询成功");
            return R.data(result);
        } catch (Exception e) {
            log.error("BusinessSectionController.getFullPath ERROR: dictId={}, error={}", 
                    dictId, e.getMessage(), e);
            return R.fail("查询失败：" + e.getMessage());
        }
    }
}

