<?php
if (!defined('ABSPATH')) {
    exit;
}
?>

<div class="lm-analytics">
    <div class="lm-analytics__cards">
        <div class="lm-analytics-card">
            <div class="lm-analytics-card__header">
                <span class="lm-analytics-card__label"><?php esc_html_e('Total Articles', 'launchmind-blog'); ?></span>
                <span class="lm-analytics-card__icon lm-analytics-card__icon--blue"><span class="dashicons dashicons-visibility"></span></span>
            </div>
            <div class="lm-analytics-card__value"><?php echo (int) count($posts); ?></div>
            <div class="lm-analytics-card__subtext"><?php esc_html_e('Published on your site', 'launchmind-blog'); ?></div>
        </div>
        <div class="lm-analytics-card">
            <div class="lm-analytics-card__header">
                <span class="lm-analytics-card__label"><?php esc_html_e('This Week', 'launchmind-blog'); ?></span>
                <span class="lm-analytics-card__icon lm-analytics-card__icon--green"><span class="dashicons dashicons-chart-bar"></span></span>
            </div>
            <div class="lm-analytics-card__value"><?php echo (int) $this_week; ?></div>
            <div class="lm-analytics-card__subtext"><?php esc_html_e('New articles', 'launchmind-blog'); ?></div>
        </div>
        <div class="lm-analytics-card">
            <div class="lm-analytics-card__header">
                <span class="lm-analytics-card__label"><?php esc_html_e('This Month', 'launchmind-blog'); ?></span>
                <span class="lm-analytics-card__icon lm-analytics-card__icon--yellow"><span class="dashicons dashicons-chart-bar"></span></span>
            </div>
            <div class="lm-analytics-card__value"><?php echo (int) $this_month; ?></div>
            <div class="lm-analytics-card__subtext"><?php esc_html_e('New articles', 'launchmind-blog'); ?></div>
        </div>
    </div>

    <div class="lm-analytics__section">
        <div class="lm-analytics__section-header">
            <h3><?php esc_html_e('Recent Articles', 'launchmind-blog'); ?></h3>
            <a href="https://launchmind.io/backlinks/dashboard?utm_source=wordpress&utm_medium=plugin" target="_blank" rel="noopener noreferrer" class="lm-link">
                <?php esc_html_e('View Full Analytics', 'launchmind-blog'); ?>
                <span class="dashicons dashicons-external"></span>
            </a>
        </div>
        <div class="lm-analytics__table">
            <table>
                <thead>
                    <tr>
                        <th><?php esc_html_e('Article', 'launchmind-blog'); ?></th>
                        <th><?php esc_html_e('Published', 'launchmind-blog'); ?></th>
                        <th><?php esc_html_e('Status', 'launchmind-blog'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach (array_slice($posts, 0, 5) as $post): ?>
                    <tr>
                        <td>
                            <div class="lm-article-row">
                                <?php if (!empty($post['coverImage'])): ?>
                                    <img src="<?php echo esc_url($post['coverImage']); ?>" alt="">
                                <?php endif; ?>
                                <div>
                                    <strong><?php echo esc_html(wp_trim_words($post['title'], 8)); ?></strong>
                                    <span><?php echo esc_html($post['slug']); ?></span>
                                </div>
                            </div>
                        </td>
                        <td><?php echo !empty($post['publishedAt']) ? esc_html(date_i18n(get_option('date_format'), strtotime($post['publishedAt']))) : '-'; ?></td>
                        <td><span class="lm-badge lm-badge--success"><?php esc_html_e('Published', 'launchmind-blog'); ?></span></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

    <div class="lm-analytics__cta">
        <h3>📊 <?php esc_html_e('Detailed Analytics', 'launchmind-blog'); ?></h3>
        <p><?php esc_html_e('View detailed performance metrics, SEO scores, and content recommendations in your Launchmind dashboard.', 'launchmind-blog'); ?></p>
        <a href="https://launchmind.io/backlinks/dashboard?utm_source=wordpress&utm_medium=plugin" target="_blank" rel="noopener noreferrer" class="button button-primary">
            <span class="dashicons dashicons-external"></span>
            <?php esc_html_e('Open Launchmind Dashboard', 'launchmind-blog'); ?>
        </a>
    </div>
</div>
