<?php
// Site Content Analyzer UI
if ( ! defined( 'ABSPATH' ) ) exit;

// Get last scan information
$last_scan_time = get_option('blogibot_last_content_scan', false);
$scan_in_progress = get_transient('blogibot_content_scan_in_progress');
$total_posts_scanned = get_option('blogibot_total_posts_scanned', 0);

// Count only posts from the last 14 days (same filter as in the scan logic)
$date_14_days_ago = date('Y-m-d', strtotime('-14 days'));
$recent_posts_query = new WP_Query([
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'fields' => 'ids',
    'date_query' => [
        [
            'after' => $date_14_days_ago,
            'inclusive' => true,
        ],
    ],
]);
$total_posts_found = $recent_posts_query->found_posts;

// Check if Yoast SEO is active
$yoast_active = is_plugin_active('wordpress-seo/wp-seo.php');

// Get fingerprint data
$content_fingerprints = get_option('blogibot_content_fingerprints', []);
$fingerprint_count = count($content_fingerprints);

// Calculate scan statistics
$scan_coverage = $total_posts_found > 0 ? round(($total_posts_scanned / $total_posts_found) * 100, 1) : 0;
?>

<div class="blog82ai-site-content-analyzer">
    <!-- Header Section -->
    <div class="page-header">
        <h2>Site Content Analyzer</h2>
        <p>Scan your existing posts from the last 14 days and Yoast SEO data to improve AI content uniqueness.</p>
    </div>

    <!-- Scan Status Overview -->
    <div class="blog82ai-scan-overview">
        <div class="blog82ai-overview-metrics">
            <div class="blog82ai-metric-item">
                <div class="blog82ai-metric-value"><?php echo number_format($total_posts_found); ?></div>
                <div class="blog82ai-metric-label">Recent Posts</div>
                <div class="blog82ai-metric-description">Last 14 days</div>
            </div>
            <div class="blog82ai-metric-item">
                <div class="blog82ai-metric-value"><?php echo number_format($total_posts_scanned); ?></div>
                <div class="blog82ai-metric-label">Posts Analyzed</div>
                <div class="blog82ai-metric-description">Content synchronized</div>
            </div>
            <div class="blog82ai-metric-item">
                <div class="blog82ai-metric-value"><?php echo $scan_coverage; ?>%</div>
                <div class="blog82ai-metric-label">Coverage</div>
                <div class="blog82ai-metric-description">Scan completion rate</div>
            </div>
            <div class="blog82ai-metric-item">
                <div class="blog82ai-metric-value"><?php echo number_format($fingerprint_count); ?></div>
                <div class="blog82ai-metric-label">Fingerprints</div>
                <div class="blog82ai-metric-description">Content signatures stored</div>
            </div>
            <div class="blog82ai-metric-item">
                <div class="blog82ai-metric-value">
                    <?php if ($yoast_active): ?>
                        <span class="status-active">✓</span>
                    <?php else: ?>
                        <span class="status-inactive">✗</span>
                    <?php endif; ?>
                </div>
                <div class="blog82ai-metric-label">Yoast SEO</div>
                <div class="blog82ai-metric-description">
                    <?php echo $yoast_active ? 'Active' : 'Not detected'; ?>
                </div>
            </div>
        </div>
    </div>

    <!-- Last Scan Information -->
    <div class="blog82ai-last-scan-info">
        <div class="blog82ai-scan-status">
            <div class="scan-status-header">
                <h3>
                    <span class="scan-icon">🔍</span>
                    Last Scan Status
                </h3>
                <?php if ($scan_in_progress): ?>
                    <span class="status-badge status-running">
                        <span class="spinner"></span>
                        Scanning in Progress
                    </span>
                <?php elseif ($last_scan_time): ?>
                    <span class="status-badge status-completed">
                        <i class="dashicons dashicons-yes-alt"></i>
                        Completed
                    </span>
                <?php else: ?>
                    <span class="status-badge status-pending">
                        <i class="dashicons dashicons-clock"></i>
                        Not Started
                    </span>
                <?php endif; ?>
            </div>

            <div class="scan-details">
                <?php if ($last_scan_time): ?>
                    <div class="scan-detail-item">
                        <strong>Last Scan:</strong>
                        <span><?php echo date('F j, Y \a\t g:i A', $last_scan_time); ?></span>
                        <span class="time-ago">(<?php echo human_time_diff($last_scan_time); ?> ago)</span>
                    </div>
                    <div class="scan-detail-item">
                        <strong>Posts Scanned:</strong>
                        <span><?php echo number_format($total_posts_scanned); ?> of <?php echo number_format($total_posts_found); ?></span>
                    </div>
                    <div class="scan-detail-item">
                        <strong>Yoast Data:</strong>
                        <span><?php echo $yoast_active ? 'Included' : 'Not available'; ?></span>
                    </div>
                    <div class="scan-detail-item">
                        <strong>Duration:</strong>
                        <span><?php echo get_option('blogibot_last_scan_duration', 'Unknown'); ?></span>
                    </div>
                <?php else: ?>
                    <div class="scan-detail-item no-scan">
                        <strong>No Scan Performed Yet</strong>
                        <span>No content scan has been performed yet. Click "Sync Now" to analyze your existing content.</span>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Content Sync Section -->
    <div class="blog82ai-sync-section">
        <div class="sync-action-card">
            <div class="sync-card-header">
                <h3>
                    <span class="sync-icon">📊</span>
                    Content Analysis & Sync
                </h3>
            </div>
            
            <div class="sync-card-body">
                <div class="sync-description">
                    <p>What will be analyzed:</p>
                    <ul class="feature-list">
                        <li>✓ Post titles and content</li>
                        <li>✓ Publication dates and categories</li>
                        <li>✓ Post excerpts and metadata</li>
                        <?php if ($yoast_active): ?>
                            <li>✓ Yoast SEO meta titles</li>
                            <li>✓ Yoast SEO meta descriptions</li>
                            <li>✓ Focus keywords and SEO scores</li>
                        <?php else: ?>
                            <li class="feature-disabled">✗ Yoast SEO data (plugin not active)</li>
                        <?php endif; ?>
                    </ul>
                </div>
                
                <div class="sync-benefits">
                    <p>Benefits of content analysis:</p>
                    <ul class="benefits-list">
                        <li>🎯 Improved AI content uniqueness</li>
                        <li>🔍 Avoid duplicate content creation</li>
                        <li>📈 Better SEO optimization</li>
                        <li>⚡ Faster content generation</li>
                        <li>🎨 More relevant AI suggestions</li>
                    </ul>
                </div>
            </div>
            
            <div class="sync-card-footer">
                <div class="sync-progress" style="<?php echo !$scan_in_progress ? 'display: none;' : ''; ?>">
                    <div class="progress-bar">
                        <div class="progress-fill" style="width: 0%;"></div>
                    </div>
                    <div class="progress-text">Initializing scan...</div>
                </div>
                
                <div class="sync-actions">
                    <button type="button" 
                            class="blog82ai-btn blog82ai-btn-primary" 
                            id="start-content-sync"
                            <?php echo $scan_in_progress ? 'disabled' : ''; ?>>
                        <span class="btn-icon"><i class="dashicons dashicons-cloud-upload"></i></span>
                        <span class="btn-text">
                            <?php echo $scan_in_progress ? 'Scanning...' : 'Sync Now'; ?>
                        </span>
                    </button>
                    
                    <?php if ($scan_in_progress): ?>
                        <button type="button" class="blog82ai-btn blog82ai-btn-secondary" id="reset-content-scan">
                            <span class="btn-icon"><i class="dashicons dashicons-image-rotate"></i></span>
                            <span class="btn-text">Reset</span>
                        </button>
                    <?php endif; ?>
                    
                    <?php if ($last_scan_time): ?>
                        <button type="button" class="blog82ai-btn blog82ai-btn-secondary" id="view-scan-log">
                            <span class="btn-icon"><i class="dashicons dashicons-list-view"></i></span>
                            <span class="btn-text">View Log</span>
                        </button>
                    <?php endif; ?>
                </div>
                
                <div class="sync-note">
                    <small>
                        <strong>Note:</strong> Large sites may take several minutes to scan. The process runs in the background and you can safely navigate away.
                    </small>
                </div>
            </div>
        </div>
    </div>
</div>
