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

import com.jeesharp.business.modules.mall.application.MallAfterSaleApplicationService;
import com.jeesharp.business.modules.mall.dto.MallAfterSaleSaveDTO;
import com.jeesharp.business.modules.mall.vo.MallAfterSaleInfoVO;
import com.jeesharp.business.modules.mall.vo.MallAfterSaleListVO;
import com.jeesharp.business.modules.mall.vo.MallAfterSaleQueryVO;
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;

/**
 * 管理端商城售后单接口：审核与处理完成（含退款类售后退券）。
 *
 * <p>用户申请在 C 端 {@link MallAfterSaleMobileController}；
 * 退款完成时 {@link com.jeesharp.business.modules.mall.application.MallAfterSaleApplicationService#handleAfterSale}
 * 对「退款中」单调用 {@code CouponCheckoutApi.returnOnRefund}。</p>
 *
 * @author jeesharp
 * @since 2026-05-08
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/manager/business/mall/afterSale/v1")
@Tag(name = "售后单管理")
@AllArgsConstructor
public class MallAfterSaleController extends BaseController {

    private final MallAfterSaleApplicationService mallAfterSaleApplicationService;

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

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

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

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

    @Operation(summary = "审核售后单")
    @PostMapping(value = "audit")
    public AjaxResult<Void> audit(@RequestParam("afterSaleId") String afterSaleId,
                           @RequestParam("status") Integer status,
                           @RequestParam("auditBy") String auditBy,
                           @RequestParam(value = "auditRemark", required = false) String auditRemark) {
        mallAfterSaleApplicationService.auditAfterSale(afterSaleId, status, auditBy, auditRemark);
        return AjaxResult.ok("审核成功");
    }

    /**
     * 售后处理完成：财务退款/退货/换货落库后调用；若原状态为退款中则同步退回订单已用优惠券。
     */
    @Operation(summary = "处理售后单")
    @PostMapping(value = "handle")
    public AjaxResult<Void> handle(@RequestParam("afterSaleId") String afterSaleId,
                            @RequestParam("handlerId") String handlerId,
                            @RequestParam(value = "handleRemark", required = false) String handleRemark) {
        mallAfterSaleApplicationService.handleAfterSale(afterSaleId, handlerId, handleRemark);
        return AjaxResult.ok("处理成功");
    }

    @Operation(summary = "发起微信退款")
    @PostMapping(value = "triggerRefund")
    public AjaxResult<Void> triggerRefund(@RequestParam("afterSaleId") String afterSaleId,
                                          @RequestParam("operatorId") String operatorId) {
        mallAfterSaleApplicationService.triggerRefund(afterSaleId, operatorId);
        return AjaxResult.ok("退款已提交");
    }
}
