package com.seer.teach.mall.good.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.common.enums.GoodsAttributeScopeEnum;
import com.seer.teach.mall.good.service.IAdminGoodsAttributeValueService;
import com.seer.teach.mall.request.GoodsAttributeValuePageReq;
import com.seer.teach.mall.request.GoodsAttributeValueReq;
import com.seer.teach.mall.response.GoodsAttributeValueResp;
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.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
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;

import java.util.List;

/**
 * <p>
 *  商家商品属性值控制器
 * </p>
 *
 * @author 
 * @since 2025-06-12
 */
@RestController
@RequestMapping("/mall/merchant/goods/attribute/value")
@Tag(name = "商家端 - 商品属性值管理")
@Validated
@RequiredArgsConstructor
@SaCheckLogin(type = StpUtil.TYPE)
public class MerchantGoodsAttributeValueController {

    private final IAdminGoodsAttributeValueService goodsAttributeValueService;

    @PostMapping("/page")
    @LogPrint
    @Operation(summary = "商品属性值分页查询")
    @SaCheckPermission("merchant:goods:attribute:value:page")
    public ResultBean<PageListBean<GoodsAttributeValueResp>> getPage(
            @RequestBody @Validated GoodsAttributeValuePageReq pageReq) {
        // 商家可以查看平台属性值和自己的属性值
        pageReq.setScope(null);
        return ResultBean.success(goodsAttributeValueService.getPage(pageReq));
    }

    @GetMapping("/{id}")
    @LogPrint
    @Operation(summary = "根据ID查询商品属性值")
    @SaCheckPermission("merchant:goods:attribute:value:detail")
    public ResultBean<GoodsAttributeValueResp> getById(
            @PathVariable @Parameter(description = "ID") Integer id) {
        return ResultBean.success(goodsAttributeValueService.getById(id));
    }

    @PostMapping
    @LogPrint
    @Operation(summary = "新增商品属性值")
    @SaCheckPermission("merchant:goods:attribute:value:save")
    public ResultBean<Boolean> save(
            @RequestBody @Validated GoodsAttributeValueReq req) {
        req.setScope(GoodsAttributeScopeEnum.MERCHANT.getCode());
        req.setMerchantId(StpUtil.getLoginIdAsInt());
        return ResultBean.success(goodsAttributeValueService.save(req));
    }

    @PutMapping
    @LogPrint
    @Operation(summary = "修改商品属性值")
    @SaCheckPermission("merchant:goods:attribute:value:update")
    public ResultBean<Boolean> update(
            @RequestBody @Validated GoodsAttributeValueReq req) {
        return ResultBean.success(goodsAttributeValueService.update(req));
    }

    @DeleteMapping
    @LogPrint
    @Operation(summary = "删除商品属性值")
    @SaCheckPermission("merchant:goods:attribute:value:delete")
    public ResultBean<Boolean> delete(
            @Parameter(description = "ID列表")
            @RequestBody @NotEmpty List<Integer> ids) {
        return ResultBean.success(goodsAttributeValueService.delete(ids));
    }
}