<?php

declare(strict_types=1);

require_once __DIR__ . '/support.php';

$phpVersion = PHP_VERSION;
$composerExists = scripts_check_command('composer');
$gitExists = scripts_check_command('git');
$env = scripts_read_env_file(scripts_env_path());
$dbConnection = scripts_normalize_db_connection($env['DB_CONNECTION'] ?? 'mysql');
$pdoMysqlLoaded = extension_loaded('pdo_mysql');
$pdoSqlsrvLoaded = extension_loaded('pdo_sqlsrv');
$requiredExtension = scripts_required_pdo_extension($dbConnection);

scripts_stdout('Pruefe lokale Voraussetzungen fuer Kaffeeliste SaaS...');
scripts_stdout('Projektwurzel: ' . scripts_project_root());
scripts_stdout('');

$checks = [
    ['PHP', true, $phpVersion],
    ['Composer', $composerExists, $composerExists ? 'verfuegbar' : 'nicht gefunden'],
    ['Git', $gitExists, $gitExists ? 'verfuegbar' : 'nicht gefunden'],
    ['DB_CONNECTION', true, $dbConnection],
    ['pdo_mysql', $pdoMysqlLoaded, $pdoMysqlLoaded ? 'geladen' : 'nicht geladen'],
    ['pdo_sqlsrv', $pdoSqlsrvLoaded, $pdoSqlsrvLoaded ? 'geladen' : 'nicht geladen'],
    ['Erforderliche PDO-Erweiterung', extension_loaded($requiredExtension), $requiredExtension],
    ['SaaS-App', is_dir(scripts_saas_app_path()), scripts_saas_app_path()],
    ['.env.example', is_file(scripts_env_example_path()), scripts_env_example_path()],
    ['.env', is_file(scripts_env_path()), is_file(scripts_env_path()) ? 'vorhanden' : 'noch nicht angelegt'],
];

foreach ($checks as [$label, $passed, $detail]) {
    $prefix = $passed ? '[OK]' : '[FEHLT]';
    scripts_stdout(sprintf('%s %s - %s', $prefix, $label, $detail));
}

scripts_stdout('');
scripts_stdout('Naechste Schritte:');
scripts_stdout('1. Falls Composer fehlt, zuerst Composer installieren.');
scripts_stdout('2. Mit `php scripts/prepare-saas-env.php` eine lokale .env anlegen.');
scripts_stdout('3. DB_CONNECTION in `saas-app/.env` auf mysql, mariadb oder sqlsrv pruefen.');
scripts_stdout('4. Mit `php scripts/install-saas.php` das SQL-Bundle fuer den gewaehlten Treiber erzeugen.');
scripts_stdout('5. Optional `php scripts/run-sql-migrations.php --connection=' . $dbConnection . ' --server=... --database=...` fuer die direkte SQL-Ausfuehrung verwenden.');
