package shop.alien.store.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import shop.alien.entity.result.R;
import shop.alien.store.service.StoreReservationService;
import shop.alien.store.service.UserReservationService;

/**
 * 预订相关定时任务回调（供 XXL-JOB 通过 Feign 调用）
 */
@Slf4j
@Api(tags = {"预订定时任务"})
@RestController
@RequestMapping("/reservation/job")
@RequiredArgsConstructor
public class ReservationJobController {

    private final UserReservationService userReservationService;
    private final StoreReservationService storeReservationService;

    @ApiOperation("未到店超时：保留位置=开始时间+保留分钟，否则仅开始时间；订单待使用则标记已过期")
    @PostMapping("/markTimeout")
    public R<Integer> markReservationTimeoutByEndTime() {
        log.info("reservation job: markTimeout 开始");
        int count = userReservationService.markReservationTimeoutByEndTime();
        log.info("reservation job: markTimeout 结束，更新条数={}", count);
        return R.data(count);
    }

    @ApiOperation("到店提醒：订单待使用且距预定开始时间≤30分钟时发送短信提醒")
    @PostMapping("/sendArrivalReminder")
    public R<Integer> sendArrivalReminder() {
        log.info("reservation job: sendArrivalReminder 开始");
        int count = userReservationService.sendArrivalReminderSms();
        log.info("reservation job: sendArrivalReminder 结束，发送条数={}", count);
        return R.data(count);
    }

    @ApiOperation("重试退款：查询支付状态为退款中的订单并重新发起退款（不发送短信和通知）")
    @PostMapping("/retryRefundFailed")
    public R<Integer> retryRefundFailed() {
        log.info("reservation job: retryRefundFailed 开始");
        int count = storeReservationService.retryRefundFailedOrders();
        log.info("reservation job: retryRefundFailed 结束，成功退款条数={}", count);
        return R.data(count);
    }
}
