<?php
require "../includes/header.php";
require "../config/config.php";

if(!isset($_SESSION['username'])){
    header("location: " . APPURL . "/auth/login.php");
    exit;
}

if(!isset($_GET['reference']) || !isset($_GET['booking_id'])){
    header("location: " . APPURL);
    exit;
}

$reference  = trim($_GET['reference']);
$booking_id = (int)$_GET['booking_id'];

// Verify payment via Paystack API
$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_URL            => "https://api.paystack.co/transaction/verify/" . rawurlencode($reference),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER     => [
        "Authorization: Bearer " . PAYSTACK_SECRET_KEY,
        "Cache-Control: no-cache",
    ],
]);
$response = curl_exec($curl);
$err      = curl_error($curl);
curl_close($curl);

$verified = false;
$booking  = null;

if(!$err){
    $data = json_decode($response);
    if($data && $data->status === true && $data->data->status === 'success'){
        // Update the booking in DB
        $update = $conn->prepare("UPDATE bookings SET payment_status = 'paid', transaction_ref = :ref WHERE id = :id AND user_id = :uid");
        $update->execute([
            ':ref' => $reference,
            ':id'  => $booking_id,
            ':uid' => $_SESSION['id'],
        ]);
        $verified = true;

        // Fetch booking details
        $stmt = $conn->prepare("SELECT * FROM bookings WHERE id = :id");
        $stmt->execute([':id' => $booking_id]);
        $booking = $stmt->fetch(PDO::FETCH_OBJ);
    }
}
?>

    <div class="hero-wrap js-fullheight" style="background-image: url('<?php echo APPURL; ?>/images/hotel-bg.jpg');" data-stellar-background-ratio="0.5">
      <div class="overlay"></div>
      <div class="container">
        <div class="row no-gutters slider-text js-fullheight align-items-center justify-content-start" data-scrollax-parent="true">
          <div class="col-md-7 ftco-animate">
            <h2 class="subheading">Payment <?php echo $verified ? 'Successful' : 'Failed'; ?></h2>
            <h1 class="mb-4"><?php echo $verified ? 'Booking Confirmed!' : 'Verification Error'; ?></h1>
          </div>
        </div>
      </div>
    </div>

    <section class="ftco-section">
      <div class="container">
        <div class="row justify-content-center">
          <div class="col-lg-7 text-center">

            <?php if($verified && $booking): ?>
              <div class="mb-4" style="font-size:80px; color:#28a745;">
                <i class="fa fa-check-circle"></i>
              </div>
              <h2 class="mb-3">Payment Verified!</h2>
              <p class="lead mb-4">Your booking at <strong><?php echo htmlspecialchars($booking->hotel_name); ?></strong> has been confirmed.</p>

              <div class="appointment-form p-4 mb-4" style="text-align:left;">
                <table class="table table-bordered">
                  <tr><th>Booking ID</th><td>#<?php echo $booking->id; ?></td></tr>
                  <tr><th>Hotel</th><td><?php echo htmlspecialchars($booking->hotel_name); ?></td></tr>
                  <tr><th>Room</th><td><?php echo htmlspecialchars($booking->room_name); ?></td></tr>
                  <tr><th>Check-In</th><td><?php echo htmlspecialchars($booking->check_in); ?></td></tr>
                  <tr><th>Check-Out</th><td><?php echo htmlspecialchars($booking->check_out); ?></td></tr>
                  <tr><th>Amount Paid</th><td>GH&#8373;<?php echo number_format($booking->price, 2); ?></td></tr>
                  <tr><th>Transaction Ref</th><td><?php echo htmlspecialchars($reference); ?></td></tr>
                  <tr><th>Status</th><td><span class="badge" style="background:#28a745;color:#fff;padding:5px 10px;">PAID</span></td></tr>
                </table>
              </div>

              <a href="<?php echo APPURL; ?>/bookings/my-bookings.php" class="btn btn-primary px-4 py-3 mr-2">View My Bookings</a>
              <a href="<?php echo APPURL; ?>" class="btn btn-outline-secondary px-4 py-3">Back to Home</a>

            <?php else: ?>
              <div class="mb-4" style="font-size:80px; color:#dc3545;">
                <i class="fa fa-times-circle"></i>
              </div>
              <h2 class="mb-3">Payment Verification Failed</h2>
              <p class="lead mb-4">We could not verify your payment. If money was deducted, please contact support with reference: <strong><?php echo htmlspecialchars($reference); ?></strong></p>
              <a href="<?php echo APPURL; ?>" class="btn btn-primary px-4 py-3">Back to Home</a>
            <?php endif; ?>

          </div>
        </div>
      </div>
    </section>

<?php require "../includes/footer.php"; ?>

