<?php

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

include_once '../config/database.php';
include_once '../objects/reservation.php'; 
include_once '../objects/reservation_detail.php'; 
include_once '../objects/deposit.php';
include_once '../helpers/rental_metadata.php';

session_start();
date_default_timezone_set('Asia/Bangkok');

$database = new Database();
$db = $database->getConnection();

$reservation = new Reservation($db);
$reservation_detail = new ReservationDetail($db);
$deposit = new Deposit($db);

$data = json_decode(file_get_contents("php://input"));
$booking_meta = parse_booking_metadata($data->remark ?? '');

// $reservation->reservation_no = 'R-' . date('ym') . '-' . uniqid();
$reservation->date_time = $data->date_time;
$reservation->reservation_no = $data->reservation_no;
$reservation->reservation_no_old = $data->reservation_no_old;
$reservation->folio_no = $data->folio_no;
$reservation->discount_amount = isset($data->discount_amount) ? floatval($data->discount_amount) : 0;
$reservation->agent_id = $data->agent_id;
$reservation->agent_name = $data->agent_name;
$reservation->guest_id = $data->guest_id;
$reservation->guest_name = $data->guest_name;
$reservation->remark = $data->remark;
$reservation->rental_mode = $booking_meta['rental_mode'] ?: 'daily';
$reservation->pickup_datetime = $booking_meta['pickup_datetime'];
$reservation->return_datetime = $booking_meta['return_datetime'];
$reservation->rental_hours = $booking_meta['rental_hours'] ?: 0;
if ($reservation->pickup_datetime === '' || $reservation->return_datetime === '') {
    $inferred_period = infer_booking_period_from_details($data->stayDetails ?? array());
    $reservation->pickup_datetime = $inferred_period['pickup_datetime'];
    $reservation->return_datetime = $inferred_period['return_datetime'];
    $reservation->rental_hours = $inferred_period['rental_hours'];
}
$reservation->adult = $data->adult ?? 1;
$reservation->child = $data->child ?? 0;

$reservation->room_type = $data->room_type;
$reservation->bed_type = $data->bed_type;
$reservation->room_no = $data->room_no;

$reservation->entry_by = isset($_SESSION['user']) ? $_SESSION['user'] : "";
$reservation->update_by = isset($_SESSION['user']) ? $_SESSION['user'] : "";

$reservation_detail->reservation_no_old = $reservation->reservation_no_old; 
$reservation_detail->reservation_no = $reservation->reservation_no; 

$deposit->reservation_no_old = $reservation->reservation_no_old; 
$deposit->reservation_no = $reservation->reservation_no; 

if ($reservation->hasAvailabilityConflict()) {
    echo json_encode(array("message" => "vehicle_unavailable"));
    die();
}

$db->beginTransaction();

if (!$reservation_detail->DeleteReservationReal()) {
    $db->rollBack();
    echo json_encode(array("message" => "error"));
    die();
}

foreach ($data->stayDetails as $detail) {

    $reservation_detail->date = $detail->date;    
    $reservation_detail->description = $detail->description ?? '';
    $reservation_detail->rate_mode = $detail->rate_mode ?? $reservation->rental_mode;
    $reservation_detail->billing_units = $detail->billing_units ?? 1;
    $reservation_detail->unit_rate = $detail->unit_rate ?? $detail->room_rate;
    $reservation_detail->room_rate = $detail->room_rate; 
    $reservation_detail->extra_bed = $detail->extra_bed; 
    $reservation_detail->extra_bed_rate = $detail->extra_bed_rate; 
    $reservation_detail->extra_person = $detail->extra_person;
    $reservation_detail->extra_person_rate = $detail->extra_person_rate;
    if (!$reservation_detail->create()) {
        $db->rollBack();
        echo json_encode(array("message" => "error"));
        die();
    }

}

if (!$deposit->updateReservation()) {
    $db->rollBack();
    echo json_encode(array("message" => "error"));
    die();
}

if ($reservation->updateReservation()) {
    $db->commit();
    echo json_encode(array("message" => "created"));
    die();
}

$db->rollBack();
echo json_encode(array("message" => "error"));
die();



?>
