package shop.alien.store.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.store.service.ReservationOrderListService;
import shop.alien.store.service.ReservationOrderPageService;
import shop.alien.store.service.UserReservationService;
import shop.alien.store.vo.ReservationOrderListResultVo;
import shop.alien.store.vo.ReservationOrderPageVo;

/**
 * 预订订单页面展示接口（按订单ID查询）、预订订单列表（店铺名称模糊搜索）
 *
 * @author system
 */
@Slf4j
@Api(tags = {"预订订单页面"})
@RestController
@RequestMapping("/store/reservationOrder")
@RequiredArgsConstructor
public class ReservationOrderPageController {

    private final ReservationOrderPageService reservationOrderPageService;
    private final ReservationOrderListService reservationOrderListService;
    private final UserReservationService userReservationService;

    @ApiOperation("预订页面展示（按订单ID）")
    @ApiImplicitParam(name = "orderId", value = "预订订单ID（user_reservation_order.id）", required = true, paramType = "query", dataType = "int")
    @GetMapping("/page")
    public R<ReservationOrderPageVo> getPage(@RequestParam Integer orderId) {
        log.info("ReservationOrderPageController.getPage orderId={}", orderId);
        ReservationOrderPageVo vo = reservationOrderPageService.getPageByOrderId(orderId);
        if (vo == null) {
            return R.fail("订单不存在");
        }
        return R.data(vo);
    }

    @ApiOperation("预订订单列表（店铺名称模糊搜索，限10字；展示店铺名/入口图/状态/预订信息；返回全部/待使用/已完成/已退款数量）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "storeName", value = "店铺名称，模糊搜索，最多10字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态 0待支付 1待使用 2已完成 3已过期 4已取消 5已关闭 6退款中 7已退款", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "current", value = "页码", paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", dataType = "long")
    })
    @GetMapping("/list")
    public R<ReservationOrderListResultVo> list(
            @RequestParam Integer userId,
            @RequestParam(required = false) String storeName,
            @RequestParam(required = false) Integer orderStatus,
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size) {
        log.info("ReservationOrderPageController.list userId={}, storeName={}, orderStatus={}, current={}, size={}", userId, storeName, orderStatus, current, size);
        if (userId == null) {
            return R.fail("用户ID不能为空");
        }
        ReservationOrderListResultVo result = reservationOrderListService.listPage(userId, storeName, orderStatus, current, size);
        return R.data(result);
    }

    @ApiOperation("预定订单删除（逻辑删除订单及预定信息）")
    @ApiImplicitParam(name = "orderId", value = "预订订单ID（user_reservation_order.id）", required = true, paramType = "query", dataType = "int")
    @PostMapping("/delete")
    public R<String> delete(@RequestParam Integer orderId) {
        log.info("ReservationOrderPageController.delete orderId={}", orderId);
        if (orderId == null) {
            return R.fail("订单ID不能为空");
        }
        try {
            boolean ok = userReservationService.deleteOrderAndReservationByOrderId(orderId);
            return ok ? R.success("删除成功") : R.fail("删除失败");
        } catch (Exception e) {
            log.error("预定订单删除失败 orderId={}", orderId, e);
            return R.fail(e.getMessage());
        }
    }
}
