<?php
/** Auto-cancel pending bookings after 48h */
require_once dirname(__DIR__) . '/config.php';
try {
    $st = db()->prepare("SELECT id, user_id FROM bookings WHERE status='pending' AND created_at < NOW() - INTERVAL 48 HOUR");
    $st->execute();
    $expired = $st->fetchAll();
    foreach ($expired as $bk) {
        db()->prepare("UPDATE bookings SET status='cancelled', cancelled_by='system', cancelled_at=NOW() WHERE id=?")->execute([$bk['id']]);
        $token = get_user_token($bk['user_id']);
        if ($token) send_push($token, '⏰ انتهت مهلة الحجز', 'جرّب مزوداً آخر!', ['tag'=>'booking']);
    }
    echo date('Y-m-d H:i:s') . " Cancelled " . count($expired) . " expired bookings\n";
} catch (Throwable $e) { echo "ERROR: ".$e->getMessage()."\n"; }
