<?php
declare(strict_types=1);

// --- TAMPER PROTECTION: LAYER 1 ---
function renderTamperAlert(string $reason, string $code = 'SEC_TAMPER_0x1') {
    http_response_code(403);
    echo <<<HTML
    <!DOCTYPE html>
    <html lang="en" class="dark">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>System Locked - Security Alert</title>
        <script src="https://cdn.tailwindcss.com"></script>
        <script>
            tailwind.config = { darkMode: 'class', theme: { extend: { colors: { primary: '#1c70b4', danger: '#ef4444' } } } }
        </script>
    </head>
    <body class="bg-gray-900 text-gray-100 min-h-screen flex items-center justify-center p-4 font-sans selection:bg-danger selection:text-white">
        <div class="max-w-lg w-full bg-gray-800 rounded-2xl shadow-2xl border border-red-900/50 p-8 relative overflow-hidden">
            <div class="absolute top-0 left-0 w-full h-1 bg-red-600 shadow-[0_0_15px_rgba(220,38,38,0.8)]"></div>
            <div class="text-center mb-6">
                <div class="mx-auto flex items-center justify-center h-20 w-20 rounded-full bg-red-900/30 mb-6 border border-red-500/30 shadow-[0_0_20px_rgba(220,38,38,0.2)]">
                    <svg class="h-10 w-10 text-red-500" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path></svg>
                </div>
                <h1 class="text-3xl font-extrabold text-white tracking-tight mb-2">System Locked</h1>
                <p class="text-red-400 font-bold tracking-widest uppercase text-xs">Critical Security Violation</p>
            </div>
            <div class="bg-gray-900/80 rounded-xl p-5 border border-gray-700/50 mb-8 shadow-inner">
                <p class="text-gray-300 text-sm leading-relaxed text-center font-medium">
                    {$reason}
                </p>
            </div>
            
            <div class="flex flex-col gap-4">
                <a href="https://support.christodev.co.za" target="_blank" class="w-full bg-red-600 hover:bg-red-500 text-white font-bold py-3 px-4 rounded-xl shadow-lg transition-all text-center flex items-center justify-center focus:ring-4 focus:ring-red-600/30">
                    <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M18.364 5.636l-3.536 3.536m0 5.656l3.536 3.536M9.172 9.172L5.636 5.636m3.536 9.192l-3.536 3.536M21 12a9 9 0 11-18 0 9 9 0 0118 0zm-5 0a4 4 0 11-8 0 4 4 0 018 0z"></path></svg>
                    Need help? Contact Support
                </a>
            </div>

            <div class="mt-8 pt-5 border-t border-gray-700/50 flex justify-between items-center text-xs text-gray-500 font-mono">
                <span class="bg-gray-900 px-2 py-1 rounded border border-gray-800">ERR: {$code}</span>
                <span class="flex items-center"><svg class="w-3 h-3 mr-1 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z"></path></svg> ChristoDev Engine</span>
            </div>
        </div>
    </body>
    </html>
HTML;
    exit;
}

$clientPath = dirname(__DIR__) . '/app/Core/AppClient.php';

if (!file_exists($clientPath) || filesize($clientPath) < 500) {
    renderTamperAlert('Critical licensing core files are missing or corrupted.');
}

require_once $clientPath;

// --- CONFIGURATION ---
$serverUrl = 'https://license.christodev.co.za'; 

$keyFile = dirname(__DIR__) . '/config/license.key';
$cacheDir = __DIR__ . '/cache';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['new_license_key'])) {
    $newKey = trim(htmlspecialchars($_POST['new_license_key']));
    
    if (!is_dir(dirname($keyFile))) { @mkdir(dirname($keyFile), 0755, true); }
    file_put_contents($keyFile, $newKey);
    
    if (is_dir($cacheDir)) { array_map('unlink', glob("$cacheDir/*.json")); }
    header("Location: " . $_SERVER['REQUEST_URI']);
    exit;
}

$licenseKey = file_exists($keyFile) ? trim(file_get_contents($keyFile)) : '';
$vFile = dirname(__DIR__) . '/version.php';
$appVersion = file_exists($vFile) ? include $vFile : '1.0.0';

$client = new AppClient($serverUrl, $licenseKey, (string)$appVersion, $cacheDir);
$license = $client->verifyLicense();

if (!isset($license->status) || ($license->status !== 'valid' && $license->status !== 'active')) {
    $errorMsg = $license->message ?? 'No active license found. Please enter a valid license key to continue.';
    ?>
    <!DOCTYPE html>
    <html lang="en" class="dark">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Software Activation</title>
        <script src="https://cdn.tailwindcss.com"></script>
        <script>
            tailwind.config = { darkMode: 'class', theme: { extend: { colors: { primary: '#1c70b4' } } } }
        </script>
    </head>
    <body class="bg-gray-50 dark:bg-gray-900 text-gray-900 dark:text-gray-100 min-h-screen flex items-center justify-center p-4 font-sans">
        
        <div class="max-w-lg w-full bg-white dark:bg-gray-800 rounded-2xl shadow-xl border border-gray-200 dark:border-gray-700 p-8">
            <div class="text-center mb-6">
                <div class="mx-auto flex items-center justify-center h-16 w-16 rounded-full bg-red-100 dark:bg-red-900/30 mb-4">
                    <svg class="h-8 w-8 text-red-600 dark:text-red-400" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"></path></svg>
                </div>
                <h1 class="text-2xl font-bold text-gray-900 dark:text-white">Action Required</h1>
                <p class="text-sm text-red-600 dark:text-red-400 font-medium mt-2"><?php echo htmlspecialchars($errorMsg); ?></p>
            </div>

            <form method="POST" action="" class="space-y-6">
                <div>
                    <label class="block text-sm font-bold text-gray-700 dark:text-gray-300 mb-2">Enter License Key</label>
                    <input type="text" name="new_license_key" placeholder="APP-XXXX-XXXX-XXXX" class="w-full px-4 py-3 rounded-lg border border-gray-300 dark:border-gray-600 bg-gray-50 dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none font-mono text-center tracking-widest uppercase transition-all" required autocomplete="off">
                </div>
                <button type="submit" class="w-full bg-primary hover:opacity-90 text-white font-bold py-3 px-4 rounded-lg shadow-sm transition-all focus:ring-4 focus:ring-primary/50 flex items-center justify-center">
                    <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                    Validate & Activate
                </button>
            </form>
            
            <div class="mt-8 pt-6 border-t border-gray-100 dark:border-gray-700 flex flex-col items-center justify-center gap-3">
                <a href="https://support.christodev.co.za" target="_blank" class="text-primary hover:opacity-80 font-bold transition-colors flex items-center text-sm">
                    <svg class="w-4 h-4 mr-1.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M18.364 5.636l-3.536 3.536m0 5.656l3.536 3.536M9.172 9.172L5.636 5.636m3.536 9.192l-3.536 3.536M21 12a9 9 0 11-18 0 9 9 0 0118 0zm-5 0a4 4 0 11-8 0 4 4 0 018 0z"></path></svg>
                    Need help?
                </a>
                <div class="text-xs text-gray-500">
                    Secured by ChristoDev Licensing Engine
                </div>
            </div>
        </div>
    </body>
    </html>
    <?php
    exit;
}

// --- TAMPER PROTECTION FLAG & LOGIC OFFLOAD ---
define('RC_CORE_SECURE', md5('ChristoDev_Secure_Valid_' . date('Y-m-d')));
define('RC_DB_DRIVER', $license->core_logic ?? '');