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

import com.jeesharp.business.modules.mall.application.MallAfterSaleApplicationService;
import com.jeesharp.business.modules.mall.entity.MallAfterSale;
import com.jeesharp.framework.pojo.AjaxResult;
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 java.math.BigDecimal;
import java.util.List;

/**
 * 小程序商城售后申请入口：用户提交退款/退货/换货申请。
 *
 * <p>审核与处理完成在管理端 {@link MallAfterSaleController}；退券在处理完成时由 Application 编排。</p>
 *
 * @author jeesharp
 * @since 2026-05-08
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/mall/afterSale/v1")
@Tag(name = "售后服务")
@AllArgsConstructor
public class MallAfterSaleMobileController {

    private final MallAfterSaleApplicationService afterSaleApplicationService;

    @Operation(summary = "申请售后")
    @PostMapping(value = "apply")
    public AjaxResult<Void> apply(@RequestParam("orderId") String orderId,
                           @RequestParam(value = "orderItemId", required = false) String orderItemId,
                           @RequestParam("memberId") String memberId,
                           @RequestParam(value = "skuId", required = false) String skuId,
                           @RequestParam(value = "spuId", required = false) String spuId,
                           @RequestParam("afterSaleType") String afterSaleType,
                           @RequestParam("applyQuantity") Integer applyQuantity,
                           @RequestParam(value = "refundAmount", required = false) BigDecimal refundAmount,
                           @RequestParam(value = "refundReason", required = false) String refundReason,
                           @RequestParam(value = "refundReasonDesc", required = false) String refundReasonDesc,
                           @RequestParam(value = "refundDesc", required = false) String refundDesc) {

        afterSaleApplicationService.applyAfterSale(orderId, orderItemId, memberId, skuId, spuId,
                afterSaleType, applyQuantity, refundAmount, refundReason, refundReasonDesc, refundDesc);
        return AjaxResult.ok("售后申请提交成功");
    }

    @Operation(summary = "我的售后单列表")
    @PostMapping(value = "myList")
    public AjaxResult<List<MallAfterSale>> myList(@RequestParam("memberId") String memberId) {
        List<MallAfterSale> list = afterSaleApplicationService.getMemberAfterSales(memberId);
        return AjaxResult.okData(list);
    }
}