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

import com.jeesharp.business.modules.mall.application.MallSpuApplicationService;
import com.jeesharp.business.modules.mall.dto.MallProductSaveDTO;
import com.jeesharp.business.modules.mall.dto.MallSpuAuditDTO;
import com.jeesharp.business.modules.mall.dto.MallSpuSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallSku;
import com.jeesharp.business.modules.mall.vo.MallSettleShelfHintVO;
import com.jeesharp.business.modules.mall.vo.MallSpuInfoVO;
import com.jeesharp.business.modules.mall.vo.MallSpuListVO;
import com.jeesharp.business.modules.mall.vo.MallSpuQueryVO;
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 lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

import java.util.List;

/**
 * 商品SPU管理 Controller
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/manager/business/mall/spu/v1")
@Tag(name = "商品SPU管理")
@AllArgsConstructor
public class MallSpuController extends BaseController {

    private final MallSpuApplicationService mallSpuApplicationService;

    @Operation(summary = "上架前结算/进件状态提示")
    @GetMapping(value = "settleShelfHint")
    public AjaxResult<MallSettleShelfHintVO> settleShelfHint(@RequestParam(value = "spuId", required = false) String spuId) {
        return AjaxResult.okData(mallSpuApplicationService.getSettleShelfHint(spuId));
    }

    @Operation(summary = "获取详情")
    @GetMapping(value = "info")
    public AjaxResult<MallSpuInfoVO> info(@RequestParam("id") String id) {
        MallSpuInfoVO vo = mallSpuApplicationService.get(id);
        return AjaxResult.okData(vo);
    }

    @Operation(summary = "按SPU查询SKU列表（管理端编辑多规格用）")
    @GetMapping(value = "skusBySpu")
    public AjaxResult<List<MallSku>> skusBySpu(@RequestParam("spuId") String spuId) {
        return AjaxResult.okData(mallSpuApplicationService.listSkusBySpuIdForAdmin(spuId));
    }

    @Operation(summary = "获取列表（分页）")
    @PostMapping(value = "list")
    public AjaxResult<PageRespVO<MallSpuListVO>> list(@RequestBody MallSpuQueryVO query) {
        PageRespVO<MallSpuListVO> page = mallSpuApplicationService.findPage(new Page<>(query), query);
        return AjaxResult.okData(page);
    }

    @Operation(summary = "平台商品审核列表（跨租户，默认待审核）")
    @PostMapping(value = "auditList")
    public AjaxResult<PageRespVO<MallSpuListVO>> auditList(@RequestBody MallSpuQueryVO query) {
        return AjaxResult.okData(mallSpuApplicationService.findAuditPage(new Page<>(query), query));
    }

    @Operation(summary = "平台审核商品（通过/驳回）")
    @PostMapping(value = "audit")
    public AjaxResult<Void> audit(@RequestBody @Valid MallSpuAuditDTO dto) {
        mallSpuApplicationService.auditSpu(dto);
        return AjaxResult.ok("审核成功");
    }

    @Operation(summary = "保存")
    @PostMapping(value = "save")
    public AjaxResult<Void> save(@RequestBody @Valid MallSpuSaveDTO dto) {
        mallSpuApplicationService.save(dto);
        return AjaxResult.ok("保存成功");
    }

    @Operation(summary = "保存完整商品信息")
    @PostMapping(value = "saveProduct")
    public AjaxResult<Void> saveProduct(@RequestBody @Valid MallProductSaveDTO dto) {
        mallSpuApplicationService.saveProduct(dto);
        return AjaxResult.ok("商品保存成功");
    }

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