package com.yhy.module.core.controller.admin.resource;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import com.yhy.module.core.controller.admin.infoprice.vo.InfoPriceBookRespVO;
import com.yhy.module.core.controller.admin.resource.vo.*;
import com.yhy.module.core.dal.dataobject.resource.ResourceCatalogItemDO;
import com.yhy.module.core.service.resource.ResourceCatalogItemService;
import com.yhy.module.core.service.resource.ResourceCategoryTreeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "管理后台 - 工料机分类/条目")
@RestController
@RequestMapping("/core/resource/catalog-items")
@Validated
public class ResourceCatalogItemController {

    @Resource
    private ResourceCatalogItemService itemService;
    
    @Resource
    private ResourceCategoryTreeService categoryTreeService;

    @GetMapping("/{catalogId}/tree")
    @Operation(summary = "获取分类/条目树（树形结构）")
    public CommonResult<List<ResourceCatalogItemTreeNodeVO>> tree(@PathVariable("catalogId") Long catalogId) {
        return success(itemService.getTreeByCatalog(catalogId));
    }
    
    @GetMapping("/{catalogId}/list")
    @Operation(summary = "获取分类/条目列表（扁平结构）")
    public CommonResult<List<ResourceCatalogItemDO>> list(@PathVariable("catalogId") Long catalogId) {
        return success(itemService.listByCatalog(catalogId));
    }
    
    @GetMapping("/list-level")
    @Operation(summary = "获取指定层级的分类/条目列表", description = "查询指定path长度的所有节点,默认为2(第二层),如path={1,10}")
    public CommonResult<List<ResourceCatalogItemDO>> listByLevel(
            @io.swagger.v3.oas.annotations.Parameter(description = "层级深度,默认为2") 
            @org.springframework.web.bind.annotation.RequestParam(value = "pathLength", required = false, defaultValue = "2") Integer pathLength) {
        // 忽略租户过滤，查询所有租户数据，然后在 Service 层排除租户1
        List<ResourceCatalogItemDO> result = TenantUtils.executeIgnore(() ->
                itemService.listByPathLevel(pathLength)
        );
        return success(result);
    }
    
    @GetMapping("/{id}/allowed-categories")
    @Operation(summary = "获取目录树节点允许的类别列表", description = "用于创建工料机项时，限制类别选择范围")
    public CommonResult<List<ResourceCategorySimpleRespVO>> getAllowedCategories(@PathVariable("id") Long id) {
        return success(itemService.getAllowedCategories(id));
    }

    @PostMapping
    @Operation(summary = "创建分类/条目")
    public CommonResult<Long> create(@Valid @RequestBody CatalogItemSaveReqVO reqVO) {
        return success(itemService.create(reqVO));
    }

    @PutMapping
    @Operation(summary = "更新分类/条目")
    public CommonResult<Boolean> update(@Valid @RequestBody CatalogItemSaveReqVO reqVO) {
        itemService.update(reqVO);
        return success(true);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除分类/条目")
    public CommonResult<Boolean> delete(@PathVariable("id") Long id) {
        itemService.delete(id);
        return success(true);
    }
    
    @PutMapping("/swap-sort")
    @Operation(summary = "交换两个节点的排序")
    public CommonResult<Boolean> swapSortOrder(@Valid @RequestBody SwapSortOrderReqVO reqVO) {
        itemService.swapSortOrder(reqVO.getNodeId1(), reqVO.getNodeId2());
        return success(true);
    }
    
    @PutMapping("/drag")
    @Operation(summary = "拖动节点到指定位置")
    public CommonResult<Boolean> dragNode(@Valid @RequestBody DragNodeReqVO reqVO) {
        itemService.dragNode(reqVO.getDragNodeId(), reqVO.getTargetNodeId(), reqVO.getPosition());
        return success(true);
    }

}
