<?php
/**
 * SEO Manager Dashboard Integration
 * Displays SEO widget and quick access on admin dashboard
 */

// This file should be included in admin_dashboard.php

if (!function_exists('display_seo_manager_widget')) {
    /**
     * Display SEO Manager widget on dashboard
     */
    function display_seo_manager_widget() {
        require_once 'seo_plugin.php';
        require_once 'seo_analytics.php';
        
        $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']
        ));
        
        $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);
        
        ?>
        <div class="seo-manager-widget" onclick="window.location.href='admin_seo_manager.php'">
            <h2>📊 SEO Manager</h2>
            <p>Comprehensive SEO optimization tool with readability, keyword analysis, and schema markup</p>
            
            <div class="seo-manager-widget-features">
                <div class="seo-feature">
                    <span class="seo-feature-icon">📄</span>
                    Pages: <?php echo $total_pages; ?>
                </div>
                <div class="seo-feature">
                    <span class="seo-feature-icon">⭐</span>
                    Excellent: <?php echo $excellent; ?>
                </div>
                <div class="seo-feature">
                    <span class="seo-feature-icon">👍</span>
                    Good: <?php echo $good; ?>
                </div>
                <div class="seo-feature">
                    <span class="seo-feature-icon">⚠️</span>
                    Needs Work: <?php echo $poor; ?>
                </div>
                <div class="seo-feature">
                    <span class="seo-feature-icon">💪</span>
                    Health: <?php echo $health_score; ?>%
                </div>
                <div class="seo-feature">
                    <span class="seo-feature-icon">🔍</span>
                    Analytics Ready
                </div>
            </div>
            
            <span class="seo-badge">👉 Click to manage</span>
        </div>
        <?php
    }
}

if (!function_exists('display_seo_quick_stats')) {
    /**
     * Display SEO Quick Stats on dashboard
     */
    function display_seo_quick_stats() {
        require_once 'seo_plugin.php';
        require_once 'seo_analytics.php';
        require_once 'db_config.php';
        
        $analytics = new SEOAnalytics(new mysqli(
            $db_config['host'],
            $db_config['user'],
            $db_config['password'],
            $db_config['database']
        ));
        
        $report = $analytics->generateReport(30);
        $growth = $analytics->getGrowthMetrics(30);
        
        ?>
        <div class="seo-quick-stats">
            <div class="seo-stat-card">
                <div class="seo-stat-label">👀 Impressions</div>
                <div class="seo-stat-value"><?php echo number_format($report['overview']['total_impressions'] ?? 0); ?></div>
                <div class="seo-stat-change <?php echo $growth['impressions_growth'] > 0 ? 'positive' : 'negative'; ?>">
                    <?php echo ($growth['impressions_growth'] > 0 ? '↑' : '↓') . abs($growth['impressions_growth']) . '%'; ?>
                </div>
            </div>
            
            <div class="seo-stat-card">
                <div class="seo-stat-label">🖱️ Clicks</div>
                <div class="seo-stat-value"><?php echo number_format($report['overview']['total_clicks'] ?? 0); ?></div>
                <div class="seo-stat-change <?php echo $growth['clicks_growth'] > 0 ? 'positive' : 'negative'; ?>">
                    <?php echo ($growth['clicks_growth'] > 0 ? '↑' : '↓') . abs($growth['clicks_growth']) . '%'; ?>
                </div>
            </div>
            
            <div class="seo-stat-card">
                <div class="seo-stat-label">📊 CTR</div>
                <div class="seo-stat-value"><?php echo round($report['overview']['avg_ctr'] ?? 0, 2); ?>%</div>
                <div class="seo-stat-change positive">Last 30 days</div>
            </div>
            
            <div class="seo-stat-card">
                <div class="seo-stat-label">📍 Avg Position</div>
                <div class="seo-stat-value"><?php echo round($report['overview']['avg_position'] ?? 0, 1); ?></div>
                <div class="seo-stat-change">Search ranking</div>
            </div>
        </div>
        <?php
    }
}

if (!function_exists('get_seo_recommendations')) {
    /**
     * Get SEO recommendations for display
     */
    function get_seo_recommendations() {
        require_once 'seo_plugin.php';
        require_once 'db_config.php';
        
        $seo_plugin = new SEOPlugin($db_config);
        $all_seo = $seo_plugin->getAllSEOData(null, 100, 0);
        
        $recommendations = [];
        $pages_with_issues = 0;
        
        while ($page = $all_seo->fetch_assoc()) {
            $suggestions = $seo_plugin->generateSuggestions($page);
            if (!empty($suggestions)) {
                $pages_with_issues++;
                if (count($recommendations) < 5) {
                    foreach ($suggestions as $suggestion) {
                        if (count($recommendations) < 5) {
                            $recommendations[] = $suggestion['message'];
                        }
                    }
                }
            }
        }
        
        return [
            'recommendations' => $recommendations,
            'pages_with_issues' => $pages_with_issues
        ];
    }
}

if (!function_exists('display_seo_recommendations')) {
    /**
     * Display SEO recommendations panel
     */
    function display_seo_recommendations() {
        $data = get_seo_recommendations();
        
        if (empty($data['recommendations'])) {
            return;
        }
        
        ?>
        <div class="seo-recommendations">
            <div class="seo-recommendations-title">
                🎯 SEO Optimization Tips (<?php echo $data['pages_with_issues']; ?> pages need attention)
            </div>
            <ul class="seo-recommendations-list">
                <?php foreach (array_slice($data['recommendations'], 0, 5) as $rec): ?>
                    <li><?php echo $rec; ?></li>
                <?php endforeach; ?>
            </ul>
            <a href="admin_seo_manager.php" class="btn btn-primary" style="margin-top: 15px;">
                View Full SEO Manager
            </a>
        </div>
        <?php
    }
}
?>
