<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once "config/config.php";
require_once "Data.class.php";

$Data = new Data($pdo);

// Alle Termine holen (nur die mit bestehender CalDAV-UID!)
$sql = $pdo->query("
    SELECT *
    FROM termine
    ORDER BY id ASC
");

$termine = $sql->fetchAll(PDO::FETCH_ASSOC);

echo "<h2>Starte Update-Sync aller Termine zu iCloud…</h2>";
echo "<pre>";

$counter = 0;

foreach ($termine as $t) {

    $counter++;

    $id     = (int)$t['id'];

    echo "→ Update Termin #$id ... ";

    $ok = $Data->updateCalendarEvent($id);

    if ($ok) {
        echo "OK\n";
    } else {
        echo "FEHLER!\n";
    }

    // 🔒 iCloud Rate-Limit Schutz
    usleep(800000); // 0.8 Sekunden
}

echo "\n---\n";
echo "Geupdatete Termine: $counter\n";
echo "</pre>";

echo "<h3>Update-Sync abgeschlossen!</h3>";
?>
