package org.dromara.mall.trade.controller.app;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.mall.trade.domain.bo.MallAfterSaleBo;
import org.dromara.mall.trade.domain.vo.MallAfterSaleVo;
import org.dromara.mall.trade.service.MallAfterSaleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 售后申请（移动端）
 *
 * @author pengles
 */
@Tag(name = "移动端-售后申请", description = "售后申请接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/mall/order/after-sale")
public class AppMallAfterSaleController {

    private final MallAfterSaleService mallAfterSaleService;

    /**
     * 查询售后申请列表（移动端）
     */
    @Operation(summary = "查询售后申请列表", description = "查询用户的售后申请列表")
    @GetMapping("/list")
    public R<TableDataInfo<MallAfterSaleVo>> list(@Parameter(description = "售后申请查询条件") MallAfterSaleBo bo,
                                                  @Parameter(description = "分页参数") PageQuery pageQuery) {
        TableDataInfo<MallAfterSaleVo> result = mallAfterSaleService.queryPageList(bo, pageQuery);
        return R.ok(result);
    }

    /**
     * 根据订单ID查询售后申请列表
     */
    @Operation(summary = "查询订单售后申请列表", description = "根据订单ID查询售后申请列表")
    @GetMapping("/order/{orderId}")
    public R<List<MallAfterSaleVo>> listByOrderId(@Parameter(description = "订单ID", required = true)
                                                  @NotNull(message = "订单ID不能为空")
                                                  @PathVariable Long orderId) {
        MallAfterSaleBo bo = new MallAfterSaleBo();
        bo.setOrderId(orderId);
        List<MallAfterSaleVo> list = mallAfterSaleService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 获取售后申请详细信息
     */
    @Operation(summary = "获取售后申请详情", description = "根据售后申请ID获取详细信息")
    @GetMapping("/{id}")
    public R<MallAfterSaleVo> getInfo(@Parameter(description = "售后申请ID", required = true)
                                      @NotNull(message = "主键不能为空")
                                      @PathVariable Long id) {
        return R.ok(mallAfterSaleService.queryById(id));
    }

    /**
     * 新增售后申请
     */
    @Operation(summary = "新增售后申请", description = "提交售后申请（退款/退货/换货）")
    @PostMapping
    public R<Void> add(@Parameter(description = "售后申请信息") @Validated @RequestBody MallAfterSaleBo bo) {
        return R.ok(mallAfterSaleService.insertByBo(bo));
    }

    /**
     * 用户填写退货物流信息
     */
    @Operation(summary = "填写退货物流信息", description = "用户填写退货物流信息（仅退货和换货类型）")
    @PutMapping("/logistics/{id}")
    public R<Void> fillLogistics(@Parameter(description = "售后申请ID", required = true)
                                 @NotNull(message = "主键不能为空")
                                 @PathVariable Long id,
                                 @Parameter(description = "物流公司", required = true)
                                 @NotBlank(message = "物流公司不能为空")
                                 @RequestParam String logisticsCompany,
                                 @Parameter(description = "物流单号", required = true)
                                 @NotBlank(message = "物流单号不能为空")
                                 @RequestParam String logisticsNo) {
        return R.ok(mallAfterSaleService.fillLogistics(id, logisticsCompany, logisticsNo));
    }

    /**
     * 取消售后申请
     */
    @Operation(summary = "取消售后申请", description = "用户取消售后申请")
    @PutMapping("/cancel/{id}")
    public R<Void> cancel(@Parameter(description = "售后申请ID", required = true)
                          @NotNull(message = "主键不能为空")
                          @PathVariable Long id) {
        return R.ok(mallAfterSaleService.cancel(id));
    }
}
