<?php
declare(strict_types=1);

if (!function_exists('exec_ui_kpi_card')) {
    function exec_ui_kpi_card(array $card): void
    {
        $tone = (string)($card['tone'] ?? 'neutral');
        $icon = (string)($card['icon'] ?? '◆');
        $label = (string)($card['label'] ?? '');
        $value = (string)($card['value'] ?? '—');
        $comparison = (string)($card['comparison'] ?? '');
        $metric = (string)($card['metric'] ?? '');
        ?>
        <article class="exec-kpi-card exec-tone-<?= e($tone) ?>">
            <span class="exec-kpi-icon" aria-hidden="true"><?= e($icon) ?></span>
            <div class="exec-kpi-copy">
                <span class="exec-kpi-label"><?= e($label) ?></span>
                <strong class="exec-kpi-value"><?= e($value) ?></strong>
                <small class="exec-kpi-comparison"><?= e($comparison) ?></small>
            </div>
            <?php if ($metric !== ''): ?><span class="exec-kpi-metric"><?= e($metric) ?></span><?php endif; ?>
        </article>
        <?php
    }
}

if (!function_exists('exec_ui_section_heading')) {
    function exec_ui_section_heading(string $eyebrow, string $title, string $description = ''): void
    {
        ?>
        <div class="exec-section-heading">
            <div>
                <span><?= e($eyebrow) ?></span>
                <h2><?= e($title) ?></h2>
                <?php if ($description !== ''): ?><p><?= e($description) ?></p><?php endif; ?>
            </div>
        </div>
        <?php
    }
}

if (!function_exists('exec_ui_chart_card')) {
    function exec_ui_chart_card(array $chart): void
    {
        $key = (string)$chart['key'];
        ?>
        <article class="exec-surface exec-chart-card" data-exec-chart-card="<?= e($key) ?>" data-chart-state="pending">
            <header>
                <div>
                    <span class="exec-overline"><?= e((string)$chart['eyebrow']) ?></span>
                    <h3><?= e((string)$chart['title']) ?></h3>
                    <small><?= e((string)$chart['subtitle']) ?></small>
                </div>
                <span class="exec-chart-unit"><?= e((string)$chart['unit']) ?></span>
            </header>
            <div class="exec-chart-stage">
                <canvas id="exec_chart_<?= e($key) ?>" width="760" height="440"></canvas>
                <div class="exec-chart-state exec-chart-empty"><?= e((string)$chart['empty']) ?></div>
                <div class="exec-chart-state exec-chart-error"><?= e((string)$chart['error']) ?></div>
            </div>
            <footer>
                <span><?= e((string)$chart['total_label']) ?></span>
                <strong><?= e((string)$chart['total_value']) ?></strong>
            </footer>
        </article>
        <?php
    }
}

if (!function_exists('exec_ui_page_intro')) {
    /**
     * Shared compact executive page introduction.
     *
     * Contract:
     * - low visual density
     * - executive clarity in 3–5 seconds
     * - no page-specific CSS dependency
     * - reusable across all dashboard domains
     */
    function exec_ui_page_intro(array $intro): void
    {
        $eyebrow = (string)($intro['eyebrow'] ?? '');
        $title = (string)($intro['title'] ?? '');
        $description = (string)($intro['description'] ?? '');
        $contextLabel = (string)($intro['context_label'] ?? '');
        $contextValue = (string)($intro['context_value'] ?? '');
        $contextNote = (string)($intro['context_note'] ?? '');
        $primaryLabel = (string)($intro['primary_label'] ?? '');
        $primaryHref = (string)($intro['primary_href'] ?? '#');
        $secondaryLabel = (string)($intro['secondary_label'] ?? '');
        $statusLabel = (string)($intro['status_label'] ?? '');
        ?>
        <section class="exec-page-intro" data-exec-shared-component="page-intro">
            <div class="exec-page-intro__copy">
                <?php if ($eyebrow !== ''): ?>
                    <span class="exec-overline"><?= e($eyebrow) ?></span>
                <?php endif; ?>
                <h2><?= e($title) ?></h2>
                <?php if ($description !== ''): ?>
                    <p><?= e($description) ?></p>
                <?php endif; ?>
                <div class="exec-page-intro__actions">
                    <?php if ($primaryLabel !== ''): ?>
                        <a class="exec-primary-link" href="<?= e($primaryHref) ?>"><?= e($primaryLabel) ?></a>
                    <?php endif; ?>
                    <?php if ($secondaryLabel !== ''): ?>
                        <button type="button" class="exec-secondary-button" data-branch-open><?= e($secondaryLabel) ?></button>
                    <?php endif; ?>
                </div>
            </div>

            <aside class="exec-page-context">
                <?php if ($statusLabel !== ''): ?>
                    <span class="exec-status-pill exec-status-pill--positive"><?= e($statusLabel) ?></span>
                <?php endif; ?>
                <?php if ($contextLabel !== ''): ?>
                    <small><?= e($contextLabel) ?></small>
                <?php endif; ?>
                <strong><?= e($contextValue) ?></strong>
                <?php if ($contextNote !== ''): ?>
                    <p><?= e($contextNote) ?></p>
                <?php endif; ?>
            </aside>
        </section>
        <?php
    }
}

if (!function_exists('exec_ui_empty_state')) {
    /**
     * Shared compact no-data / unavailable state.
     */
    function exec_ui_empty_state(array $state): void
    {
        $title = (string)($state['title'] ?? '');
        $text = (string)($state['text'] ?? '');
        $tone = (string)($state['tone'] ?? 'neutral');
        ?>
        <div class="exec-empty-state exec-empty-state--<?= e($tone) ?>" data-exec-shared-component="empty-state">
            <span aria-hidden="true">◇</span>
            <div>
                <?php if ($title !== ''): ?><strong><?= e($title) ?></strong><?php endif; ?>
                <?php if ($text !== ''): ?><p><?= e($text) ?></p><?php endif; ?>
            </div>
        </div>
        <?php
    }
}

if (!function_exists('exec_ui_section_label')) {
    /**
     * Shared section label for consistent information hierarchy.
     */
    function exec_ui_section_label(string $eyebrow, string $title, string $description = ''): void
    {
        ?>
        <div class="exec-section-label" data-exec-shared-component="section-label">
            <span class="exec-overline"><?= e($eyebrow) ?></span>
            <h2><?= e($title) ?></h2>
            <?php if ($description !== ''): ?><p><?= e($description) ?></p><?php endif; ?>
        </div>
        <?php
    }
}

