package com.seer.teach.mall.order.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.mall.order.service.IAdminTradeAfterSaleService;
import com.seer.teach.mall.request.AfterSaleDisagreeReq;
import com.seer.teach.mall.request.AfterSalePageReq;
import com.seer.teach.mall.request.AfterSaleRefuseReq;
import com.seer.teach.mall.response.AfterSaleResp;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
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 jakarta.validation.Valid;

import static com.seer.teach.common.ResultBean.success;

/**
 * <p>
 * 商户交易售后 前端控制器
 * </p>
 *
 * @author Captain
 * @since 2025-06-21
 */
@RestController
@RequestMapping("/mall/merchant/after-sale")
@Tag(name = "商家端 - 交易售后接口")
@Validated
@RequiredArgsConstructor
public class MerchantTradeAfterSaleController {

    private final IAdminTradeAfterSaleService tradeAfterSaleService;

    @Operation(summary = "商家分页查询")
    @PostMapping("/paged")
    @SaCheckPermission("admin:merchant:after-sale:paged")
    public ResultBean<PageListBean<AfterSaleResp>> getMerchantAfterSalePage(@Valid @RequestBody AfterSalePageReq pageVO) {
        Integer userId = StpUtil.getLoginIdAsInt();
        pageVO.setMerchantUserId(userId);
        return ResultBean.success(tradeAfterSaleService.queryPageList(pageVO));
    }

    @GetMapping("/detail")
    @Operation(summary = "获得售后订单详情")
    @SaCheckPermission("admin:merchant:after-sale:detail")
    public ResultBean<AfterSaleResp> getTradeAfterSaleOrderDetail(@RequestParam("id") Integer id) {
        Integer userId = StpUtil.getLoginIdAsInt();
        return success(tradeAfterSaleService.getTradeAfterSaleOrderDetail(id,userId));
    }

    @PutMapping("/agree")
    @Operation(summary = "同意售后")
    @SaCheckPermission("admin:merchant:after-sale:agree")
    public ResultBean<Boolean> agreeAfterSale(@RequestParam("id") Integer id) {
        return ResultBean.success(tradeAfterSaleService.agreeAfterSaleApplyMerchant(id,StpUtil.getLoginIdAsInt()));
    }

    @PutMapping("/disagree")
    @Operation(summary = "拒绝售后")
    @SaCheckPermission("admin:merchant:after-sale:disagree")
    public ResultBean<Boolean> disagreeAfterSale(@RequestBody AfterSaleDisagreeReq disagreeReq) {
        disagreeReq.setMerchantUserId(StpUtil.getLoginIdAsInt());
        return ResultBean.success(tradeAfterSaleService.disagreeAfterSaleApplyMerchant(disagreeReq,StpUtil.getLoginIdAsInt()));
    }

    @PutMapping("/receive")
    @Operation(summary = "确认收货")
    @SaCheckPermission("admin:merchant:after-sale:receive")
    public ResultBean<Boolean> receiveAfterSale(@RequestParam("id") Integer id) {
        return ResultBean.success(tradeAfterSaleService.confirmReceiveMerchant(id,StpUtil.getLoginIdAsInt()));
    }

    @PutMapping("/refuse")
    @Operation(summary = "拒绝收货")
    @SaCheckPermission("admin:merchant:after-sale:refuse")
    public ResultBean<Boolean> refuseAfterSale(AfterSaleRefuseReq refuseReqVO) {
        return ResultBean.success(tradeAfterSaleService.confirmRejectionMerchant(refuseReqVO,StpUtil.getLoginIdAsInt()));
    }

}