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/after-sale")
@Tag(name = "管理端 - 交易售后接口")
@Validated
@RequiredArgsConstructor
public class AdminTradeAfterSaleController {

    private final IAdminTradeAfterSaleService tradeAfterSaleService;

    @Operation(summary = "分页查询")
    @PostMapping("/page")
    @SaCheckPermission("admin:after-sale:page")
    public ResultBean<PageListBean<AfterSaleResp>> getAfterSalePage(@Valid @RequestBody AfterSalePageReq pageVO) {
        return ResultBean.success(tradeAfterSaleService.queryPageList(pageVO));
    }

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

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

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

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

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

    @PutMapping("/refund")
    @Operation(summary = "确认退款")
    @SaCheckPermission("admin:after-sale:refund")
    public ResultBean<Boolean> refundAfterSale(@RequestParam("id") Integer id) {
        return ResultBean.success(tradeAfterSaleService.refundAfterSale(id));
    }
}