<?php

define('ROOT_PATH', dirname(__DIR__));

function run_test(string $name, callable $test) {
    try {
        $test();
        echo "✅ PASS: $name\n";
    } catch (\Throwable $e) {
        echo "❌ FAIL: $name\n";
        echo "   Error: " . $e->getMessage() . " at " . $e->getFile() . ":" . $e->getLine() . "\n";
    }
}

function simulate_cgi_request(string $method, string $uri, string $scriptName) {
    $scriptFilename = ROOT_PATH . '/public/index.php';
    
    // Pass environment variables using putenv
    putenv("REQUEST_METHOD=$method");
    putenv("REQUEST_URI=$uri");
    putenv("SCRIPT_NAME=$scriptName");
    putenv("SCRIPT_FILENAME=$scriptFilename");
    putenv("HTTP_HOST=localhost");
    putenv("HTTPS=off");
    putenv("REDIRECT_STATUS=200");
    
    $cmd = 'C:/xampp/php/php-cgi.exe ' . escapeshellarg($scriptFilename);
    $output = (string) shell_exec($cmd);
    
    // Clear environment variables
    putenv("REQUEST_METHOD");
    putenv("REQUEST_URI");
    putenv("SCRIPT_NAME");
    putenv("SCRIPT_FILENAME");
    putenv("HTTP_HOST");
    putenv("HTTPS");
    putenv("REDIRECT_STATUS");
    
    return $output;
}

run_test('GET / in subfolder returns Landing Page', function () {
    $res = simulate_cgi_request('GET', '/inveetaire/', '/inveetaire/public/index.php');
    if (strpos($res, 'INVEETAIRE') === false) {
        throw new Exception("Expected landing page content, got output:\n$res");
    }
    if (strpos($res, 'Status: 302') !== false) {
        throw new Exception("Expected 200 OK, got redirect status:\n$res");
    }
});

run_test('GET / in production root returns Landing Page', function () {
    $res = simulate_cgi_request('GET', '/', '/index.php');
    if (strpos($res, 'INVEETAIRE') === false) {
        throw new Exception("Expected landing page content, got output:\n$res");
    }
    if (strpos($res, 'Status: 302') !== false) {
        throw new Exception("Expected 200 OK, got redirect status:\n$res");
    }
});

run_test('GET /login redirects to /couple', function () {
    $res = simulate_cgi_request('GET', '/inveetaire/login', '/inveetaire/public/index.php');
    if (strpos($res, 'Location: http://localhost/inveetaire/couple') === false) {
        throw new Exception("Expected redirect to '/couple', got output:\n$res");
    }
    if (strpos($res, 'Status: 302') === false) {
        throw new Exception("Expected 302 redirect status, got output:\n$res");
    }
});

run_test('GET /couple loads login page successfully', function () {
    $res = simulate_cgi_request('GET', '/inveetaire/couple', '/inveetaire/public/index.php');
    if (strpos($res, 'Location:') !== false) {
        throw new Exception("Expected no redirect for /couple, got output:\n$res");
    }
    if (strpos($res, 'Welcome back') === false) {
        throw new Exception("Expected couple login page content, got output:\n$res");
    }
});

run_test('GET /unknown-path still returns 404', function () {
    $res = simulate_cgi_request('GET', '/inveetaire/unknown-path', '/inveetaire/public/index.php');
    if (strpos($res, 'Page Not Found') === false) {
        throw new Exception("Expected 404 page content, got output:\n$res");
    }
    if (strpos($res, 'Status: 404') === false) {
        throw new Exception("Expected 404 status, got output:\n$res");
    }
});
