package com.jeesharp.business.modules.mall.controller;

import com.jeesharp.business.modules.mall.application.MallCategorySpecApplicationService;
import com.jeesharp.business.modules.mall.dto.MallCategorySpecSaveDTO;
import com.jeesharp.business.modules.mall.vo.MallSpecOptionVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 类目与可用规格白名单管理端接口。
 *
 * @author jeesharp
 * @since 2026-05-15
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/manager/business/mall/categorySpec/v1")
@Tag(name = "类目可用规格")
@AllArgsConstructor
public class MallCategorySpecController extends BaseController {

    private final MallCategorySpecApplicationService mallCategorySpecApplicationService;

    @Operation(summary = "按类目列出已启用的可选规格（白名单）")
    @GetMapping(value = "listByCategory")
    public AjaxResult<List<MallSpecOptionVO>> listByCategory(@RequestParam("categoryId") String categoryId) {
        return AjaxResult.okData(mallCategorySpecApplicationService.listByCategoryId(categoryId));
    }

    @Operation(summary = "按发布模板汇总可选规格（绑定该模板的所有类目白名单并集）")
    @GetMapping(value = "listByTemplate")
    public AjaxResult<List<MallSpecOptionVO>> listByTemplate(@RequestParam("templateId") String templateId) {
        return AjaxResult.okData(mallCategorySpecApplicationService.listByTemplateId(templateId));
    }

    @Operation(summary = "保存类目规格白名单（全量替换）")
    @PostMapping(value = "save")
    public AjaxResult<Void> save(@RequestBody @Valid MallCategorySpecSaveDTO dto) {
        mallCategorySpecApplicationService.save(dto);
        return AjaxResult.ok("保存成功");
    }
}
