<?php
require_once __DIR__ . '/../app/helpers/functions.php';
require_once __DIR__ . '/../app/migrations.php';

if (!app_config_exists()) {
    fwrite(STDERR, "config/config.php is missing. Run public/install.php first.\n");
    exit(1);
}

try {
    require_once __DIR__ . '/../app/Core.php';
    $pdo = Database::connection();
    $ran = app_run_migrations($pdo, true);
    echo "Migrations completed. Statements applied: " . count($ran) . "\n";
    $missing = app_missing_tables($pdo);
    if ($missing) {
        echo "Still missing tables: " . implode(', ', $missing) . "\n";
        exit(2);
    }
    echo "Schema is complete.\n";
} catch (Throwable $e) {
    fwrite(STDERR, $e->getMessage() . "\n");
    exit(1);
}
