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

import com.jeesharp.business.modules.mall.application.MallProductTemplateApplicationService;
import com.jeesharp.business.modules.mall.dto.MallProductTemplateSaveDTO;
import com.jeesharp.business.modules.mall.vo.MallProductTemplateInfoVO;
import com.jeesharp.business.modules.mall.vo.MallProductTemplateListVO;
import com.jeesharp.business.modules.mall.vo.MallProductTemplateQueryVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
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;

/**
 * 商品发布模板管理端 REST，含按分类解析接口供 SPU 编辑页加载规格与属性定义。
 *
 * @author jeesharp
 * @since 2026-05-15
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/manager/business/mall/productTemplate/v1")
@Tag(name = "商品发布模板")
@AllArgsConstructor
public class MallProductTemplateController extends BaseController {

    private final MallProductTemplateApplicationService mallProductTemplateApplicationService;

    @Operation(summary = "获取详情（含明细行）")
    @GetMapping(value = "info")
    public AjaxResult<MallProductTemplateInfoVO> info(@RequestParam("id") String id) {
        return AjaxResult.okData(mallProductTemplateApplicationService.get(id));
    }

    @Operation(summary = "按类目解析模板（含明细）；未绑定时 data 为 null")
    @GetMapping(value = "resolveByCategory")
    public AjaxResult<MallProductTemplateInfoVO> resolveByCategory(@RequestParam("categoryId") String categoryId) {
        return AjaxResult.okData(mallProductTemplateApplicationService.resolveByCategoryId(categoryId));
    }

    @Operation(summary = "分页列表")
    @PostMapping(value = "list")
    public AjaxResult<PageRespVO<MallProductTemplateListVO>> list(@RequestBody MallProductTemplateQueryVO query) {
        PageRespVO<MallProductTemplateListVO> page =
            mallProductTemplateApplicationService.findPage(new Page<>(query), query);
        return AjaxResult.okData(page);
    }

    @Operation(summary = "保存（头+明细全量替换），成功返回模板 ID")
    @PostMapping(value = "save")
    public AjaxResult<String> save(@RequestBody @Valid MallProductTemplateSaveDTO dto) {
        String id = mallProductTemplateApplicationService.save(dto);
        return AjaxResult.ok("保存成功", id);
    }

    @Operation(summary = "删除")
    @GetMapping(value = "delete")
    public AjaxResult<Void> delete(@RequestParam("ids") String ids) {
        mallProductTemplateApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }
}
