<?php
declare(strict_types=1);

/**
 * CLI Entry Point — Migrations, Seeds, and System Commands.
 *
 * Usage:
 *   php cli.php migrate            Run all pending migrations
 *   php cli.php migrate:rollback   Rollback last batch
 *   php cli.php migrate:status     Show migration status
 *   php cli.php seed               Run all pending seeds
 *   php cli.php seed:run <Name>    Run a specific seed
 *   php cli.php cron               Run background jobs
 */

// Prevent web access
if (PHP_SAPI !== 'cli') {
    die('CLI only.');
}

require_once __DIR__ . '/vendor/autoload.php';

// Load .env
$envFile = __DIR__ . '/.env';
if (file_exists($envFile)) {
    $lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    foreach ($lines as $line) {
        $line = trim($line);
        if ($line === '' || $line[0] === '#') continue;
        if (str_contains($line, '=')) {
            [$key, $value] = explode('=', $line, 2);
            $key = trim($key);
            $value = trim($value);
            if (!isset($_ENV[$key])) {
                $_ENV[$key] = $value;
                putenv("{$key}={$value}");
            }
        }
    }
}

// Minimal bootstrap — just database, no session/routing
$config = [];
$configDir = __DIR__ . '/config';
if (is_dir($configDir)) {
    foreach (glob($configDir . '/*.php') as $configFile) {
        $key = basename($configFile, '.php');
        $config[$key] = require $configFile;
    }
}

// Build database config
$dbConfig = [
    'host'      => $_ENV['DB_HOST'] ?? $config['database']['host'] ?? '127.0.0.1',
    'port'      => $_ENV['DB_PORT'] ?? $config['database']['port'] ?? '3306',
    'name'      => $_ENV['DB_NAME'] ?? $config['database']['name'] ?? 'the_club_erp',
    'user'      => $_ENV['DB_USER'] ?? $config['database']['user'] ?? 'root',
    'pass'      => $_ENV['DB_PASS'] ?? $config['database']['pass'] ?? '',
    'charset'   => $config['database']['charset'] ?? 'utf8mb4',
    'collation' => $config['database']['collation'] ?? 'utf8mb4_unicode_ci',
];

$db = new \App\Core\Database(
    (string) $dbConfig['host'],
    (int) $dbConfig['port'],
    (string) $dbConfig['name'],
    (string) $dbConfig['user'],
    (string) $dbConfig['pass'],
    (string) $dbConfig['charset']
);

$command = $argv[1] ?? 'help';
$arg2 = $argv[2] ?? null;

echo "\n";

switch ($command) {
    case 'migrate':
        $runner = new \App\Core\Migration\MigrationRunner($db);
        $results = $runner->migrate();
        if (empty($results)) {
            echo "✅ Nothing to migrate. All migrations are up to date.\n";
        } else {
            echo "✅ Ran " . count($results) . " migration(s):\n";
            foreach ($results as $name) {
                echo "   ✔ {$name}\n";
            }
        }
        break;

    case 'migrate:rollback':
        $runner = new \App\Core\Migration\MigrationRunner($db);
        $results = $runner->rollback();
        if (empty($results)) {
            echo "⚠️  Nothing to rollback.\n";
        } else {
            echo "✅ Rolled back " . count($results) . " migration(s):\n";
            foreach ($results as $name) {
                echo "   ↩ {$name}\n";
            }
        }
        break;

    case 'migrate:status':
        $runner = new \App\Core\Migration\MigrationRunner($db);
        $status = $runner->status();
        echo str_pad('Migration', 60) . "Status\n";
        echo str_repeat('─', 70) . "\n";
        foreach ($status as $s) {
            $icon = $s['status'] === 'Ran' ? '✔' : '○';
            echo "{$icon} " . str_pad($s['migration'], 58) . $s['status'] . "\n";
        }
        break;

    case 'seed':
        echo "🌱 Running seeds...\n";
        $runner = new \App\Core\Seeder\SeederRunner($db);
        $results = $runner->run();
        if (empty($results)) {
            echo "✅ Nothing to seed. All seeds already executed.\n";
        } else {
            echo "✅ Ran " . count($results) . " seed(s):\n";
            foreach ($results as $name) {
                echo "   🌱 {$name}\n";
            }
        }
        break;

    case 'seed:run':
        if (!$arg2) {
            echo "❌ Please specify seed name: php cli.php seed:run SeedName\n";
            exit(1);
        }
        $runner = new \App\Core\Seeder\SeederRunner($db);
        $runner->runSingle($arg2);
        echo "✅ Seed executed: {$arg2}\n";
        break;

    case 'cron':
        echo "⏰ Running cron jobs...\n";
        $cronRunner = __DIR__ . '/cron/runner.php';
        if (file_exists($cronRunner)) {
            require $cronRunner;
        } else {
            echo "⚠️  No cron runner found.\n";
        }
        break;

    case 'routes':
        $app = \App\Core\App::getInstance();
        $app->boot();
        $routes = [];
        $modulesDir = __DIR__ . '/app/Modules';
        $routeFiles = glob($modulesDir . '/*/Routes.php');
        sort($routeFiles);
        foreach ($routeFiles as $file) {
            $moduleRoutes = require $file;
            if (is_array($moduleRoutes)) {
                foreach ($moduleRoutes as $r) {
                    if (is_array($r) && count($r) >= 3) {
                        $routes[] = $r;
                    }
                }
            }
        }
        echo str_pad('METHOD', 8) . str_pad('PATH', 50) . str_pad('HANDLER', 55) . str_pad('MIDDLEWARE', 20) . "PERMISSION\n";
        echo str_repeat('─', 145) . "\n";
        foreach ($routes as $r) {
            $mw = implode(',', $r[3] ?? []);
            echo str_pad($r[0], 8) . str_pad($r[1], 50) . str_pad($r[2], 55) . str_pad($mw, 20) . ($r[4] ?? '') . "\n";
        }
        echo "\nTotal: " . count($routes) . " routes\n";
        break;

    case 'permissions:orphans':
        require_once __DIR__ . '/app/Core/Registries/PermissionRegistry.php';
        $app = \App\Core\App::getInstance();
        $app->boot();
        $orphans = \App\Modules\AccessMatrix\Services\PermissionDiscoveryService::findOrphanPermissions();
        echo "ORPHAN PERMISSIONS (registered but not used in any route):\n";
        echo str_repeat('─', 60) . "\n";
        if (empty($orphans)) {
            echo "  ✅ No orphan permissions found.\n";
        } else {
            foreach ($orphans as $o) {
                echo "  ⚠️  {$o}\n";
            }
            echo "\nTotal: " . count($orphans) . " orphan permissions\n";
        }
        break;

    case 'permissions:unprotected':
        $app = \App\Core\App::getInstance();
        $app->boot();
        $unprotected = \App\Modules\AccessMatrix\Services\PermissionDiscoveryService::findUnprotectedRoutes();
        echo "UNPROTECTED ROUTES (no permission defined):\n";
        echo str_repeat('─', 80) . "\n";
        if (empty($unprotected)) {
            echo "  ✅ All routes have permissions.\n";
        } else {
            echo str_pad('MODULE', 20) . str_pad('METHOD', 8) . "PATH\n";
            echo str_repeat('─', 80) . "\n";
            foreach ($unprotected as $u) {
                echo str_pad($u['file'], 20) . str_pad($u['method'], 8) . $u['path'] . "\n";
            }
            echo "\nTotal: " . count($unprotected) . " unprotected routes\n";
        }
        break;

    case 'permissions:stats':
        $app = \App\Core\App::getInstance();
        $app->boot();
        $stats = \App\Modules\AccessMatrix\Services\PermissionDiscoveryService::getPermissionStats();
        echo "PERMISSION STATISTICS:\n";
        echo str_repeat('─', 50) . "\n";
        echo "  Total permissions: {$stats['total']}\n";
        echo "  Total groups: {$stats['groups']}\n";
        echo str_repeat('─', 50) . "\n";
        echo str_pad('GROUP', 30) . "COUNT\n";
        echo str_repeat('─', 50) . "\n";
        foreach ($stats['by_group'] as $group => $count) {
            echo str_pad($group, 30) . $count . "\n";
        }
        break;

    case 'help':
    default:
        echo "THE CLUB ERP — CLI Commands\n";
        echo "───────────────────────────────────\n";
        echo "  php cli.php migrate               Run pending migrations\n";
        echo "  php cli.php migrate:rollback      Rollback last batch\n";
        echo "  php cli.php migrate:status        Show migration status\n";
        echo "  php cli.php seed                  Run all pending seeds\n";
        echo "  php cli.php seed:run <Name>       Run specific seed\n";
        echo "  php cli.php cron                  Run background jobs\n";
        echo "  php cli.php routes                List all routes\n";
        echo "  php cli.php permissions:orphans   Registered but unused permissions\n";
        echo "  php cli.php permissions:unprotected  Routes without permissions\n";
        echo "  php cli.php permissions:stats     Permission count per module\n";
        echo "  php cli.php help                  Show this help\n";
        break;
}

echo "\n";