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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.resource.vo.ResourceCategorySimpleRespVO;
import com.yhy.module.core.controller.admin.resource.vo.ResourceCategoryTreeMappingSaveReqVO;
import com.yhy.module.core.controller.admin.resource.vo.ResourceCategoryTreeNodeVO;
import com.yhy.module.core.controller.admin.resource.vo.ResourceCategoryTreeSaveReqVO;
import com.yhy.module.core.controller.admin.resource.vo.SwapSortOrderReqVO;
import com.yhy.module.core.service.resource.ResourceCategoryTreeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

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

@Tag(name = "管理后台 - 工料机机类树")
@RestController
@RequestMapping("/core/resource/category-tree")
@Validated
public class ResourceCategoryTreeController {

    @Resource
    private ResourceCategoryTreeService categoryTreeService;

    @PostMapping
    @Operation(summary = "创建工料机机类树节点")
    public CommonResult<Long> createNode(@Valid @RequestBody ResourceCategoryTreeSaveReqVO reqVO) {
        return success(categoryTreeService.createNode(reqVO));
    }

    @PutMapping
    @Operation(summary = "更新工料机机类树节点")
    public CommonResult<Boolean> updateNode(@Valid @RequestBody ResourceCategoryTreeSaveReqVO reqVO) {
        categoryTreeService.updateNode(reqVO);
        return success(true);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除工料机机类树节点")
    @Parameter(name = "id", description = "节点ID", required = true)
    public CommonResult<Boolean> deleteNode(@PathVariable("id") Long id) {
        categoryTreeService.deleteNode(id);
        return success(true);
    }

    @GetMapping("/tree")
    @Operation(summary = "获取工料机机类树")
    public CommonResult<List<ResourceCategoryTreeNodeVO>> getTree() {
        return success(categoryTreeService.getTree());
    }

    @GetMapping("/{treeNodeId}/categories")
    @Operation(summary = "获取指定节点下的类别列表（简化版）")
    @Parameter(name = "treeNodeId", description = "树节点ID", required = true)
    public CommonResult<List<ResourceCategorySimpleRespVO>> getCategoriesByTreeNode(@PathVariable("treeNodeId") Long treeNodeId) {
        return success(categoryTreeService.getCategoriesByTreeNode(treeNodeId));
    }

    @PostMapping("/mapping")
    @Operation(summary = "添加树节点与类别的关联")
    public CommonResult<Boolean> addCategoryMapping(@Valid @RequestBody ResourceCategoryTreeMappingSaveReqVO reqVO) {
        categoryTreeService.addCategoryMapping(reqVO);
        return success(true);
    }

    @DeleteMapping("/mapping/{treeNodeId}/{categoryId}")
    @Operation(summary = "删除树节点与类别的关联")
    @Parameter(name = "treeNodeId", description = "树节点ID", required = true)
    @Parameter(name = "categoryId", description = "类别ID", required = true)
    public CommonResult<Boolean> removeCategoryMapping(
            @PathVariable("treeNodeId") Long treeNodeId,
            @PathVariable("categoryId") Long categoryId) {
        categoryTreeService.removeCategoryMapping(treeNodeId, categoryId);
        return success(true);
    }
    
    @PutMapping("/swap-sort")
    @Operation(summary = "交换两个节点的排序")
    public CommonResult<Boolean> swapSortOrder(@Valid @RequestBody SwapSortOrderReqVO reqVO) {
        categoryTreeService.swapSortOrder(reqVO.getNodeId1(), reqVO.getNodeId2());
        return success(true);
    }
}
