#!/usr/bin/env php
<?php

declare(strict_types=1);

echo "=== PHP 8.5 Features Test ===\n\n";

// 1. Check PHP Version
echo "PHP Version: " . PHP_VERSION . "\n";
echo "Zend Engine: " . zend_version() . "\n\n";

// 2. Property Hooks Test (PHP 8.5 Feature)
echo "=== Property Hooks Test ===\n";

class UserProfile
{
    private string $rawName = '';

    public string $name {
        get => ucwords($this->rawName);
        set (string $value) {
            $this->rawName = strtolower(trim($value));
        }
    }

    public int $age {
        set (int $value) {
            if ($value < 0 || $value > 150) {
                throw new InvalidArgumentException('Invalid age');
            }
            $this->age = $value;
        }
    }
}

try {
    $user = new UserProfile();
    $user->name = '  john doe  ';
    echo "✅ Property hooks (set): " . $user->name . "\n";

    $user->age = 30;
    echo "✅ Property hooks (validated age): {$user->age}\n";
} catch (Throwable $e) {
    echo "❌ Property hooks failed: " . $e->getMessage() . "\n";
}

echo "\n";

// 3. New ext-uri extension
echo "=== URI Extension Test ===\n";
if (extension_loaded('uri')) {
    echo "✅ ext-uri is loaded\n";

    // Test URI parsing if available
    if (function_exists('uri_parse')) {
        $parsed = uri_parse('https://michaelschiemer.de/path?query=value#fragment');
        echo "✅ URI parsing works\n";
    }
} else {
    echo "⚠️  ext-uri not loaded\n";
}

echo "\n";

// 4. New ext-lexbor (HTML5 parser)
echo "=== Lexbor Extension Test ===\n";
if (extension_loaded('lexbor')) {
    echo "✅ ext-lexbor is loaded (High-performance HTML5 parser)\n";
} else {
    echo "⚠️  ext-lexbor not loaded\n";
}

echo "\n";

// 5. Zend OPcache (built-in)
echo "=== OPcache Test ===\n";
if (extension_loaded('Zend OPcache')) {
    echo "✅ Zend OPcache is loaded (built-in)\n";
    $status = opcache_get_status(false);
    if ($status) {
        echo "✅ OPcache enabled: " . ($status['opcache_enabled'] ? 'Yes' : 'No') . "\n";
        echo "✅ Cache hits: " . ($status['opcache_statistics']['hits'] ?? 0) . "\n";
    }
} else {
    echo "❌ OPcache not loaded\n";
}

echo "\n";

// 6. Sodium (built-in crypto)
echo "=== Sodium Cryptography Test ===\n";
if (extension_loaded('sodium')) {
    echo "✅ Sodium extension loaded\n";

    $key = sodium_crypto_secretbox_keygen();
    $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
    $message = "Test encryption with PHP 8.5";

    $encrypted = sodium_crypto_secretbox($message, $nonce, $key);
    $decrypted = sodium_crypto_secretbox_open($encrypted, $nonce, $key);

    if ($decrypted === $message) {
        echo "✅ Sodium encryption/decryption works\n";
    }
} else {
    echo "❌ Sodium not loaded\n";
}

echo "\n";

// 7. Random Extension (PHP 8.5)
echo "=== Random Extension Test ===\n";
if (extension_loaded('random')) {
    echo "✅ Random extension loaded\n";

    // Cryptographically secure random
    $randomBytes = random_bytes(16);
    $randomInt = random_int(1, 100);

    echo "✅ Random bytes generated: " . bin2hex($randomBytes) . "\n";
    echo "✅ Random int (1-100): {$randomInt}\n";
} else {
    echo "❌ Random extension not loaded\n";
}

echo "\n";

// 8. Performance Test
echo "=== Performance Test ===\n";
$start = microtime(true);
$iterations = 1000000;

for ($i = 0; $i < $iterations; $i++) {
    $x = $i * 2;
}

$duration = microtime(true) - $start;
$perSecond = $iterations / $duration;

echo sprintf("✅ Performance: %.2f M ops/sec\n", $perSecond / 1000000);

echo "\n=== Test Complete ===\n";
