<?php
/**
 * SEO Manager Console Integration
 * Adds SEO menu to admin console for quick access
 */

// This file should be included in the console menu system

if (!function_exists('add_seo_console_menu')) {
    /**
     * Add SEO menu section to admin console
     */
    function add_seo_console_menu() {
        require_once 'seo_plugin.php';
        require_once 'seo_analytics.php';
        
        // Check if user is admin
        if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
            return;
        }
        
        $db_config = [
            'host' => 'localhost',
            'user' => 'root',
            'password' => '',
            'database' => 'login-table'
        ];
        
        $seo_plugin = new SEOPlugin($db_config);
        $analytics = new SEOAnalytics(new mysqli(
            $db_config['host'],
            $db_config['user'],
            $db_config['password'],
            $db_config['database']
        ));
        
        $health_score = $analytics->generateHealthScore(30);
        
        ?>
        <div class="console-seo-section">
            <h3>📊 SEO Management</h3>
            <ul class="console-seo-menu">
                <li>
                    <a href="admin_seo_manager.php?action=dashboard" title="SEO Dashboard">
                        Dashboard <span style="margin-left: auto; font-weight: bold; color: #667eea;"><?php echo $health_score; ?>%</span>
                    </a>
                </li>
                <li>
                    <a href="admin_seo_manager.php?action=pages" title="Manage Page SEO">
                        Pages & Posts
                    </a>
                </li>
                <li>
                    <a href="admin_seo_manager.php?action=meta-tags" title="Edit Meta Tags">
                        Meta Tags
                    </a>
                </li>
                <li>
                    <a href="admin_seo_manager.php?action=readability" title="Readability Analyzer">
                        Readability
                    </a>
                </li>
                <li>
                    <a href="admin_seo_manager.php?action=keywords" title="Keyword Optimizer">
                        Keywords
                    </a>
                </li>
                <li>
                    <a href="admin_seo_manager.php?action=schema" title="Schema Markup">
                        Schema Markup
                    </a>
                </li>
                <li>
                    <a href="admin_seo_manager.php?action=analytics" title="SEO Analytics">
                        Analytics
                    </a>
                </li>
                <li>
                    <a href="admin_seo_manager.php?action=settings" title="SEO Settings">
                        Settings
                    </a>
                </li>
            </ul>
        </div>
        <?php
    }
}

if (!function_exists('get_seo_console_status')) {
    /**
     * Get SEO status for console display
     */
    function get_seo_console_status() {
        require_once 'seo_plugin.php';
        require_once 'seo_analytics.php';
        require_once 'db_config.php';
        
        $seo_plugin = new SEOPlugin($db_config);
        $analytics = new SEOAnalytics(new mysqli(
            $db_config['host'],
            $db_config['user'],
            $db_config['password'],
            $db_config['database']
        ));
        
        $all_seo = $seo_plugin->getAllSEOData(null, 1000, 0);
        $total_pages = $all_seo->num_rows;
        
        // Count pages by score
        $excellent = 0;
        $good = 0;
        $poor = 0;
        
        while ($page = $all_seo->fetch_assoc()) {
            $score = $seo_plugin->calculateSEOScore($page);
            if ($score >= 80) $excellent++;
            elseif ($score >= 60) $good++;
            else $poor++;
        }
        
        $health_score = $analytics->generateHealthScore(30);
        $report = $analytics->generateReport(30);
        
        return [
            'health_score' => $health_score,
            'total_pages' => $total_pages,
            'excellent' => $excellent,
            'good' => $good,
            'poor' => $poor,
            'impressions' => $report['overview']['total_impressions'] ?? 0,
            'clicks' => $report['overview']['total_clicks'] ?? 0,
            'avg_position' => round($report['overview']['avg_position'] ?? 0, 1),
            'ctr' => round($report['overview']['avg_ctr'] ?? 0, 2)
        ];
    }
}

if (!function_exists('display_seo_console_status')) {
    /**
     * Display SEO status in console header or status bar
     */
    function display_seo_console_status() {
        $status = get_seo_console_status();
        
        $health_color = $status['health_score'] >= 80 ? '#22c55e' : ($status['health_score'] >= 60 ? '#f59e0b' : '#ef4444');
        
        ?>
        <div style="display: flex; align-items: center; gap: 20px; padding: 12px 20px; background: #f0f4ff; border-radius: 8px; margin: 10px 0;">
            <div style="flex: 1;">
                <strong style="color: #667eea; font-size: 14px;">SEO Status</strong>
            </div>
            <div style="display: flex; align-items: center; gap: 15px; font-size: 13px;">
                <div style="text-align: center;">
                    <div style="font-weight: 700; color: #667eea;"><?php echo $status['health_score']; ?>%</div>
                    <div style="color: #7f8c8d;">Health</div>
                </div>
                <div style="width: 1px; height: 30px; background: #e9ecef;"></div>
                <div style="text-align: center;">
                    <div style="font-weight: 700; color: #667eea;"><?php echo $status['excellent']; ?></div>
                    <div style="color: #7f8c8d;">Excellent</div>
                </div>
                <div style="text-align: center;">
                    <div style="font-weight: 700; color: #667eea;"><?php echo $status['good']; ?></div>
                    <div style="color: #7f8c8d;">Good</div>
                </div>
                <div style="text-align: center;">
                    <div style="font-weight: 700; color: #667eea;"><?php echo $status['poor']; ?></div>
                    <div style="color: #7f8c8d;">Poor</div>
                </div>
                <div style="width: 1px; height: 30px; background: #e9ecef;"></div>
                <a href="admin_seo_manager.php" style="background: #667eea; color: #fff; padding: 6px 14px; border-radius: 4px; text-decoration: none; font-weight: 600; font-size: 12px; transition: all 0.3s;">
                    Manage SEO
                </a>
            </div>
        </div>
        <?php
    }
}

if (!function_exists('display_seo_mini_widget')) {
    /**
     * Display mini SEO widget for sidebar
     */
    function display_seo_mini_widget() {
        $status = get_seo_console_status();
        
        ?>
        <div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: #fff; padding: 15px; border-radius: 8px; margin: 10px 0; cursor: pointer;" onclick="window.location.href='admin_seo_manager.php'">
            <div style="display: flex; justify-content: space-between; align-items: center; font-size: 13px; font-weight: 600;">
                <span>📊 SEO Manager</span>
                <span style="background: rgba(255, 255, 255, 0.2); padding: 4px 10px; border-radius: 20px;"><?php echo $status['health_score']; ?>%</span>
            </div>
            <div style="font-size: 11px; opacity: 0.8; margin-top: 6px;">
                <?php echo $status['excellent']; ?> excellent • 
                <?php echo $status['good']; ?> good • 
                <?php echo $status['poor']; ?> needs work
            </div>
        </div>
        <?php
    }
}

?>
