<?php
/**
 * Shared Page Header Component
 *
 * Provides consistent header across all admin pages.
 *
 * Expected variables in scope:
 * - $consolety_ctx           (PageContext) - from PageContext::get()
 * - $consolety_page_icon     (string) - dashicons class name
 * - $consolety_page_title    (string) - page title
 * - $consolety_page_sub      (string) - subtitle text
 * - $consolety_show_credits  (bool, optional) - show credits pill, default true
 * - $consolety_show_tier     (bool, optional) - show tier badge, default true
 * - $consolety_header_actions (string, optional) - extra HTML for action buttons
 *
 * @package Consolety
 */

if (!defined('ABSPATH')) {
    exit;
}

// Backward/forward compatible variable contract: accept both the consolety_
// prefixed variables (develop) and the legacy unprefixed ones (5.0.3 production
// views) so every admin page renders a correct header regardless of which view
// convention is deployed. Safe in both worlds; remove the legacy fallbacks once
// all views ship prefixed network-wide.
$consolety_ctx = $consolety_ctx ?? ($ctx ?? null);
$consolety_page_icon = $consolety_page_icon ?? ($page_icon ?? '');
$consolety_page_title = $consolety_page_title ?? ($page_title ?? '');
$consolety_page_sub = $consolety_page_sub ?? ($page_sub ?? '');
$consolety_show_credits = $consolety_show_credits ?? ($show_credits ?? true);
$consolety_show_tier = $consolety_show_tier ?? ($show_tier ?? true);
$consolety_header_actions = $consolety_header_actions ?? ($header_actions ?? '');
?>
<div class="vp-header">
    <div>
        <h1>
            <span class="dashicons <?php echo esc_attr($consolety_page_icon); ?> vp-card-icon"></span>
            <?php echo esc_html($consolety_page_title); ?>
        </h1>
        <p class="vp-header-sub"><?php echo esc_html($consolety_page_sub); ?></p>
    </div>

    <?php if ($consolety_ctx->isConnected && ($consolety_show_credits || $consolety_show_tier)): ?>
    <div class="vp-credits-display">
        <?php if ($consolety_show_credits): ?>
            <span class="vp-credits-value"><?php echo esc_html($consolety_ctx->credits); ?></span>
            <span class="vp-credits-label"><?php esc_html_e('points', 'consolety'); ?></span>
        <?php endif; ?>
        <?php if ($consolety_show_tier): ?>
            <span class="vp-tier-badge vp-tier-<?php echo esc_attr($consolety_ctx->tierSlug); ?>">
                <?php echo esc_html($consolety_ctx->tierLabel); ?>
            </span>
        <?php endif; ?>
        <a href="<?php echo esc_url(admin_url('admin.php?page=consolety-account')); ?>" class="vp-account-link" title="<?php esc_attr_e('Manage subscription', 'consolety'); ?>">
            <span class="dashicons dashicons-admin-users"></span>
        </a>
        <?php echo $consolety_header_actions; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- pre-escaped HTML ?>
    </div>
    <?php endif; ?>
</div>
