<?php
/**
 * OTA Auto-Sync
 * Run via cron: every 4 hours
 * Crontab: 0 */4 * * * php /path/to/hostiva/cron/sync_ota.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) {
    exit(1);
}

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

echo "[" . date('Y-m-d H:i:s') . "] Starting OTA sync...\n";

// Get all properties with active OTA connections
$stmt = $db->prepare("SELECT DISTINCT property_id FROM ota_connections WHERE active = 1");
$stmt->execute();
$properties = $stmt->fetchAll(PDO::FETCH_ASSOC);

$synced = 0;
foreach ($properties as $prop) {
    $ota_service->syncAirbnbBookings($prop['property_id']);
    $ota_service->syncBookingComBookings($prop['property_id']);
    $synced++;
}

echo "[" . date('Y-m-d H:i:s') . "] Synced $synced properties\n";
