<?php
/**
 * Path Diagnostic Tool
 * Upload this to /booking/ folder to see what paths are being used
 * Access: https://projek.apasaja.my/booking/diagnostic.php
 */

echo "<h1>Path Diagnostic</h1>";
echo "<style>body{font-family:monospace;padding:20px;} table{border-collapse:collapse;margin:20px 0;} td,th{border:1px solid #ddd;padding:8px;text-align:left;} th{background:#f0f0f0;}</style>";

echo "<h2>Current Script Location</h2>";
echo "<table>";
echo "<tr><th>Variable</th><th>Value</th></tr>";
echo "<tr><td>__FILE__</td><td>" . __FILE__ . "</td></tr>";
echo "<tr><td>__DIR__</td><td>" . __DIR__ . "</td></tr>";
echo "<tr><td>\$_SERVER['PHP_SELF']</td><td>" . $_SERVER['PHP_SELF'] . "</td></tr>";
echo "<tr><td>\$_SERVER['SCRIPT_FILENAME']</td><td>" . $_SERVER['SCRIPT_FILENAME'] . "</td></tr>";
echo "<tr><td>\$_SERVER['DOCUMENT_ROOT']</td><td>" . $_SERVER['DOCUMENT_ROOT'] . "</td></tr>";
echo "<tr><td>basename(\$_SERVER['PHP_SELF'])</td><td>" . basename($_SERVER['PHP_SELF']) . "</td></tr>";
echo "</table>";

echo "<h2>Check if files exist</h2>";
echo "<table>";
echo "<tr><th>File Path</th><th>Exists?</th></tr>";

$checkFiles = [
    '../config.php',
    '../sas_integration.php',
    '../activate_license.php',
    '../includes/trial_expired.php',
    '../includes/license_expired.php',
    'config.php',
    'sas_integration.php',
    'activate_license.php',
];

foreach ($checkFiles as $file) {
    $exists = file_exists($file) ? '✅ YES' : '❌ NO';
    $realpath = file_exists($file) ? realpath($file) : 'N/A';
    echo "<tr><td>$file</td><td>$exists<br><small>$realpath</small></td></tr>";
}
echo "</table>";

echo "<h2>When config.php is loaded</h2>";

if (file_exists('../config.php')) {
    echo "<p>✅ Found ../config.php</p>";
    
    // Check if sas_integration would be loaded
    $configContent = file_get_contents('../config.php');
    if (strpos($configContent, 'sas_integration.php') !== false) {
        echo "<p>✅ config.php loads sas_integration.php</p>";
        
        // Simulate the path
        if (file_exists('../sas_integration.php')) {
            $sasDir = dirname(realpath('../sas_integration.php'));
            echo "<p>sas_integration.php __DIR__ would be: <strong>$sasDir</strong></p>";
            
            $trialExpiredPath = $sasDir . '/includes/trial_expired.php';
            echo "<p>Trial expired path would be: <strong>$trialExpiredPath</strong></p>";
            echo "<p>File exists: " . (file_exists($trialExpiredPath) ? '✅ YES' : '❌ NO') . "</p>";
        }
    }
} else {
    echo "<p>❌ config.php not found in parent directory</p>";
}

echo "<h2>URL Generation Test</h2>";
echo "<table>";
echo "<tr><th>Type</th><th>URL</th></tr>";

$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'];

echo "<tr><td>Relative</td><td>activate_license.php</td></tr>";
echo "<tr><td>Root-relative</td><td>/activate_license.php</td></tr>";
echo "<tr><td>Absolute</td><td>$protocol://$host/activate_license.php</td></tr>";
echo "</table>";

echo "<h2>Test Links</h2>";
echo "<p><a href='activate_license.php'>Relative: activate_license.php</a> (goes to /booking/activate_license.php) ❌</p>";
echo "<p><a href='/activate_license.php'>Root-relative: /activate_license.php</a> (goes to /activate_license.php) ✅</p>";
echo "<p><a href='$protocol://$host/activate_license.php'>Absolute</a> (goes to /activate_license.php) ✅</p>";
?>
