<?php ob_start(); ?>

<?php
$name = $preview['name'] ?? $preview['slug'] ?? 'Package';
admin_content_header($name, 'shield-check', [
    ['url' => admin_url('marketplace/detail/' . ($preview['slug'] ?? '')), 'label' => __('admin.marketplace.back_to_details'), 'class' => 'btn-secondary'],
]);
?>

<div class="mp-preview">

    <!-- Security verification banner -->
    <?php if ($preview['signature_valid'] ?? false): ?>
    <div class="alert alert-success" style="display:flex; align-items:center; gap:.75rem; margin-bottom:1.5rem;">
        <?= icon('shield-check', 20) ?>
        <div>
            <strong><?= __('admin.marketplace.preview_verified') ?></strong>
            <span style="opacity:.7; font-size:.85rem; margin-left:.5rem;">
                <?= __('admin.marketplace.preview_key_id') ?>: <?= e($preview['signed_by_key_id'] ?? '') ?>
                <?php if ($preview['signed_at'] ?? ''): ?>
                &nbsp;·&nbsp; <?= e(date('Y-m-d', strtotime($preview['signed_at']))) ?>
                <?php endif; ?>
            </span>
        </div>
    </div>
    <?php elseif (!empty($preview['sha256'])): ?>
    <div class="alert alert-warning" style="display:flex; align-items:center; gap:.75rem; margin-bottom:1.5rem;">
        <?= icon('alert-triangle', 20) ?>
        <div>
            <strong><?= __('admin.marketplace.preview_unsigned') ?></strong>
            <span style="opacity:.7; font-size:.85rem; margin-left:.5rem;"><?= __('admin.marketplace.preview_unsigned_hint') ?></span>
        </div>
    </div>
    <?php endif; ?>

    <!-- CMS compatibility warning -->
    <?php if (!($preview['cms_compatible'] ?? true)): ?>
    <div class="alert alert-danger" style="margin-bottom:1.5rem;">
        <?= icon('x-circle', 18) ?>
        <?php foreach ($preview['cms_warnings'] ?? [] as $w): ?>
        <strong><?= e($w) ?></strong><br>
        <?php endforeach; ?>
        <?= __('admin.marketplace.preview_incompatible_note') ?>
    </div>
    <?php endif; ?>

    <div class="mp-preview__layout">

        <!-- Main column -->
        <div class="mp-preview__main">

            <!-- Package info -->
            <div class="card">
                <div class="card-body">
                    <div style="display:flex; gap:1.25rem; align-items:flex-start;">
                        <?php if (!empty($preview['thumbnail'])): ?>
                        <img src="<?= e($preview['thumbnail']) ?>" alt=""
                             style="width:80px; height:80px; object-fit:cover; border-radius:8px; flex-shrink:0;">
                        <?php else: ?>
                        <div class="mp-card__placeholder" style="width:80px; height:80px; border-radius:8px; flex-shrink:0;">
                            <?= icon(($preview['type'] ?? 'plugin') === 'theme' ? 'palette' : 'puzzle', 36) ?>
                        </div>
                        <?php endif; ?>
                        <div>
                            <h2 style="margin:0 0 .25rem;"><?= e($preview['name'] ?? '') ?></h2>
                            <div style="display:flex; gap:.5rem; flex-wrap:wrap; margin-bottom:.5rem;">
                                <span class="mp-badge mp-badge--<?= e($preview['type'] ?? 'plugin') ?>">
                                    <?= ($preview['type'] ?? 'plugin') === 'theme' ? __('admin.marketplace.type_theme') : __('admin.marketplace.type_plugin') ?>
                                </span>
                                <?php if ($preview['is_premium'] ?? false): ?>
                                <span class="mp-badge mp-badge--premium"><?= __('admin.marketplace.premium') ?></span>
                                <?php else: ?>
                                <span class="mp-badge mp-badge--free"><?= __('admin.marketplace.free') ?></span>
                                <?php endif; ?>
                            </div>
                            <?php if ($preview['description'] ?? ''): ?>
                            <p style="margin:0; color:var(--color-text-muted);"><?= e($preview['description']) ?></p>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>

            <!-- SHA256 + manifest details -->
            <?php if ($preview['sha256'] ?? ''): ?>
            <div class="card" style="margin-top:1.25rem;">
                <div class="card-body">
                    <h3 style="margin-top:0; font-size:.95rem;"><?= __('admin.marketplace.preview_integrity') ?></h3>
                    <table class="mp-detail__info">
                        <tr>
                            <th>SHA-256</th>
                            <td><code style="font-size:.78rem; word-break:break-all;"><?= e($preview['sha256']) ?></code></td>
                        </tr>
                        <tr>
                            <th><?= __('admin.marketplace.preview_size') ?></th>
                            <td><?= e(number_format(($preview['size_bytes'] ?? 0) / 1024, 1)) ?> KB</td>
                        </tr>
                        <tr>
                            <th><?= __('admin.marketplace.preview_sig_key') ?></th>
                            <td>
                                <?php if ($preview['signed_by_key_id'] ?? ''): ?>
                                <span class="<?= ($preview['signature_valid'] ?? false) ? 'text-success' : 'text-warning' ?>">
                                    <?= icon(($preview['signature_valid'] ?? false) ? 'check-circle' : 'alert-circle', 14) ?>
                                    <?= e($preview['signed_by_key_id']) ?>
                                </span>
                                <?php else: ?>
                                <span class="text-muted">&ndash;</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
            <?php endif; ?>

            <!-- Files affected -->
            <div class="card" style="margin-top:1.25rem;">
                <div class="card-body">
                    <h3 style="margin-top:0; font-size:.95rem;"><?= __('admin.marketplace.preview_files') ?></h3>
                    <code><?= e($preview['files_affected_note'] ?? '') ?></code>
                    <?php if ($preview['backup_will_be_created'] ?? false): ?>
                    <p style="margin:.75rem 0 0; font-size:.85rem; color:var(--color-text-muted);">
                        <?= icon('archive', 14) ?>
                        <?= __('admin.marketplace.preview_backup_note') ?>
                        <?php if (($preview['existing_backups'] ?? 0) > 0): ?>
                        (<?= (int) $preview['existing_backups'] ?> <?= __('admin.marketplace.preview_existing_backups') ?>)
                        <?php endif; ?>
                    </p>
                    <?php endif; ?>
                </div>
            </div>

        </div>

        <!-- Sidebar -->
        <div class="mp-preview__sidebar">
            <div class="card">
                <div class="card-body">
                    <table class="mp-detail__info">
                        <tr>
                            <th><?= __('admin.marketplace.version') ?></th>
                            <td>
                                <strong><?= e($preview['version_new'] ?? '') ?></strong>
                                <?php if ($preview['version_current'] ?? ''): ?>
                                <br><small class="text-muted"><?= __('admin.marketplace.preview_installed') ?>: <?= e($preview['version_current']) ?></small>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php if ($preview['author'] ?? ''): ?>
                        <tr>
                            <th><?= __('admin.marketplace.author') ?></th>
                            <td><?= e($preview['author']) ?></td>
                        </tr>
                        <?php endif; ?>
                        <?php if ($preview['min_cms_version'] ?? ''): ?>
                        <tr>
                            <th><?= __('admin.marketplace.min_cms') ?></th>
                            <td>
                                <?= e($preview['min_cms_version']) ?>
                                <?php if (!($preview['cms_compatible'] ?? true)): ?>
                                <span class="text-danger" style="font-size:.8rem;"><?= icon('x-circle', 12) ?></span>
                                <?php else: ?>
                                <span class="text-success" style="font-size:.8rem;"><?= icon('check-circle', 12) ?></span>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endif; ?>
                        <?php if ($preview['categories'] ?? []): ?>
                        <tr>
                            <th><?= __('admin.marketplace.categories') ?></th>
                            <td><?= e(implode(', ', (array) $preview['categories'])) ?></td>
                        </tr>
                        <?php endif; ?>
                    </table>
                </div>
            </div>

            <!-- Install / Update button -->
            <?php if ($preview['cms_compatible'] ?? true): ?>
            <div class="card" style="margin-top:1rem;">
                <div class="card-body">
                    <?php $isUpdate = $preview['is_update'] ?? false; ?>

                    <?php if ($preview['license_required'] ?? false): ?>
                    <div class="form-group" style="margin-bottom:1rem;">
                        <label for="preview_license_key"><?= __('admin.marketplace.license_key') ?></label>
                        <input type="text" id="preview_license_key" name="preview_license_key" class="form-control"
                               placeholder="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">
                        <small class="text-muted"><?= __('admin.marketplace.requires_license') ?></small>
                    </div>
                    <?php endif; ?>

                    <form method="POST"
                          action="<?= admin_url($isUpdate ? 'marketplace/update' : 'marketplace/install') ?>"
                          id="preview-install-form">
                        <?= csrf_field() ?>
                        <input type="hidden" name="slug" value="<?= e($preview['slug'] ?? '') ?>">
                        <input type="hidden" name="version" value="<?= e($preview['version_new'] ?? '') ?>">
                        <?php if ($preview['license_required'] ?? false): ?>
                        <input type="hidden" name="license_key" id="preview-license-passthrough" value="">
                        <?php endif; ?>

                        <button type="submit" class="btn <?= $isUpdate ? 'btn-warning' : 'btn-primary' ?>"
                                style="width:100%;" id="preview-confirm-btn">
                            <?= icon($isUpdate ? 'refresh-cw' : 'download-cloud', 16) ?>
                            <?= $isUpdate ? __('admin.marketplace.confirm_update_now') : __('admin.marketplace.confirm_install_now') ?>
                        </button>
                    </form>

                    <p style="margin:.75rem 0 0; font-size:.8rem; color:var(--color-text-muted); text-align:center;">
                        <?= __('admin.marketplace.preview_confirm_note') ?>
                    </p>
                </div>
            </div>
            <?php else: ?>
            <div class="card" style="margin-top:1rem;">
                <div class="card-body">
                    <p class="text-danger" style="margin:0;">
                        <?= icon('x-circle', 16) ?> <?= __('admin.marketplace.preview_blocked_incompatible') ?>
                    </p>
                </div>
            </div>
            <?php endif; ?>
        </div>

    </div>
</div>

<?php if ($preview['license_required'] ?? false): ?>
<script>
document.getElementById('preview-install-form').addEventListener('submit', function () {
    var key = document.getElementById('preview_license_key').value;
    document.getElementById('preview-license-passthrough').value = key;
});
</script>
<?php endif; ?>

<?php
$content = ob_get_clean();
echo $content;
