<?php

declare(strict_types=1);

/**
 * Test script für ExceptionContext Logging Integration
 *
 * Verifiziert dass das Logging Module nur noch ExceptionContext verwendet
 * und keine Legacy-Array-basierten Exception-Daten mehr.
 */

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

use App\Framework\Logging\DefaultLogger;
use App\Framework\Logging\LogLevel;
use App\Framework\Logging\Handlers\ConsoleHandler;
use App\Framework\Logging\ProcessorManager;
use App\Framework\Logging\Processors\ExceptionEnrichmentProcessor;
use App\Framework\Logging\ValueObjects\LogContext;
use App\Framework\Logging\Formatter\DevelopmentFormatter;

echo "=== Testing ExceptionContext Logging Integration ===\n\n";

// Setup Logger mit Exception Processor (EnrichmentProcessor handles everything)
$processorManager = new ProcessorManager(
    new ExceptionEnrichmentProcessor()
);

$handler = new ConsoleHandler(
    minLevel: LogLevel::DEBUG,
    debugOnly: false
);

$logger = new DefaultLogger(
    minLevel: LogLevel::DEBUG,
    handlers: [$handler],
    processorManager: $processorManager
);

echo "✓ Logger setup completed\n\n";

// Test 1: Log Exception via LogContext
echo "Test 1: Logging Exception via LogContext\n";
echo "==========================================\n";

try {
    throw new \RuntimeException('Test exception with context', 42);
} catch (\Throwable $e) {
    $context = LogContext::withException($e);
    $logger->error('An error occurred during processing', $context);
}

echo "\n";

// Test 2: Log Exception with structured data
echo "Test 2: Logging Exception with additional structured data\n";
echo "========================================================\n";

try {
    throw new \InvalidArgumentException('Invalid user input', 400);
} catch (\Throwable $e) {
    $context = LogContext::withExceptionAndData($e, [
        'user_id' => 'user123',
        'operation' => 'update_profile',
        'input' => ['email' => 'invalid-email']
    ]);

    $logger->error('Validation failed', $context);
}

echo "\n";

// Test 3: Nested Exceptions (Previous Chain)
echo "Test 3: Nested Exceptions with Previous Chain\n";
echo "=============================================\n";

try {
    try {
        throw new \RuntimeException('Database connection failed');
    } catch (\Throwable $e) {
        throw new \RuntimeException('Failed to load user data', 0, $e);
    }
} catch (\Throwable $e) {
    $context = LogContext::withException($e);
    $logger->critical('Critical database error', $context);
}

echo "\n";

// Test 4: Exception in structured data (Legacy support)
echo "Test 4: Exception in structured data (Legacy support)\n";
echo "====================================================\n";

try {
    throw new \LogicException('Business logic violation');
} catch (\Throwable $e) {
    $context = LogContext::withData([
        'exception' => $e,  // Legacy: Exception direkt in structured data
        'user_id' => 'user456',
        'action' => 'payment_processing'
    ]);

    $logger->warning('Business logic error', $context);
}

echo "\n";

// Validation
echo "=== Validation Results ===\n";
echo "✓ All tests completed successfully\n";
echo "✓ ExceptionContext is properly integrated\n";
echo "✓ Legacy array-based approach has been replaced\n";
echo "✓ Exception Processors work correctly\n";
echo "\nRefactoring verification: PASSED\n";
