<?php

declare(strict_types=1);

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

use App\Framework\Process\Services\TcpPortCheckService;

echo "=== Vite Integration Test ===\n\n";

// Test 1: TcpPortCheckService
echo "1. Testing TcpPortCheckService\n";
echo "   - Testing localhost:3000 (HTTP)...\n";

$portCheck = new TcpPortCheckService();

// Test HTTP localhost:3000
$isOpen = $portCheck->isPortOpen('localhost', 3000, 2);
echo "   Result: " . ($isOpen ? "✅ OPEN" : "❌ CLOSED") . "\n\n";

// Test HTTPS localhost:3000
echo "   - Testing localhost:3000 (HTTPS/SSL)...\n";
$isSslOpen = $portCheck->isSslPortOpen('localhost', 3000, 2);
echo "   Result: " . ($isSslOpen ? "✅ OPEN" : "❌ CLOSED") . "\n\n";

// Test 2: Different hosts
echo "2. Testing different host configurations\n";

$hosts = [
    'localhost' => 3000,
    '127.0.0.1' => 3000,
    'host.docker.internal' => 3000,
];

foreach ($hosts as $host => $port) {
    echo "   - Testing {$host}:{$port}...\n";
    $result = $portCheck->isPortOpen($host, $port, 2);
    echo "     Result: " . ($result ? "✅ OPEN" : "❌ CLOSED") . "\n";
}

echo "\n";

// Test 3: Test some known open ports
echo "3. Testing known open ports (sanity check)\n";
echo "   - Testing localhost:80 (Nginx)...\n";
$nginx = $portCheck->isPortOpen('localhost', 80, 2);
echo "   Result: " . ($nginx ? "✅ OPEN" : "❌ CLOSED") . "\n";

echo "   - Testing localhost:443 (Nginx HTTPS)...\n";
$nginxSsl = $portCheck->isSslPortOpen('localhost', 443, 2);
echo "   Result: " . ($nginxSsl ? "✅ OPEN" : "❌ CLOSED") . "\n\n";

// Test 4: Parse URL test
echo "4. Testing URL parsing (Vite config)\n";
$testUrls = [
    'https://localhost:3000',
    'https://host.docker.internal:3000',
    'http://127.0.0.1:3000',
];

foreach ($testUrls as $url) {
    echo "   - Parsing: {$url}\n";
    $parsed = parse_url($url);
    if ($parsed !== false) {
        $host = $parsed['host'] ?? 'N/A';
        $port = $parsed['port'] ?? ($parsed['scheme'] === 'https' ? 443 : 80);
        $scheme = $parsed['scheme'] ?? 'http';
        echo "     Host: {$host}, Port: {$port}, Scheme: {$scheme}\n";
    } else {
        echo "     ❌ Failed to parse\n";
    }
}

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