<?php

declare(strict_types=1);

echo "=== Simple Multi-Parameter Route Regex Test ===" . PHP_EOL . PHP_EOL;

// Test the regex pattern conversion from RouteCompiler
function convertPathToRegex(string $path, array &$paramNames): string
{
    $paramNames = [];
    $regex = preg_replace_callback('#\{(\w+)(\*)?}#', function ($matches) use (&$paramNames) {
        $paramNames[] = $matches[1];

        // If {id*} allow slashes, but make it non-greedy
        if (isset($matches[2]) && $matches[2] === '*') {
            return '(.+?)'; // Non-greedy: match as little as possible
        }

        return '([^/]+)'; // No slashes
    }, $path);

    return '~^' . $regex . '$~';
}

// Test 1: Single parameter
echo "Test 1: Single Parameter Route" . PHP_EOL;
$path1 = '/campaign/{slug}';
$params1 = [];
$regex1 = convertPathToRegex($path1, $params1);

echo "  Path: $path1" . PHP_EOL;
echo "  Regex: $regex1" . PHP_EOL;
echo "  Params: " . implode(', ', $params1) . PHP_EOL;

$testPath1 = '/campaign/test-campaign';
if (preg_match($regex1, $testPath1, $matches1)) {
    echo "  ✅ Matched: $testPath1" . PHP_EOL;
    echo "  Captured: " . ($matches1[1] ?? 'NONE') . PHP_EOL;
} else {
    echo "  ❌ NO MATCH for: $testPath1" . PHP_EOL;
}

echo PHP_EOL;

// Test 2: Two parameters (like Campaign PreSave route)
echo "Test 2: Two Parameters Route (Campaign PreSave)" . PHP_EOL;
$path2 = '/campaign/{slug}/presave/{platform}';
$params2 = [];
$regex2 = convertPathToRegex($path2, $params2);

echo "  Path: $path2" . PHP_EOL;
echo "  Regex: $regex2" . PHP_EOL;
echo "  Params: " . implode(', ', $params2) . PHP_EOL;

$testPath2 = '/campaign/test-campaign/presave/spotify';
if (preg_match($regex2, $testPath2, $matches2)) {
    echo "  ✅ Matched: $testPath2" . PHP_EOL;
    echo "  Captured slug: " . ($matches2[1] ?? 'NONE') . PHP_EOL;
    echo "  Captured platform: " . ($matches2[2] ?? 'NONE') . PHP_EOL;

    if ($matches2[1] === 'test-campaign' && $matches2[2] === 'spotify') {
        echo "  ✅ Parameter extraction korrekt!" . PHP_EOL;
    } else {
        echo "  ❌ Parameter extraction FALSCH!" . PHP_EOL;
    }
} else {
    echo "  ❌ NO MATCH for: $testPath2" . PHP_EOL;
}

echo PHP_EOL;

// Test 3: Three parameters
echo "Test 3: Three Parameters Route" . PHP_EOL;
$path3 = '/api/users/{userId}/posts/{postId}/comments/{commentId}';
$params3 = [];
$regex3 = convertPathToRegex($path3, $params3);

echo "  Path: $path3" . PHP_EOL;
echo "  Regex: $regex3" . PHP_EOL;
echo "  Params: " . implode(', ', $params3) . PHP_EOL;

$testPath3 = '/api/users/123/posts/456/comments/789';
if (preg_match($regex3, $testPath3, $matches3)) {
    echo "  ✅ Matched: $testPath3" . PHP_EOL;
    echo "  Captured userId: " . ($matches3[1] ?? 'NONE') . PHP_EOL;
    echo "  Captured postId: " . ($matches3[2] ?? 'NONE') . PHP_EOL;
    echo "  Captured commentId: " . ($matches3[3] ?? 'NONE') . PHP_EOL;
} else {
    echo "  ❌ NO MATCH for: $testPath3" . PHP_EOL;
}

echo PHP_EOL;
echo "=== Summary ===" . PHP_EOL;
echo "Multi-Parameter Route Regex funktioniert grundsätzlich: ✅ JA" . PHP_EOL;
