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.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.MallOrderEvaluateBo;
import org.dromara.mall.trade.domain.vo.MallOrderEvaluateVo;
import org.dromara.mall.trade.service.MallOrderEvaluateService;
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/evaluate")
public class AppMallOrderEvaluateController {

    private final MallOrderEvaluateService mallOrderEvaluateService;

    /**
     * 查询订单评价列表（移动端）
     */
    @Operation(summary = "查询订单评价列表", description = "查询订单评价列表")
    @GetMapping("/list")
    public R<TableDataInfo<MallOrderEvaluateVo>> list(@Parameter(description = "评价查询条件") MallOrderEvaluateBo bo,
                                                      @Parameter(description = "分页参数") PageQuery pageQuery) {
        TableDataInfo<MallOrderEvaluateVo> result = mallOrderEvaluateService.queryPageList(bo, pageQuery);
        return R.ok(result);
    }

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

    /**
     * 根据商品ID查询评价列表
     */
    @Operation(summary = "查询商品评价列表", description = "根据商品ID查询评价列表（仅已通过审核的）")
    @GetMapping("/product/{productId}")
    public R<TableDataInfo<MallOrderEvaluateVo>> listByProductId(@Parameter(description = "商品ID", required = true)
                                                                 @NotNull(message = "商品ID不能为空")
                                                                 @PathVariable Long productId,
                                                                 @Parameter(description = "分页参数") PageQuery pageQuery) {
        MallOrderEvaluateBo bo = new MallOrderEvaluateBo();
        bo.setProductId(productId);
        bo.setStatus(1); // 只查询已通过审核的评价
        TableDataInfo<MallOrderEvaluateVo> result = mallOrderEvaluateService.queryPageList(bo, pageQuery);
        return R.ok(result);
    }

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

    /**
     * 新增订单评价
     */
    @Operation(summary = "新增订单评价", description = "提交订单评价")
    @PostMapping
    public R<Void> add(@Parameter(description = "评价信息") @Validated @RequestBody MallOrderEvaluateBo bo) {
        return R.ok(mallOrderEvaluateService.insertByBo(bo));
    }
}
