<?php
// check_invoice_status_passenger.php
header('Content-Type: application/json; charset=utf-8');

// تضمين الاتصال بقاعدة البيانات ($con)
include "../jwtconnect.php";

// 1) استقبال رقم الفاتورة
$invoice_number = filterRequest("invoice_number");

// التحقق من المدخلات
if (empty($invoice_number)) {
    printFailure("invoice_number is required.");
    exit();
}

try {
    // 2) البحث عن حالة الفاتورة في جدول الركّاب
    $sql = "SELECT status FROM `invoices_sms_passenger` WHERE invoice_number = :invoice_number LIMIT 1";
    $stmt = $con->prepare($sql);
    $stmt->execute([':invoice_number' => $invoice_number]);

    $invoice = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($invoice) {
        echo json_encode([
            "status"         => "success",
            "invoice_status" => $invoice['status'] // "pending" أو "completed" أو "failed"
        ]);
    } else {
        printFailure("Invoice not found.");
    }

} catch (PDOException $e) {
    printFailure("Database error: " . $e->getMessage());
}