<?php
include 'includes/auth.php';
include 'db.php';

// Check if stay_id is provided
$stay_id = $_GET['stay_id'] ?? null;
if (!$stay_id) {
    die("Invalid request: No stay selected.");
}

// Fetch stay, guest, and room details
$stay = $conn->query("SELECT s.*, g.name AS guest_name, r.room_number, r.room_type
                      FROM stays s
                      JOIN guests g ON s.guest_id = g.id
                      JOIN rooms r ON s.room_id = r.id
                      WHERE s.id = {$stay_id}")->fetch_assoc();

if (!$stay) {
    die("Stay not found.");
}

// ================= REVERSE CHECKOUT =================
// 1. Set checkout_date and days to NULL/0
$reverse = $conn->query("UPDATE stays SET checkout_date = NULL, days = 0 WHERE id = {$stay_id}");

// 2. Update room status to Available
$update_room = $conn->query("UPDATE rooms SET status='Available' WHERE id={$stay['room_id']}");

if ($reverse && $update_room) {

    // ================= SEND EMAIL =================
    $to = "giantplusmw@gmail.com";
    $subject = "Checkout Reversed Notification";
    
    $message = "
    <html>
    <head>
        <title>Checkout Reversed</title>
        <style>
            body { font-family: Arial, sans-serif; color: #333; }
            .container { padding: 20px; border: 1px solid #ddd; border-radius: 8px; }
            .header { background: #4CAF50; color: white; padding: 10px; text-align: center; border-radius: 5px 5px 0 0; }
            .details { margin-top: 20px; }
            .details table { width: 100%; border-collapse: collapse; }
            .details th, .details td { text-align: left; padding: 8px; border: 1px solid #ddd; }
            .footer { margin-top: 20px; font-size: 0.9em; color: #555; }
        </style>
    </head>
    <body>
        <div class='container'>
            <div class='header'><h2>Checkout Reversed</h2></div>
            <p>Hello,</p>
            <p>The following checkout has been reversed in the system:</p>
            <div class='details'>
                <table>
                    <tr><th>Guest Name</th><td>{$stay['guest_name']}</td></tr>
                    <tr><th>Room Number</th><td>{$stay['room_number']}</td></tr>
                    <tr><th>Room Type</th><td>{$stay['room_type']}</td></tr>
                    <tr><th>Original Check-In</th><td>{$stay['checkin_date']}</td></tr>
                    <tr><th>Original Check-Out</th><td>{$stay['checkout_date']}</td></tr>
                </table>
            </div>
            <div class='footer'>
                <p>This is an automated notification from the GiantPlus Hotel Management System.</p>
            </div>
        </div>
    </body>
    </html>
    ";

    // Set headers for HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= "From: cloud@gianterp.space" . "\r\n";

    // Send email
    mail($to, $subject, $message, $headers);

    // Redirect back with success message
    $_SESSION['success'] = "Checkout reversed successfully and notification sent!";
    header("Location:checkouts.php");
    exit;

} else {
    $_SESSION['error'] = "Failed to reverse checkout. Please try again.";
    header("Location: checkouts.php");
    exit;
}
?>