package com.mall.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.domain.vo.result.PageResult;
import com.mall.common.domain.vo.result.Result;
import com.mall.common.domain.vo.result.ResultCodeEnum;
import com.mall.product.domain.dto.CategoryDto;
import com.mall.product.domain.entity.Category;
import com.mall.product.domain.vo.CategoryVo;
import com.mall.product.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品三级分类 前端控制器
 * </p>
 *
 * @author Bunny
 * @since 2025-07-04 15:19:19
 */
@Tag(name = "商品三级分类", description = "商品三级分类相关接口")
@RestController
@RequestMapping("/api/product/category")
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;

    @Operation(summary = "分页查询商品三级分类", description = "分页商品三级分类")
    @GetMapping("{page}/{limit}")
    public Result<PageResult<CategoryVo>> getCategoryPage(
            @Parameter(name = "page", description = "当前页", required = true)
            @PathVariable("page") Integer page,
            @Parameter(name = "limit", description = "每页记录数", required = true)
            @PathVariable("limit") Integer limit,
            CategoryDto dto) {
        Page<Category> pageParams = new Page<>(page, limit);
        PageResult<CategoryVo> pageResult = categoryService.getCategoryPage(pageParams, dto);
        return Result.success(pageResult);
    }

    @Operation(summary = "添加商品三级分类", description = "添加商品三级分类")
    @PostMapping()
    public Result<String> addCategory(@Valid @RequestBody CategoryDto dto) {
        categoryService.addCategory(dto);
        return Result.success(ResultCodeEnum.ADD_SUCCESS);
    }

    @Operation(summary = "更新商品三级分类", description = "更新商品三级分类")
    @PutMapping()
    public Result<String> updateCategory(@Valid @RequestBody CategoryDto dto) {
        categoryService.updateCategory(dto);
        return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
    }

    @Operation(summary = "删除商品三级分类", description = "删除商品三级分类")
    @DeleteMapping()
    public Result<String> deleteCategory(@RequestBody List<Long> ids) {
        categoryService.deleteCategory(ids);
        return Result.success(ResultCodeEnum.DELETE_SUCCESS);
    }
}