<?php
// ==================================================
// Nuclear Fancy Pants Edition™ — NUCLEAR+++
// File: /templates/content-freshness.php
// Description: Content Health dashboard template - NUCLEAR+++
// Last Edited: 2026-04-26
// ==================================================

defined('ABSPATH') || exit;

// $needs_attention, $stale_posts, $total_published set by ContentFreshness::render()
$attention_count = count($needs_attention);
$stale_count     = count($stale_posts);
?>
<div class="wrap" id="amai-content-health-wrap">
    <div class="amai-ch-header">
        <div>
            <h1 style="font-family:'Nunito',sans-serif;font-size:28px;font-weight:800;color:#D97757;-webkit-text-fill-color:#D97757 !important;background:none !important;margin:0 0 4px;">Content Health</h1>
            <p class="amai-ch-subtitle">
                <?php if ($attention_count > 0): ?>
                    <?php echo $attention_count; ?> of <?php echo $total_published; ?> post<?php echo $total_published !== 1 ? 's' : ''; ?> need<?php echo $total_published === 1 ? 's' : ''; ?> attention.
                <?php else: ?>
                    All <?php echo $total_published; ?> published post<?php echo $total_published !== 1 ? 's' : ''; ?> are in good shape.
                <?php endif; ?>
            </p>
        </div>
        <button type="button" class="amai-ch-rescan-btn" id="amai-ch-rescan-btn">
            Re-analyze all posts
        </button>
    </div>
    <div class="amai-ch-rescan-status" id="amai-ch-rescan-status"></div>

    <?php // ── Needs Attention ─────────────────────────────── ?>
    <div class="amai-ch-section">
        <div class="amai-ch-section-header">
            <div class="amai-ch-section-title">Needs Attention
                <?php if ($attention_count > 0): ?>
                    <span class="amai-ch-section-count"><?php echo $attention_count; ?></span>
                <?php endif; ?>
            </div>
            <div class="amai-ch-section-desc">These posts have room to improve. Open any one and use Auto-Optimize to fix issues.</div>
        </div>

        <?php if (empty($needs_attention)): ?>
            <div class="amai-ch-empty">
                <div class="amai-ch-empty-icon">&#10003;</div>
                <div class="amai-ch-empty-text">All your analyzed posts are scoring 80 or above.</div>
            </div>
        <?php else: ?>
            <div class="amai-ch-table">
                <div class="amai-ch-table-head">
                    <div class="amai-ch-col amai-ch-col--title">Post</div>
                    <div class="amai-ch-col amai-ch-col--score">Content Score</div>
                    <div class="amai-ch-col amai-ch-col--score">Readability</div>
                    <div class="amai-ch-col amai-ch-col--issues">Issues</div>
                    <div class="amai-ch-col amai-ch-col--action"></div>
                </div>
                <?php foreach ($needs_attention as $item): ?>
                    <?php
                    $post        = $item['post'];
                    $score_s     = $item['score_seo'];
                    $score_r     = $item['score_read'];
                    $issue_count = $item['issue_count'];
                    $analyzed_at = $item['analyzed_at'];
                    $edit_url    = add_query_arg('amai_autooptimize', '1', get_edit_post_link($post->ID));
                    $s_class     = $score_s >= 70 ? 'good' : ($score_s >= 40 ? 'ok' : 'poor');
                    $r_class     = $score_r >= 70 ? 'good' : ($score_r >= 40 ? 'ok' : 'poor');
                    $analyzed_label = $analyzed_at > 0 ? 'Scored ' . gmdate('M j', $analyzed_at) : '';
                    // Build issue badge - show which score is the problem
                    $low_scores = [];
                    if ($score_s < 80) { $low_scores[] = 'Content Score: ' . $score_s; }
                    if ($score_r < 80) { $low_scores[] = 'Readability: ' . $score_r; }
                    $score_label = implode(', ', $low_scores);
                    ?>
                    <div class="amai-ch-row">
                        <div class="amai-ch-col amai-ch-col--title">
                            <div class="amai-ch-post-title"><?php echo esc_html($post->post_title); ?></div>
                            <div class="amai-ch-post-date">Updated <?php echo esc_html(gmdate('M j, Y', strtotime($post->post_modified_gmt))); ?><?php if ($analyzed_label): ?> &middot; <?php echo esc_html($analyzed_label); ?><?php endif; ?></div>
                        </div>
                        <div class="amai-ch-col amai-ch-col--score">
                            <div class="amai-ch-dial amai-ch-dial--<?php echo esc_attr($s_class); ?>"><?php echo $score_s; ?></div>
                        </div>
                        <div class="amai-ch-col amai-ch-col--score">
                            <div class="amai-ch-dial amai-ch-dial--<?php echo esc_attr($r_class); ?>"><?php echo $score_r; ?></div>
                        </div>
                        <div class="amai-ch-col amai-ch-col--issues">
                            <?php if ($issue_count > 0): ?>
                                <span class="amai-ch-badge amai-ch-badge--warning"><?php echo $issue_count; ?> issue<?php echo $issue_count !== 1 ? 's' : ''; ?></span>
                            <?php elseif ($score_label): ?>
                                <span class="amai-ch-badge amai-ch-badge--suggestion"><?php echo esc_html($score_label); ?></span>
                            <?php endif; ?>
                        </div>
                        <div class="amai-ch-col amai-ch-col--action">
                            <a href="<?php echo esc_url($edit_url); ?>" class="amai-ch-optimize-btn">Auto-Optimize</a>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>

    <?php // ── Getting Stale ────────────────────────────────── ?>
    <div class="amai-ch-section<?php echo $stale_count === 0 ? ' amai-ch-section--collapsed' : ''; ?>">
        <div class="amai-ch-section-header amai-ch-section-header--toggle" id="amai-ch-stale-header">
            <div>
                <div class="amai-ch-section-title<?php echo $stale_count === 0 ? ' amai-ch-section-title--muted' : ''; ?>">Getting Stale
                    <?php if ($stale_count > 0): ?>
                        <span class="amai-ch-section-count"><?php echo $stale_count; ?></span>
                    <?php endif; ?>
                </div>
                <div class="amai-ch-section-desc">Posts not updated in over 6 months. Fresh content ranks better.</div>
            </div>
            <?php if ($stale_count === 0): ?>
                <span class="amai-ch-section-all-good">&#10003; All up to date</span>
            <?php else: ?>
                <span class="amai-ch-section-chevron" id="amai-ch-stale-chevron">&#9650;</span>
            <?php endif; ?>
        </div>

        <?php if ($stale_count > 0): ?>
            <div id="amai-ch-stale-body">
                <div class="amai-ch-table">
                    <div class="amai-ch-table-head">
                        <div class="amai-ch-col amai-ch-col--title">Post</div>
                        <div class="amai-ch-col amai-ch-col--score">Content Score</div>
                        <div class="amai-ch-col amai-ch-col--score">Readability</div>
                        <div class="amai-ch-col amai-ch-col--stale">Last Updated</div>
                        <div class="amai-ch-col amai-ch-col--action"></div>
                    </div>
                    <?php foreach ($stale_posts as $item): ?>
                        <?php
                        $post        = $item['post'];
                        $score_s     = $item['score_seo'];
                        $score_r     = $item['score_read'];
                        $days_stale  = $item['days_stale'];
                        $analyzed_at = $item['analyzed_at'];
                        $edit_url    = get_edit_post_link($post->ID);
                        $s_class     = $score_s >= 70 ? 'good' : ($score_s >= 40 ? 'ok' : 'poor');
                        $r_class     = $score_r >= 70 ? 'good' : ($score_r >= 40 ? 'ok' : 'poor');
                        $analyzed_label = $analyzed_at > 0 ? 'Scored ' . gmdate('M j', $analyzed_at) : '';
                        ?>
                        <div class="amai-ch-row" data-post-id="<?php echo esc_attr((string) $post->ID); ?>">
                            <div class="amai-ch-col amai-ch-col--title">
                                <div class="amai-ch-post-title"><?php echo esc_html($post->post_title); ?></div>
                                <div class="amai-ch-post-date"><?php echo esc_html(gmdate('M j, Y', strtotime($post->post_modified_gmt))); ?><?php if ($analyzed_label): ?> &middot; <?php echo esc_html($analyzed_label); ?><?php endif; ?></div>
                            </div>
                            <div class="amai-ch-col amai-ch-col--score">
                                <div class="amai-ch-dial amai-ch-dial--<?php echo esc_attr($s_class); ?>"><?php echo $score_s ?: '--'; ?></div>
                            </div>
                            <div class="amai-ch-col amai-ch-col--score">
                                <div class="amai-ch-dial amai-ch-dial--<?php echo esc_attr($r_class); ?>"><?php echo $score_r ?: '--'; ?></div>
                            </div>
                            <div class="amai-ch-col amai-ch-col--stale">
                                <span class="amai-ch-stale-badge"><?php echo esc_html($days_stale); ?> days ago</span>
                            </div>
                            <div class="amai-ch-col amai-ch-col--action">
                                <button
                                    type="button"
                                    class="amai-ch-refresh-btn"
                                    data-post-id="<?php echo esc_attr((string) $post->ID); ?>"
                                >Refresh</button>
                                <a href="<?php echo esc_url($edit_url); ?>" class="amai-ch-edit-btn">Edit</a>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endif; ?>
    </div>
    <?php // -- Traffic Decay ----------------------------------------------------------------- ?>
    <div class="amai-ch-section">
        <div class="amai-ch-section-header">
            <div class="amai-ch-section-title">Traffic Decay
                <?php if ($ga4_connected && count($decaying_posts) > 0): ?>
                    <span class="amai-ch-section-count"><?php echo count($decaying_posts); ?></span>
                <?php endif; ?>
            </div>
            <div class="amai-ch-section-desc">Posts losing traffic over the last 30 days compared to the previous 30. A refresh can recover rankings.</div>
        </div>
        <?php if (!$ga4_connected): ?>
            <div class="amai-ch-empty">
                <div class="amai-ch-empty-icon">&#9432;</div>
                <div class="amai-ch-empty-text">Connect Google Analytics 4 on the <a href="<?php echo esc_url(admin_url('admin.php?page=automarketer-ai-modules')); ?>">Modules page</a> to see traffic decay alerts.</div>
            </div>
        <?php elseif (empty($decaying_posts)): ?>
            <div class="amai-ch-empty">
                <div class="amai-ch-empty-icon">&#10003;</div>
                <div class="amai-ch-empty-text">No traffic decay detected. Your posts are holding steady.</div>
            </div>
        <?php else: ?>
            <div class="amai-ch-table">
                <div class="amai-ch-table-head">
                    <div class="amai-ch-col amai-ch-col--title">Post</div>
                    <div class="amai-ch-col amai-ch-col--score">Sessions (30d)</div>
                    <div class="amai-ch-col amai-ch-col--score">Prev 30d</div>
                    <div class="amai-ch-col amai-ch-col--stale">Age</div>
                    <div class="amai-ch-col amai-ch-col--action"></div>
                </div>
                <?php foreach ($decaying_posts as $row): ?>
                    <?php
                    $curr     = (int) $row['sessions_30d'];
                    $prev     = (int) $row['sessions_prev_30d'];
                    $drop_pct = $prev > 0 ? (int) round((($prev - $curr) / $prev) * 100) : 0;
                    ?>
                    <div class="amai-ch-row">
                        <div class="amai-ch-col amai-ch-col--title">
                            <div class="amai-ch-post-title"><?php echo esc_html($row['post_title']); ?></div>
                            <div class="amai-ch-post-date">Last updated <?php echo esc_html($row['last_updated']); ?> days ago</div>
                        </div>
                        <div class="amai-ch-col amai-ch-col--score">
                            <div class="amai-ch-dial amai-ch-dial--poor"><?php echo $curr; ?></div>
                        </div>
                        <div class="amai-ch-col amai-ch-col--score">
                            <div class="amai-ch-dial amai-ch-dial--ok"><?php echo $prev; ?></div>
                        </div>
                        <div class="amai-ch-col amai-ch-col--stale">
                            <span class="amai-ch-stale-badge"><?php echo esc_html($row['days_old']); ?> days old</span>
                            <?php if ($drop_pct > 0): ?>
                                <span class="amai-ch-stale-badge" style="background:#fde8e8;color:#c0392b;">-<?php echo $drop_pct; ?>%</span>
                            <?php endif; ?>
                        </div>
                        <div class="amai-ch-col amai-ch-col--action">
                            <a href="<?php echo esc_url($row['edit_url']); ?>" class="amai-ch-refresh-btn">Refresh</a>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</div>