<?php
$stats = $templateLibraryStats ?? ['total' => count($templates ?? []), 'email' => 0, 'sms' => 0, 'theme' => 0];
?>
<section class="workspace-hero">
    <div>
        <h2>Template Library</h2>
        <p>Manage your saved email themes, SMS/MMS quick templates, and reusable layouts in one dedicated workspace. Reopen any item in Template Creator when you need to revise it.</p>
    </div>
    <div class="workspace-actions">
        <a class="btn primary small" href="index.php?route=templates/index">Create Template</a>
        <a class="btn secondary small" href="index.php?route=messages/campaignStudio">Campaign Studio</a>
    </div>
</section>

<section class="theme-library-shell">
    <div class="card theme-library-overview-card">
        <div class="toolbar">
            <h2>Saved Template Library</h2>
            <span class="badge"><?= (int)$stats['total'] ?> total</span>
        </div>
        <div class="theme-library-overview-grid mt-12">
            <div class="theme-pro-stat-pill"><span class="small muted">Email templates</span><strong><?= (int)$stats['email'] ?></strong></div>
            <div class="theme-pro-stat-pill"><span class="small muted">SMS/MMS templates</span><strong><?= (int)$stats['sms'] ?></strong></div>
            <div class="theme-pro-stat-pill"><span class="small muted">Theme Creator layouts</span><strong><?= (int)$stats['theme'] ?></strong></div>
            <div class="theme-pro-stat-pill"><span class="small muted">Reusable starts</span><strong><?= (int)$stats['total'] ?></strong></div>
        </div>
        <div class="theme-library-overview-note mt-12 small muted">Open any template to edit it in the advanced builder, reuse it in Campaign Studio, or keep it available for one-to-one Mail Center replies and sends.</div>
    </div>

    <div class="card theme-library-card theme-v3-library-card">
        <div class="toolbar">
            <h2>All templates</h2>
            <span class="badge">Reusable content</span>
        </div>
        <div class="theme-library-list mt-12">
            <?php foreach ($templates as $template): ?>
                <?php $excerpt = trim(preg_replace('/\s+/', ' ', strip_tags((string)(($template['type'] ?? '') === 'email' ? ($template['theme_html'] ?? $template['body'] ?? '') : ($template['body'] ?? ''))))); ?>
                <article class="theme-library-item theme-v3-library-item theme-library-item--pro">
                    <div class="theme-library-head">
                        <div>
                            <h3><?= e((string)$template['name']) ?></h3>
                            <div class="theme-library-meta">
                                <span class="mail-chip color-slate"><?= e(strtoupper((string)$template['type'])) ?></span>
                                <?php if (!empty($template['is_theme_template'])): ?><span class="mail-chip color-blue">Theme Creator</span><?php endif; ?>
                                <?php if (!empty($template['theme_preset'])): ?><span class="mail-chip color-violet"><?= e(ucfirst((string)$template['theme_preset'])) ?></span><?php endif; ?>
                            </div>
                        </div>
                        <div class="small muted">Updated <?= e(date('M j, Y g:i a', strtotime((string)$template['updated_at']))) ?></div>
                    </div>
                    <?php if (!empty($template['subject'])): ?><div class="small"><strong>Subject:</strong> <?= e((string)$template['subject']) ?></div><?php endif; ?>
                    <p class="small muted"><?= e($excerpt !== '' ? ((strlen($excerpt) > 200) ? (substr($excerpt, 0, 197) . '…') : $excerpt) : 'Saved template ready for reuse.') ?></p>
                    <div class="actions-cell">
                        <a class="btn secondary small" href="index.php?route=templates/index&edit=<?= (int)$template['id'] ?>">Edit in Creator</a>
                        <a class="btn danger small" href="index.php?route=templates/delete&id=<?= (int)$template['id'] ?>" onclick="return confirm('Delete template?')">Delete</a>
                    </div>
                </article>
            <?php endforeach; ?>
            <?php if (empty($templates)): ?>
                <div class="muted">No templates saved yet.</div>
            <?php endif; ?>
        </div>
    </div>
</section>
