<?php
/**
 * Template Builder Installation Verification
 *
 * Run this script to verify the template builder is properly installed
 * Access via: /verify_template_builder.php
 */

require_once dirname(__FILE__) . '/f_core/config.core.php';

// Only allow in development or for admins
if (!isset($_SESSION['USER_ID']) || $_SESSION['USER_ID'] <= 0) {
    die('Please log in to verify installation.');
}

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Template Builder Installation Verification</title>
    <style>
        body { font-family: Arial, sans-serif; padding: 40px; background: #f5f5f5; }
        .container { max-width: 900px; margin: 0 auto; background: white; padding: 30px; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
        h1 { color: #333; border-bottom: 2px solid #3b82f6; padding-bottom: 10px; }
        .check { margin: 20px 0; padding: 15px; border-radius: 6px; }
        .check.success { background: #d1fae5; border-left: 4px solid #10b981; }
        .check.error { background: #fee2e2; border-left: 4px solid #ef4444; }
        .check.warning { background: #fef3c7; border-left: 4px solid #f59e0b; }
        .check h3 { margin: 0 0 8px 0; }
        .check p { margin: 0; color: #666; }
        .status { font-weight: bold; }
        .success .status { color: #10b981; }
        .error .status { color: #ef4444; }
        .warning .status { color: #f59e0b; }
        .file-list { background: #f9fafb; padding: 10px; border-radius: 4px; margin-top: 10px; font-size: 13px; }
        .code { background: #1f2937; color: #10b981; padding: 15px; border-radius: 6px; margin: 15px 0; overflow-x: auto; }
        .btn { display: inline-block; padding: 10px 20px; background: #3b82f6; color: white; text-decoration: none; border-radius: 6px; margin-top: 20px; }
        .btn:hover { background: #2563eb; }
    </style>
</head>
<body>
    <div class="container">
        <h1>Template Builder Installation Verification</h1>

        <?php
        $errors = [];
        $warnings = [];
        $success = [];

        // Check 1: Database Tables
        echo '<div class="check ';
        $tables = [
            'db_templatebuilder_templates',
            'db_templatebuilder_components',
            'db_templatebuilder_assignments',
            'db_templatebuilder_versions',
            'db_templatebuilder_user_prefs'
        ];

        $tables_exist = true;
        $tables_status = [];

        foreach ($tables as $table) {
            $result = $db->execute("SHOW TABLES LIKE '{$table}'");
            if ($db->num_rows($result) > 0) {
                $tables_status[$table] = 'Found';
            } else {
                $tables_status[$table] = 'Missing';
                $tables_exist = false;
            }
        }

        echo $tables_exist ? 'success' : 'error';
        echo '">';
        echo '<h3><span class="status">' . ($tables_exist ? '✓' : '✗') . '</span> Database Tables</h3>';
        echo '<p>';
        if ($tables_exist) {
            echo 'All 5 template builder tables exist.';
            $success[] = 'Database tables';
        } else {
            echo 'Some tables are missing. Run the SQL migration file.';
            $errors[] = 'Database tables incomplete';
        }
        echo '</p>';
        echo '<div class="file-list">';
        foreach ($tables_status as $table => $status) {
            echo "<div>{$table}: <strong>{$status}</strong></div>";
        }
        echo '</div>';
        echo '</div>';

        // Check 2: Default Components
        echo '<div class="check ';
        $result = $db->execute("SELECT COUNT(*) as count FROM db_templatebuilder_components WHERE is_system = 1");
        $row = $db->fetch_assoc($result);
        $component_count = (int)$row['count'];
        $components_ok = $component_count >= 7;

        echo $components_ok ? 'success' : 'warning';
        echo '">';
        echo '<h3><span class="status">' . ($components_ok ? '✓' : '⚠') . '</span> Default Components</h3>';
        echo '<p>';
        if ($components_ok) {
            echo "Found {$component_count} system components.";
            $success[] = 'Default components';
        } else {
            echo "Found only {$component_count} components. Expected at least 7.";
            $warnings[] = 'Missing some default components';
        }
        echo '</p>';
        echo '</div>';

        // Check 3: PHP Class File
        echo '<div class="check ';
        $class_file = dirname(__FILE__) . '/f_core/f_classes/class.templatebuilder.php';
        $class_exists = file_exists($class_file);

        echo $class_exists ? 'success' : 'error';
        echo '">';
        echo '<h3><span class="status">' . ($class_exists ? '✓' : '✗') . '</span> PHP Class File</h3>';
        echo '<p>';
        if ($class_exists) {
            echo 'VTemplateBuilder class file exists.';
            $success[] = 'PHP class file';
        } else {
            echo 'Class file not found: ' . $class_file;
            $errors[] = 'PHP class file missing';
        }
        echo '</p>';
        echo '</div>';

        // Check 4: Template Files
        echo '<div class="check ';
        $template_files = [
            'f_templates/tpl_frontend/tpl_builder/tpl_builder_main.tpl',
            'f_templates/tpl_backend/tpl_template_manager.tpl'
        ];

        $templates_exist = true;
        foreach ($template_files as $file) {
            if (!file_exists(dirname(__FILE__) . '/' . $file)) {
                $templates_exist = false;
                break;
            }
        }

        echo $templates_exist ? 'success' : 'error';
        echo '">';
        echo '<h3><span class="status">' . ($templates_exist ? '✓' : '✗') . '</span> Smarty Template Files</h3>';
        echo '<p>';
        if ($templates_exist) {
            echo 'All template files exist.';
            $success[] = 'Template files';
        } else {
            echo 'Some template files are missing.';
            $errors[] = 'Template files missing';
        }
        echo '</p>';
        echo '</div>';

        // Check 5: CSS Files
        echo '<div class="check ';
        $css_file = dirname(__FILE__) . '/f_scripts/fe/css/builder/builder.css';
        $css_exists = file_exists($css_file);

        echo $css_exists ? 'success' : 'error';
        echo '">';
        echo '<h3><span class="status">' . ($css_exists ? '✓' : '✗') . '</span> CSS Files</h3>';
        echo '<p>';
        if ($css_exists) {
            echo 'Builder CSS file exists.';
            $success[] = 'CSS files';
        } else {
            echo 'CSS file not found: ' . $css_file;
            $errors[] = 'CSS files missing';
        }
        echo '</p>';
        echo '</div>';

        // Check 6: JavaScript Files
        echo '<div class="check ';
        $js_file = dirname(__FILE__) . '/f_scripts/fe/js/builder/builder-core.js';
        $js_exists = file_exists($js_file);

        echo $js_exists ? 'success' : 'error';
        echo '">';
        echo '<h3><span class="status">' . ($js_exists ? '✓' : '✗') . '</span> JavaScript Files</h3>';
        echo '<p>';
        if ($js_exists) {
            echo 'Builder JavaScript file exists.';
            $success[] = 'JavaScript files';
        } else {
            echo 'JavaScript file not found: ' . $js_file;
            $errors[] = 'JavaScript files missing';
        }
        echo '</p>';
        echo '</div>';

        // Check 7: AJAX Handler
        echo '<div class="check ';
        $ajax_file = dirname(__FILE__) . '/f_modules/m_frontend/templatebuilder_ajax.php';
        $ajax_exists = file_exists($ajax_file);

        echo $ajax_exists ? 'success' : 'error';
        echo '">';
        echo '<h3><span class="status">' . ($ajax_exists ? '✓' : '✗') . '</span> AJAX Handler</h3>';
        echo '<p>';
        if ($ajax_exists) {
            echo 'AJAX handler file exists.';
            $success[] = 'AJAX handler';
        } else {
            echo 'AJAX handler not found: ' . $ajax_file;
            $errors[] = 'AJAX handler missing';
        }
        echo '</p>';
        echo '</div>';

        // Check 8: Management Interface
        echo '<div class="check ';
        $manager_file = dirname(__FILE__) . '/f_modules/m_backend/template_manager.php';
        $manager_exists = file_exists($manager_file);

        echo $manager_exists ? 'success' : 'error';
        echo '">';
        echo '<h3><span class="status">' . ($manager_exists ? '✓' : '✗') . '</span> Management Interface</h3>';
        echo '<p>';
        if ($manager_exists) {
            echo 'Template manager file exists.';
            $success[] = 'Management interface';
        } else {
            echo 'Manager file not found: ' . $manager_file;
            $errors[] = 'Management interface missing';
        }
        echo '</p>';
        echo '</div>';

        // Summary
        echo '<div class="check ';
        if (count($errors) === 0 && count($warnings) === 0) {
            echo 'success';
        } elseif (count($errors) > 0) {
            echo 'error';
        } else {
            echo 'warning';
        }
        echo '">';
        echo '<h3>Installation Summary</h3>';
        echo '<p>';

        if (count($errors) === 0 && count($warnings) === 0) {
            echo '<strong style="color: #10b981;">✓ All checks passed! Template builder is ready to use.</strong>';
        } elseif (count($errors) > 0) {
            echo '<strong style="color: #ef4444;">✗ Installation incomplete. Please fix the errors above.</strong>';
            echo '<div class="code">';
            echo '# To fix database issues, run:<br>';
            echo 'mysql -u username -p database_name < __install/add_template_builder.sql';
            echo '</div>';
        } else {
            echo '<strong style="color: #f59e0b;">⚠ Installation complete with warnings. The system should work but may have limited functionality.</strong>';
        }

        echo '</p>';
        echo '</div>';
        ?>

        <div style="margin-top: 30px; padding-top: 20px; border-top: 1px solid #e5e7eb;">
            <h3>Next Steps:</h3>
            <ol>
                <li>Add "My Templates" link to your navigation menu</li>
                <li>Visit <a href="/templates.php">/templates.php</a> or <a href="/f_modules/m_backend/template_manager.php">/f_modules/m_backend/template_manager.php</a></li>
                <li>Create your first template!</li>
            </ol>

            <a href="/f_modules/m_backend/template_manager.php" class="btn">Go to Template Manager</a>
            <a href="/TEMPLATE_BUILDER_GUIDE.md" class="btn" style="background: #6b7280;">View Documentation</a>
        </div>
    </div>
</body>
</html>
