<?php

declare(strict_types=1);

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

use App\Framework\Core\AppBootstrapper;
use App\Framework\Database\ConnectionInterface;
use App\Framework\Database\ValueObjects\SqlQuery;
use App\Framework\DateTime\SystemClock;
use App\Framework\DateTime\SystemHighResolutionClock;
use App\Framework\Performance\EnhancedPerformanceCollector;
use App\Framework\Performance\MemoryMonitor;

echo "🔍 Checking Database Schema\n";
echo "==========================\n\n";

try {
    // Bootstrap the application
    $clock = new SystemClock();
    $highResClock = new SystemHighResolutionClock();
    $memoryMonitor = new MemoryMonitor();
    $collector = new EnhancedPerformanceCollector($clock, $highResClock, $memoryMonitor, enabled: false);

    $bootstrapper = new AppBootstrapper(__DIR__ . '/../..', $collector, $memoryMonitor);
    $container = $bootstrapper->bootstrapWorker();

    $connection = $container->get(ConnectionInterface::class);

    // Check job_history table structure
    echo "📊 job_history table structure:\n";
    echo "-------------------------------\n";

    $result = $connection->query(SqlQuery::create("DESCRIBE job_history"));
    $columns = $result->fetchAll();

    foreach ($columns as $column) {
        echo sprintf(
            "%-20s %-15s %-10s %-10s %-10s\n",
            $column['Field'],
            $column['Type'],
            $column['Null'],
            $column['Key'],
            $column['Default'] ?? 'NULL'
        );
    }

    echo "\n📊 dead_letter_jobs table structure:\n";
    echo "------------------------------------\n";

    try {
        $result = $connection->query(SqlQuery::create("DESCRIBE dead_letter_jobs"));
        $columns = $result->fetchAll();

        foreach ($columns as $column) {
            echo sprintf(
                "%-20s %-15s %-10s %-10s %-10s\n",
                $column['Field'],
                $column['Type'],
                $column['Null'],
                $column['Key'],
                $column['Default'] ?? 'NULL'
            );
        }
    } catch (\Exception $e) {
        echo "Table dead_letter_jobs: " . $e->getMessage() . "\n";
    }

    echo "\n📊 job_metrics table structure:\n";
    echo "-------------------------------\n";

    try {
        $result = $connection->query(SqlQuery::create("DESCRIBE job_metrics"));
        $columns = $result->fetchAll();

        foreach ($columns as $column) {
            echo sprintf(
                "%-20s %-15s %-10s %-10s %-10s\n",
                $column['Field'],
                $column['Type'],
                $column['Null'],
                $column['Key'],
                $column['Default'] ?? 'NULL'
            );
        }
    } catch (\Exception $e) {
        echo "Table job_metrics: " . $e->getMessage() . "\n";
    }

    echo "\n📊 Available tables:\n";
    echo "-------------------\n";

    $result = $connection->query(SqlQuery::create("SHOW TABLES"));
    $tables = $result->fetchAll();

    foreach ($tables as $table) {
        echo "• " . array_values($table)[0] . "\n";
    }

} catch (\Exception $e) {
    echo "❌ Error checking schema: {$e->getMessage()}\n";
    echo "Stack trace:\n{$e->getTraceAsString()}\n";
    exit(1);
}

echo "\n✅ Schema check completed!\n";
