<?php
require __DIR__ . '/../includes/db.php';
$rows = $pdo->query('SELECT id FROM residents')->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $r) {
    $st = $pdo->prepare('SELECT waste_segregation, door_to_door FROM trainings WHERE resident_id=? ORDER BY training_number DESC LIMIT 1');
    $st->execute([$r['id']]);
    $latest = $st->fetch();
    if($latest) {
        $upd = $pdo->prepare('UPDATE residents SET waste_segregation=?, door_to_door=? WHERE id=?');
        $upd->execute([$latest['waste_segregation'], $latest['door_to_door'], $r['id']]);
    }
}
echo "Database synced successfully.";
