<?php
/**
 * Script de Testare - Sistem Categorii cu Mega-Menu
 * Rulați acest script pentru a verifica că totul este instalat corect
 * URL: http://localhost/test-categories.php
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once 'includes/Database.php';

// Start HTML
?>
<!DOCTYPE html>
<html lang="ro">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test Sistem Categorii</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        .container {
            max-width: 900px;
            margin: 0 auto;
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            overflow: hidden;
        }
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 40px;
            text-align: center;
        }
        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
            font-weight: 700;
        }
        .header p {
            font-size: 1.1em;
            opacity: 0.9;
        }
        .content {
            padding: 40px;
        }
        .test-section {
            margin-bottom: 30px;
            padding: 25px;
            background: #f8f9fa;
            border-radius: 12px;
            border-left: 4px solid #667eea;
        }
        .test-section h2 {
            color: #333;
            margin-bottom: 15px;
            font-size: 1.5em;
        }
        .status {
            display: inline-block;
            padding: 8px 16px;
            border-radius: 20px;
            font-weight: 600;
            font-size: 0.9em;
            margin-bottom: 15px;
        }
        .status.success {
            background: #d4edda;
            color: #155724;
        }
        .status.error {
            background: #f8d7da;
            color: #721c24;
        }
        .status.warning {
            background: #fff3cd;
            color: #856404;
        }
        .detail {
            margin: 10px 0;
            padding: 12px;
            background: white;
            border-radius: 8px;
            font-size: 0.95em;
        }
        .detail strong {
            color: #667eea;
            display: inline-block;
            min-width: 200px;
        }
        .code {
            background: #2d3748;
            color: #a0aec0;
            padding: 15px;
            border-radius: 8px;
            font-family: 'Courier New', monospace;
            font-size: 0.9em;
            overflow-x: auto;
            margin-top: 10px;
        }
        .button-group {
            margin-top: 30px;
            text-align: center;
        }
        .btn {
            display: inline-block;
            padding: 12px 30px;
            margin: 0 10px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            text-decoration: none;
            border-radius: 25px;
            font-weight: 600;
            transition: transform 0.2s;
        }
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 25px rgba(102, 126, 234, 0.4);
        }
        .icon {
            font-size: 1.5em;
            margin-right: 10px;
        }
        .category-preview {
            background: white;
            border: 2px solid #e2e8f0;
            border-radius: 8px;
            padding: 10px;
            margin: 5px 0;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .category-preview .name {
            font-weight: 600;
            color: #2d3748;
        }
        .category-preview .badge {
            background: #667eea;
            color: white;
            padding: 4px 12px;
            border-radius: 12px;
            font-size: 0.8em;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🚀 Test Sistem Categorii</h1>
            <p>Verificare automată a instalării și configurării</p>
        </div>
        
        <div class="content">
            <?php
            $allTestsPassed = true;
            $db = Database::getInstance();
            
            // TEST 1: Verificare conexiune bază de date
            echo '<div class="test-section">';
            echo '<h2>1. Conexiune Bază de Date</h2>';
            try {
                $result = $db->fetchOne("SELECT DATABASE() as dbname");
                echo '<div class="status success">✅ SUCCES</div>';
                echo '<div class="detail"><strong>Baza de date activă:</strong> ' . $result['dbname'] . '</div>';
            } catch (Exception $e) {
                echo '<div class="status error">❌ EROARE</div>';
                echo '<div class="detail">Nu se poate conecta la baza de date: ' . $e->getMessage() . '</div>';
                $allTestsPassed = false;
            }
            echo '</div>';
            
            // TEST 2: Verificare tabel categories
            echo '<div class="test-section">';
            echo '<h2>2. Tabel Categories</h2>';
            try {
                $result = $db->fetchOne("SHOW TABLES LIKE 'categories'");
                if ($result) {
                    echo '<div class="status success">✅ EXISTĂ</div>';
                    
                    // Count categories
                    $count = $db->fetchOne("SELECT COUNT(*) as total FROM categories");
                    $activeCount = $db->fetchOne("SELECT COUNT(*) as total FROM categories WHERE is_active = 1");
                    $rootCount = $db->fetchOne("SELECT COUNT(*) as total FROM categories WHERE parent_id IS NULL");
                    
                    echo '<div class="detail"><strong>Total categorii:</strong> ' . $count['total'] . '</div>';
                    echo '<div class="detail"><strong>Categorii active:</strong> ' . $activeCount['total'] . '</div>';
                    echo '<div class="detail"><strong>Categorii root:</strong> ' . $rootCount['total'] . '</div>';
                    
                    // Preview some categories
                    $categories = $db->fetchAll("SELECT name, slug, icon FROM categories WHERE parent_id IS NULL AND is_active = 1 LIMIT 5");
                    if (!empty($categories)) {
                        echo '<div style="margin-top: 15px;"><strong>Preview categorii root:</strong></div>';
                        foreach ($categories as $cat) {
                            echo '<div class="category-preview">';
                            echo '<div class="name">' . ($cat['icon'] ? '<i class="' . $cat['icon'] . '"></i> ' : '') . htmlspecialchars($cat['name']) . '</div>';
                            echo '<div class="badge">' . htmlspecialchars($cat['slug']) . '</div>';
                            echo '</div>';
                        }
                    }
                } else {
                    echo '<div class="status error">❌ NU EXISTĂ</div>';
                    echo '<div class="detail">Tabelul "categories" nu a fost găsit. Rulați migration_categories_system.sql</div>';
                    $allTestsPassed = false;
                }
            } catch (Exception $e) {
                echo '<div class="status error">❌ EROARE</div>';
                echo '<div class="detail">' . $e->getMessage() . '</div>';
                $allTestsPassed = false;
            }
            echo '</div>';
            
            // TEST 3: Verificare tabel category_promo_cards
            echo '<div class="test-section">';
            echo '<h2>3. Tabel Category Promo Cards</h2>';
            try {
                $result = $db->fetchOne("SHOW TABLES LIKE 'category_promo_cards'");
                if ($result) {
                    echo '<div class="status success">✅ EXISTĂ</div>';
                    
                    $count = $db->fetchOne("SELECT COUNT(*) as total FROM category_promo_cards");
                    $activeCount = $db->fetchOne("SELECT COUNT(*) as total FROM category_promo_cards WHERE is_active = 1");
                    
                    echo '<div class="detail"><strong>Total carduri promoționale:</strong> ' . $count['total'] . '</div>';
                    echo '<div class="detail"><strong>Carduri active:</strong> ' . $activeCount['total'] . '</div>';
                    
                    // Preview promo cards
                    $cards = $db->fetchAll("SELECT title, badge_text, price_sale FROM category_promo_cards WHERE is_active = 1 LIMIT 3");
                    if (!empty($cards)) {
                        echo '<div style="margin-top: 15px;"><strong>Preview carduri:</strong></div>';
                        foreach ($cards as $card) {
                            echo '<div class="category-preview">';
                            echo '<div class="name">' . htmlspecialchars($card['title']) . '</div>';
                            if ($card['badge_text']) {
                                echo '<div class="badge">' . htmlspecialchars($card['badge_text']) . '</div>';
                            }
                            echo '</div>';
                        }
                    }
                } else {
                    echo '<div class="status error">❌ NU EXISTĂ</div>';
                    $allTestsPassed = false;
                }
            } catch (Exception $e) {
                echo '<div class="status error">❌ EROARE</div>';
                echo '<div class="detail">' . $e->getMessage() . '</div>';
                $allTestsPassed = false;
            }
            echo '</div>';
            
            // TEST 4: Verificare tabel category_settings
            echo '<div class="test-section">';
            echo '<h2>4. Tabel Category Settings</h2>';
            try {
                $result = $db->fetchOne("SHOW TABLES LIKE 'category_settings'");
                if ($result) {
                    echo '<div class="status success">✅ EXISTĂ</div>';
                    
                    $count = $db->fetchOne("SELECT COUNT(*) as total FROM category_settings");
                    echo '<div class="detail"><strong>Total setări:</strong> ' . $count['total'] . '</div>';
                    
                    // Show all settings
                    $settings = $db->fetchAll("SELECT setting_key, setting_value, description FROM category_settings");
                    if (!empty($settings)) {
                        echo '<div style="margin-top: 15px;"><strong>Setări configurate:</strong></div>';
                        foreach ($settings as $setting) {
                            echo '<div class="detail">';
                            echo '<strong>' . htmlspecialchars($setting['setting_key']) . ':</strong> ';
                            echo htmlspecialchars($setting['setting_value']);
                            if ($setting['description']) {
                                echo ' <span style="color: #718096;">(' . htmlspecialchars($setting['description']) . ')</span>';
                            }
                            echo '</div>';
                        }
                    }
                } else {
                    echo '<div class="status error">❌ NU EXISTĂ</div>';
                    $allTestsPassed = false;
                }
            } catch (Exception $e) {
                echo '<div class="status error">❌ EROARE</div>';
                echo '<div class="detail">' . $e->getMessage() . '</div>';
                $allTestsPassed = false;
            }
            echo '</div>';
            
            // TEST 5: Verificare fișier CategoryManager.php
            echo '<div class="test-section">';
            echo '<h2>5. Fișier CategoryManager.php</h2>';
            if (file_exists(__DIR__ . '/includes/CategoryManager.php')) {
                echo '<div class="status success">✅ EXISTĂ</div>';
                echo '<div class="detail"><strong>Locație:</strong> includes/CategoryManager.php</div>';
                
                // Try to load it
                try {
                    require_once __DIR__ . '/includes/CategoryManager.php';
                    $catManager = CategoryManager::getInstance();
                    echo '<div class="detail"><strong>Status:</strong> Clasa încărcată cu succes</div>';
                    
                    // Test a method
                    $rootCats = $catManager->getRootCategories();
                    echo '<div class="detail"><strong>Test funcție:</strong> getRootCategories() returnează ' . count($rootCats) . ' categorii</div>';
                } catch (Exception $e) {
                    echo '<div class="status warning">⚠️ AVERTISMENT</div>';
                    echo '<div class="detail">Eroare la încărcare: ' . $e->getMessage() . '</div>';
                }
            } else {
                echo '<div class="status error">❌ NU EXISTĂ</div>';
                echo '<div class="detail">Fișierul CategoryManager.php nu a fost găsit în includes/</div>';
                $allTestsPassed = false;
            }
            echo '</div>';
            
            // REZULTAT FINAL
            echo '<div class="test-section" style="border-left-color: ' . ($allTestsPassed ? '#28a745' : '#dc3545') . ';">';
            echo '<h2>📊 Rezultat Final</h2>';
            if ($allTestsPassed) {
                echo '<div class="status success" style="font-size: 1.2em;">✅ TOATE TESTELE AU TRECUT</div>';
                echo '<div class="detail">Sistemul de categorii este instalat corect și funcțional!</div>';
                echo '<div class="button-group">';
                echo '<a href="/" class="btn">🏠 Vezi Homepage</a>';
                echo '<a href="/shop-catalog-electronics.php" class="btn">🛍️ Vezi Catalog</a>';
                echo '</div>';
            } else {
                echo '<div class="status error" style="font-size: 1.2em;">❌ UNELE TESTE AU EȘUAT</div>';
                echo '<div class="detail">Verificați erorile de mai sus și rulați migration_categories_system.sql</div>';
                echo '<div class="code">mysql -u root -p cartzilla_shop < database/migration_categories_system.sql</div>';
            }
            echo '</div>';
            ?>
            
            <div class="button-group">
                <a href="database/CATEGORIES_README.md" class="btn">📖 Documentație</a>
                <a href="CATEGORIES_QUICK_START.md" class="btn">🚀 Quick Start</a>
            </div>
        </div>
    </div>
</body>
</html>

