<?php
/**
 * Test Encryption System
 * This file tests if encryption/decryption works correctly
 */

session_start();

// Check if user is logged in
if (!isset($_SESSION['panel_logged_in']) || $_SESSION['panel_logged_in'] !== true) {
    die('Please login first: <a href="login.php">Login</a>');
}

// Define BASE_PATH
if (!defined('BASE_PATH')) {
    define('BASE_PATH', dirname(__DIR__));
}

// Include encryption functions
require_once __DIR__ . '/../includes/encryption.php';

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test Encryption</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
            background: #f5f5f5;
        }
        .test-box {
            background: white;
            padding: 20px;
            margin: 20px 0;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .success { color: #28a745; }
        .error { color: #dc3545; }
        h1 { color: #333; }
        h2 { color: #666; margin-top: 0; }
        pre {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 4px;
            overflow-x: auto;
        }
        .btn {
            display: inline-block;
            padding: 10px 20px;
            background: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            margin-top: 20px;
        }
        .btn:hover {
            background: #0056b3;
        }
    </style>
</head>
<body>
    <h1>🔐 Encryption System Test</h1>
    
    <div class="test-box">
        <h2>Test 1: Encrypt & Decrypt</h2>
        <?php
        $testToken = '1234567890:ABCdefGHIjklMNOpqrsTUVwxyz';
        $testChatId = '-1001234567890';
        
        echo "<p><strong>Original Bot Token:</strong> <code>$testToken</code></p>";
        echo "<p><strong>Original Chat ID:</strong> <code>$testChatId</code></p>";
        
        $encryptedToken = encryptData($testToken);
        $encryptedChatId = encryptData($testChatId);
        
        echo "<p><strong>Encrypted Bot Token:</strong></p>";
        echo "<pre>$encryptedToken</pre>";
        
        echo "<p><strong>Encrypted Chat ID:</strong></p>";
        echo "<pre>$encryptedChatId</pre>";
        
        $decryptedToken = decryptData($encryptedToken);
        $decryptedChatId = decryptData($encryptedChatId);
        
        echo "<p><strong>Decrypted Bot Token:</strong> <code>$decryptedToken</code></p>";
        echo "<p><strong>Decrypted Chat ID:</strong> <code>$decryptedChatId</code></p>";
        
        if ($testToken === $decryptedToken && $testChatId === $decryptedChatId) {
            echo '<p class="success">✅ Encryption/Decryption works correctly!</p>';
        } else {
            echo '<p class="error">❌ Encryption/Decryption failed!</p>';
        }
        ?>
    </div>
    
    <div class="test-box">
        <h2>Test 2: Config File Access</h2>
        <?php
        $configFile = __DIR__ . '/../config/config.php';
        
        if (file_exists($configFile)) {
            echo '<p class="success">✅ Config file exists</p>';
            echo "<p><strong>Path:</strong> <code>$configFile</code></p>";
            
            if (is_readable($configFile)) {
                echo '<p class="success">✅ Config file is readable</p>';
            } else {
                echo '<p class="error">❌ Config file is NOT readable</p>';
            }
            
            if (is_writable($configFile)) {
                echo '<p class="success">✅ Config file is writable</p>';
            } else {
                echo '<p class="error">❌ Config file is NOT writable</p>';
                echo '<p>Run: <code>chmod 666 ' . $configFile . '</code></p>';
            }
        } else {
            echo '<p class="error">❌ Config file not found!</p>';
        }
        ?>
    </div>
    
    <div class="test-box">
        <h2>Test 3: Current Config Values</h2>
        <?php
        require_once $configFile;
        
        echo "<p><strong>TELEGRAM_BOT_TOKEN (encrypted):</strong></p>";
        echo "<pre>" . TELEGRAM_BOT_TOKEN . "</pre>";
        
        echo "<p><strong>TELEGRAM_CHAT_ID (encrypted):</strong></p>";
        echo "<pre>" . TELEGRAM_CHAT_ID . "</pre>";
        
        echo "<p><strong>Decrypted Bot Token:</strong></p>";
        echo "<pre>" . decryptData(TELEGRAM_BOT_TOKEN) . "</pre>";
        
        echo "<p><strong>Decrypted Chat ID:</strong></p>";
        echo "<pre>" . decryptData(TELEGRAM_CHAT_ID) . "</pre>";
        ?>
    </div>
    
    <div class="test-box">
        <h2>Test 4: Session Check</h2>
        <?php
        echo "<p><strong>Session Status:</strong> ";
        if (isset($_SESSION['panel_logged_in']) && $_SESSION['panel_logged_in'] === true) {
            echo '<span class="success">✅ Logged in</span></p>';
        } else {
            echo '<span class="error">❌ Not logged in</span></p>';
        }
        
        echo "<p><strong>Session Data:</strong></p>";
        echo "<pre>" . print_r($_SESSION, true) . "</pre>";
        ?>
    </div>
    
    <a href="settings.php" class="btn">← Back to Settings</a>
    <a href="index.php" class="btn">← Back to Dashboard</a>
</body>
</html>
