package cn.nianxx.thhotel.workflows.reservation.control;

import cn.nianxx.thhotel.workflows.reservation.common.request.ReservationOrderListQueryRequest;
import cn.nianxx.thhotel.workflows.reservation.common.request.ReservationTaskWorkbenchQueryRequest;
import cn.nianxx.thhotel.workflows.reservation.common.result.ReservationOrderDetailResult;
import cn.nianxx.thhotel.workflows.reservation.common.result.ReservationOrderListResult;
import cn.nianxx.thhotel.workflows.reservation.common.result.ReservationTaskWorkbenchListResult;
import cn.nianxx.thhotel.workflows.reservation.service.ReservationFrontendQueryService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Reservation 前端查询 Controller。提供页面读取接口，不做业务写操作。
 */
@RestController
@RequestMapping("/api/reservation")
public class ReservationFrontendQueryController {

    private final ReservationFrontendQueryService frontendQueryService;

    /**
     * 注入前端查询服务，Controller 只负责 HTTP 参数到查询对象的转换。
     */
    public ReservationFrontendQueryController(ReservationFrontendQueryService frontendQueryService) {
        this.frontendQueryService = frontendQueryService;
    }

    /**
     * 查询任务列表 / 工作台摘要，供前端按任务处理状态展示入口。
     */
    @GetMapping(value = "/tasks", produces = MediaType.APPLICATION_JSON_VALUE)
    public ReservationTaskWorkbenchListResult listTasks(
            @RequestParam(name = "hotel_id", required = false) String hotelId,
            @RequestParam(name = "order_id", required = false) Long orderId,
            @RequestParam(name = "task_type", required = false) String taskType,
            @RequestParam(name = "task_status", required = false) String taskStatus,
            @RequestParam(name = "task_subtype", required = false) String taskSubtype,
            @RequestParam(name = "order_status", required = false) String orderStatus,
            @RequestParam(name = "queue_participation", required = false) Boolean queueParticipation,
            @RequestParam(required = false) String keyword,
            @RequestParam(name = "page_num", required = false) Integer pageNum,
            @RequestParam(name = "page_size", required = false) Integer pageSize) {
        return frontendQueryService.queryTaskWorkbench(new ReservationTaskWorkbenchQueryRequest(
                hotelId,
                orderId,
                taskType,
                taskStatus,
                taskSubtype,
                orderStatus,
                queueParticipation,
                keyword,
                pageNum,
                pageSize));
    }

    /**
     * 查询订单列表，默认返回全部订单状态，并实时聚合未关闭任务数量。
     */
    @GetMapping(value = "/orders", produces = MediaType.APPLICATION_JSON_VALUE)
    public ReservationOrderListResult listOrders(
            @RequestParam(name = "hotel_id", required = false) String hotelId,
            @RequestParam(name = "order_status", required = false) String orderStatus,
            @RequestParam(name = "group_code", required = false) String groupCode,
            @RequestParam(name = "confirmation_number", required = false) String confirmationNumber,
            @RequestParam(required = false) String keyword,
            @RequestParam(name = "page_num", required = false) Integer pageNum,
            @RequestParam(name = "page_size", required = false) Integer pageSize) {
        return frontendQueryService.queryOrders(new ReservationOrderListQueryRequest(
                hotelId,
                orderStatus,
                groupCode,
                confirmationNumber,
                keyword,
                pageNum,
                pageSize));
    }

    /**
     * 查询订单详情和任务时间线，供订单详情页展示当前订单处理脉络。
     */
    @GetMapping(value = "/orders/{orderId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ReservationOrderDetailResult orderDetail(
            @PathVariable Long orderId,
            @RequestParam(name = "hotel_id", required = false) String hotelId,
            @RequestParam(name = "include_tasks", required = false) Boolean includeTasks,
            @RequestParam(name = "include_source_summary", required = false) Boolean includeSourceSummary) {
        return frontendQueryService.getOrderDetail(hotelId, orderId, includeTasks, includeSourceSummary);
    }
}
