package com.amms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.amms.domain.vo.Result;
import com.amms.domain.Reservation;
import com.amms.service.IReservationService;
import com.amms.service.IReservationVisitorService;
import com.amms.domain.ReservationVisitor;

import java.util.List;
import java.util.Date;

/**
 * 预约Controller
 */
@RestController
@RequestMapping("/reservation")
public class ReservationController {

    @Autowired
    private IReservationService reservationService;

    @Autowired
    private IReservationVisitorService reservationVisitorService;

    /**
     * 查询预约列表
     */
    @GetMapping("/list")
    public PageInfo list(Reservation reservation, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Reservation> reservations = reservationService.selectReservationList(reservation);
        for (Reservation r : reservations) {
            ReservationVisitor query = new ReservationVisitor();
            query.setReservationId(r.getId());
            List<ReservationVisitor> visitors = reservationVisitorService.selectReservationVisitorList(query);
            r.setReservationVisitors(visitors);
        }
        return new PageInfo(reservations);
    }

    /**
     * 查询全部预约列表
     */
    @GetMapping("/listAll")
    public List<Reservation> listAll(Reservation reservation) {
        List<Reservation> reservations = reservationService.selectReservationList(reservation);
        for (Reservation r : reservations) {
            ReservationVisitor query = new ReservationVisitor();
            query.setReservationId(r.getId());
            List<ReservationVisitor> visitors = reservationVisitorService.selectReservationVisitorList(query);
            r.setReservationVisitors(visitors);
        }
        return reservations;
    }

    /**
     * 获取预约详细信息
     */
    @GetMapping(value = "/info/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        Reservation r = reservationService.selectReservationById(id);
        if (r != null) {
            ReservationVisitor query = new ReservationVisitor();
            query.setReservationId(r.getId());
            List<ReservationVisitor> visitors = reservationVisitorService.selectReservationVisitorList(query);
            r.setReservationVisitors(visitors);
        }
        return Result.success(r);
    }

    /**
     * 新增预约
     */
    @PostMapping("/add")
    public Result add(@RequestBody Reservation reservation) {
        return reservationService.insertReservation(reservation) > 0 ? Result.success("新增成功") : Result.error("新增失败");
    }

    /**
     * 修改预约
     */
    @PutMapping
    public Result update(@RequestBody Reservation reservation) {
        return reservationService.updateReservation(reservation) > 0 ? Result.success("修改成功") : Result.error("修改失败");
    }

    /**
     * 审核预约
     *
     * 前端传入：id（预约ID）、status（审核状态：1通过 3驳回）、remark（可选审核备注）
     * 审核通过时：
     * 1）为整单生成核验二维码链接，存入 Reservation.qrCode 字段（扫码后整单游客核验状态置为1）
     * 2）为每位游客生成个人核验二维码链接，存入 ReservationVisitor.visitorQrCode 字段（扫码后该游客核验状态置为1）
     */
    @PutMapping("/audit")
    public Result audit(@RequestBody Reservation reservation) {
        if (reservation == null || reservation.getId() == null || reservation.getStatus() == null) {
            return Result.error("参数不完整：缺少预约ID或审核状态");
        }
        return reservationService.auditReservation(reservation) > 0 ? Result.success("审核成功") : Result.error("审核失败");
    }

    /**
     * 整单核验：将该预约下所有游客核验状态置为1
     */
    @GetMapping("/verifyAll/{reservationId}")
    public Result verifyAll(@PathVariable("reservationId") Long reservationId) {
        int n = reservationService.verifyAllVisitors(reservationId);
        return n >= 0 ? Result.success("整单核验成功") : Result.error("整单核验失败");
    }

    /**
     * 游客个人核验：将该游客核验状态置为1
     */
    @GetMapping("/verifyVisitor/{visitorId}")
    public Result verifyVisitor(@PathVariable("visitorId") Long visitorId) {
        int n = reservationService.verifyVisitor(visitorId);
        if (n == 0) return Result.error("游客信息不存在或核验失败");
        return Result.success("游客核验成功");
    }

    /**
     * 删除预约
     *
     * 同步删除该预约下的游客预约明细（reservation_visitor），保证数据一致性
     */
	@DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        // 先删除当前预约ID下的所有游客明细
        reservationVisitorService.deleteReservationVisitorsByReservationId(id);
        return reservationService.deleteReservationById(id) > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }
}
