<?php

declare(strict_types=1);

/**
 * Test script for ErrorReportingConfig integration
 *
 * Verifies that ErrorReportingConfig correctly loads from environment
 * and applies environment-specific defaults.
 */

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

use App\Framework\Config\Environment;
use App\Framework\Config\EnvironmentType;
use App\Framework\ErrorReporting\ErrorReportingConfig;

echo "=== Testing ErrorReportingConfig Integration ===\n\n";

// Test 1: Development Environment
echo "Test 1: Development Environment Configuration\n";
echo "=============================================\n";

$devEnv = new Environment(['APP_ENV' => 'development']);
$devConfig = ErrorReportingConfig::fromEnvironment($devEnv);

echo "✓ Config loaded from environment\n";
echo "  - enabled: " . ($devConfig->enabled ? 'true' : 'false') . "\n";
echo "  - asyncProcessing: " . ($devConfig->asyncProcessing ? 'true' : 'false') . " (expected: false for dev)\n";
echo "  - filterLevels: " . (empty($devConfig->filterLevels) ? 'ALL' : implode(', ', $devConfig->filterLevels)) . "\n";
echo "  - maxStackTraceDepth: {$devConfig->maxStackTraceDepth} (expected: 30)\n";
echo "  - sanitizeSensitiveData: " . ($devConfig->sanitizeSensitiveData ? 'true' : 'false') . " (expected: false)\n";
echo "  - maxReportsPerMinute: {$devConfig->maxReportsPerMinute} (expected: 1000)\n\n";

// Test 2: Production Environment
echo "Test 2: Production Environment Configuration\n";
echo "===========================================\n";

$prodEnv = new Environment(['APP_ENV' => 'production']);
$prodConfig = ErrorReportingConfig::fromEnvironment($prodEnv);

echo "✓ Config loaded from environment\n";
echo "  - enabled: " . ($prodConfig->enabled ? 'true' : 'false') . "\n";
echo "  - asyncProcessing: " . ($prodConfig->asyncProcessing ? 'true' : 'false') . " (expected: true)\n";
echo "  - filterLevels: " . (empty($prodConfig->filterLevels) ? 'ALL' : implode(', ', $prodConfig->filterLevels)) . " (expected: error, critical, alert, emergency)\n";
echo "  - maxStackTraceDepth: {$prodConfig->maxStackTraceDepth} (expected: 15)\n";
echo "  - sanitizeSensitiveData: " . ($prodConfig->sanitizeSensitiveData ? 'true' : 'false') . " (expected: true)\n";
echo "  - maxReportsPerMinute: {$prodConfig->maxReportsPerMinute} (expected: 30)\n\n";

// Test 3: Staging Environment
echo "Test 3: Staging Environment Configuration\n";
echo "========================================\n";

$stagingEnv = new Environment(['APP_ENV' => 'staging']);
$stagingConfig = ErrorReportingConfig::fromEnvironment($stagingEnv);

echo "✓ Config loaded from environment\n";
echo "  - enabled: " . ($stagingConfig->enabled ? 'true' : 'false') . "\n";
echo "  - asyncProcessing: " . ($stagingConfig->asyncProcessing ? 'true' : 'false') . " (expected: true)\n";
echo "  - filterLevels: " . (empty($stagingConfig->filterLevels) ? 'ALL' : implode(', ', $stagingConfig->filterLevels)) . " (expected: warning and above)\n";
echo "  - maxStackTraceDepth: {$stagingConfig->maxStackTraceDepth} (expected: 20)\n";
echo "  - analyticsRetentionDays: {$stagingConfig->analyticsRetentionDays} (expected: 14)\n\n";

// Test 4: Environment Variable Overrides
echo "Test 4: Environment Variable Overrides\n";
echo "=====================================\n";

$overrideEnv = new Environment([
    'APP_ENV' => 'production',
    'ERROR_REPORTING_ENABLED' => 'false',
    'ERROR_REPORTING_ASYNC' => 'false',
    'ERROR_REPORTING_FILTER_LEVELS' => 'critical,emergency',
    'ERROR_REPORTING_MAX_STACK_DEPTH' => '5',
    'ERROR_REPORTING_SAMPLING_RATE' => '50'
]);

$overrideConfig = ErrorReportingConfig::fromEnvironment($overrideEnv);

echo "✓ Config with environment overrides\n";
echo "  - enabled: " . ($overrideConfig->enabled ? 'true' : 'false') . " (override: false)\n";
echo "  - asyncProcessing: " . ($overrideConfig->asyncProcessing ? 'true' : 'false') . " (override: false)\n";
echo "  - filterLevels: " . implode(', ', $overrideConfig->filterLevels) . " (override: critical, emergency)\n";
echo "  - maxStackTraceDepth: {$overrideConfig->maxStackTraceDepth} (override: 5)\n";
echo "  - samplingRate: {$overrideConfig->samplingRate} (override: 50)\n\n";

// Test 5: Helper Methods
echo "Test 5: Helper Methods\n";
echo "====================\n";

$testConfig = ErrorReportingConfig::fromEnvironment($prodEnv);

// shouldReportLevel
$shouldReportError = $testConfig->shouldReportLevel('error');
$shouldReportDebug = $testConfig->shouldReportLevel('debug');

echo "✓ shouldReportLevel()\n";
echo "  - 'error' level: " . ($shouldReportError ? 'REPORT' : 'SKIP') . " (expected: REPORT)\n";
echo "  - 'debug' level: " . ($shouldReportDebug ? 'REPORT' : 'SKIP') . " (expected: SKIP in production)\n\n";

// shouldReportException
$normalException = new \RuntimeException('Test error');
$shouldReport = $testConfig->shouldReportException($normalException);

echo "✓ shouldReportException()\n";
echo "  - RuntimeException: " . ($shouldReport ? 'REPORT' : 'SKIP') . " (expected: REPORT)\n\n";

// shouldSample
$samples = 0;
for ($i = 0; $i < 100; $i++) {
    if ($testConfig->shouldSample()) {
        $samples++;
    }
}

echo "✓ shouldSample()\n";
echo "  - Sampling rate: {$testConfig->samplingRate}%\n";
echo "  - Samples in 100 attempts: {$samples} (expected: ~{$testConfig->samplingRate})\n\n";

// Test 6: Direct Environment Type
echo "Test 6: Direct Environment Type Configuration\n";
echo "============================================\n";

$directConfig = ErrorReportingConfig::forEnvironment(EnvironmentType::DEV, $devEnv);

echo "✓ Config created directly with EnvironmentType::DEV\n";
echo "  - asyncProcessing: " . ($directConfig->asyncProcessing ? 'true' : 'false') . " (expected: false)\n";
echo "  - maxReportsPerMinute: {$directConfig->maxReportsPerMinute} (expected: 1000)\n\n";

// Validation
echo "=== Validation Results ===\n";
echo "✓ All environment configurations loaded successfully\n";
echo "✓ Environment-specific defaults applied correctly\n";
echo "✓ Environment variable overrides work as expected\n";
echo "✓ Helper methods function correctly\n";
echo "✓ ErrorReportingConfig integration: PASSED\n";
