<?php
/**
 * Check-In Reminder Sender
 * Run via cron: daily at 9 AM
 * Crontab: 0 9 * * * php /path/to/hostiva/cron/send_checkin_reminders.php
 */

$config = require __DIR__ . '/../config/config.php';

try {
    $db = new PDO(
        'mysql:host=' . $config['database']['host'] . ';dbname=' . $config['database']['name'],
        $config['database']['user'],
        $config['database']['password'],
        [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
    );
} catch (PDOException $e) {
    error_log('Reminders: DB connection failed');
    exit(1);
}

require __DIR__ . '/../api/services/EmailService.php';
$email_service = new EmailService($db, $config);

echo "[" . date('Y-m-d H:i:s') . "] Sending check-in reminders...\n";

// Find bookings with check-in tomorrow
$tomorrow = date('Y-m-d', strtotime('+1 day'));
$stmt = $db->prepare("
    SELECT id FROM bookings 
    WHERE DATE(check_in) = ? AND status = 'confirmed'
");
$stmt->execute([$tomorrow]);
$bookings = $stmt->fetchAll(PDO::FETCH_ASSOC);

$sent = 0;
foreach ($bookings as $booking) {
    if ($email_service->sendCheckInReminder($booking['id'])) {
        $sent++;
    }
}

echo "[" . date('Y-m-d H:i:s') . "] Sent $sent check-in reminders\n";
