<?php
/**
 * Combined Updates Widget – Core + installed Plugins + installed Themes.
 * Only rendered when at least one update category has results.
 *
 * Expected: $updates = ['core' => [...], 'plugins' => [...], 'themes' => [...]]
 */

if (!isset($updates)) {
    return;
}

$coreAvailable    = !empty($updates['core']['available']);
$pluginUpdates    = $updates['plugins'] ?? [];
$themeUpdates     = $updates['themes']  ?? [];
$hasAny           = $coreAvailable || !empty($pluginUpdates) || !empty($themeUpdates);

if (!$hasAny) {
    return;
}

$coreData = $updates['core']['data'] ?? [];

$coreSize   = !empty($coreData['size_bytes']) ? round($coreData['size_bytes'] / 1024 / 1024, 1) . ' MB' : '';
$isSecurity = !empty($coreData['security_release']);
$isSigned   = !empty($coreData['signature_b64']);
$totalUpdates = ($coreAvailable ? 1 : 0) + count($pluginUpdates) + count($themeUpdates);
?>

<style>
.iw-upd {
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    margin-bottom: 1.5rem;
    overflow: hidden;
    box-shadow: 0 1px 2px rgba(15,23,42,.04), 0 1px 3px rgba(15,23,42,.06);
}
.iw-upd__head {
    display: flex; align-items: center; justify-content: space-between;
    padding: 1rem 1.25rem;
    background: #fbfcfe;
    border-bottom: 1px solid #e5e7eb;
}
.iw-upd__title {
    margin: 0;
    display: flex; align-items: center; gap: .6rem;
    font-size: 1rem; font-weight: 600; color: #0f172a;
}
.iw-upd__title svg { color: #2563eb; }
.iw-upd__count {
    display: inline-flex; align-items: center; justify-content: center;
    min-width: 22px; height: 22px; padding: 0 .5rem;
    background: #2563eb; color: #fff;
    font-size: .75rem; font-weight: 700;
    border-radius: 999px;
    margin-left: .25rem;
}
.iw-upd__row {
    display: flex; align-items: flex-start; gap: 1rem;
    padding: 1.1rem 1.25rem;
    border-bottom: 1px solid #f1f5f9;
}
.iw-upd__row:last-child { border-bottom: none; }
.iw-upd__icon {
    flex: 0 0 44px; height: 44px;
    border-radius: 10px;
    display: flex; align-items: center; justify-content: center;
    background: #eff6ff;
    color: #2563eb;
}
.iw-upd__icon--muted { background: #f1f5f9; color: #475569; }
.iw-upd__body { flex: 1; min-width: 0; }
.iw-upd__name {
    display: flex; align-items: baseline; gap: .6rem;
    flex-wrap: wrap;
    margin-bottom: .4rem;
}
.iw-upd__name strong { font-size: .9375rem; color: #0f172a; font-weight: 600; }
.iw-upd__ver {
    font-size: .8125rem; color: #64748b;
    font-variant-numeric: tabular-nums;
    display: inline-flex; align-items: center; gap: .25rem;
}
.iw-upd__ver-to { color: #2563eb; font-weight: 700; }
.iw-upd__badges { display: flex; gap: .35rem; flex-wrap: wrap; margin-top: .1rem; }
.iw-upd__badge {
    display: inline-flex; align-items: center; gap: .25rem;
    padding: .2rem .5rem;
    font-size: .7rem; font-weight: 600;
    border-radius: 4px;
    line-height: 1;
}
.iw-upd__badge svg { flex-shrink: 0; }
.iw-upd__badge--ok     { background: #dcfce7; color: #14532d; }
.iw-upd__badge--alert  { background: #fee2e2; color: #991b1b; }
.iw-upd__badge--meta   { background: #f1f5f9; color: #475569; }
.iw-upd__action { flex-shrink: 0; align-self: center; }
.iw-upd__btn {
    display: inline-flex; align-items: center; gap: .4rem;
    padding: .55rem 1.1rem;
    background: #2563eb;
    color: #ffffff !important;
    border: 1px solid #2563eb;
    font-size: .875rem; font-weight: 600;
    line-height: 1;
    border-radius: 8px;
    text-decoration: none;
    transition: background .15s, transform .15s;
}
.iw-upd__btn:hover { background: #1d4ed8; border-color: #1d4ed8; color: #fff !important; transform: translateY(-1px); }
.iw-upd__btn svg { color: #fff !important; }
.iw-upd__btn--ghost {
    background: #fff;
    color: #1e293b !important;
    border: 1px solid #e2e8f0;
}
.iw-upd__btn--ghost:hover { background: #f8fafc; color: #1e293b !important; border-color: #cbd5e1; transform: none; }
.iw-upd__btn--ghost svg { color: #475569 !important; }
@media (max-width: 640px) {
    .iw-upd__row { flex-wrap: wrap; }
    .iw-upd__action { width: 100%; margin-top: .5rem; }
    .iw-upd__btn { width: 100%; justify-content: center; }
}
</style>

<div class="iw-upd">
    <div class="iw-upd__head">
        <h3 class="iw-upd__title">
            <?= render_icon('refresh-cw', 18) ?>
            <?= __('admin.system_updates.widget_title') ?>
            <span class="iw-upd__count"><?= $totalUpdates ?></span>
        </h3>
    </div>

    <?php if ($coreAvailable): ?>
    <div class="iw-upd__row">
        <div class="iw-upd__icon"><?= render_icon('cpu', 22) ?></div>
        <div class="iw-upd__body">
            <div class="iw-upd__name">
                <strong>ITSWEBER CMS</strong>
                <span class="iw-upd__ver">
                    v<?= e(function_exists('cms_version') ? cms_version() : '') ?>
                    <?= render_icon('arrow-right', 12) ?>
                    <span class="iw-upd__ver-to">v<?= e($updates['core']['version']) ?></span>
                </span>
            </div>
            <div class="iw-upd__badges">
                <?php if ($isSecurity): ?>
                <span class="iw-upd__badge iw-upd__badge--alert"><?= render_icon('shield-alert', 11) ?> Sicherheits-Update</span>
                <?php endif; ?>
                <?php if ($isSigned): ?>
                <span class="iw-upd__badge iw-upd__badge--ok"><?= render_icon('shield-check', 11) ?> Signiert &amp; verifiziert</span>
                <?php endif; ?>
                <?php if ($coreSize !== ''): ?>
                <span class="iw-upd__badge iw-upd__badge--meta"><?= render_icon('package', 11) ?> <?= e($coreSize) ?></span>
                <?php endif; ?>
            </div>
        </div>
        <div class="iw-upd__action">
            <a href="<?= admin_url('system/updates') ?>" class="iw-upd__btn">
                <?= render_icon('download', 15) ?>
                Aktualisieren
            </a>
        </div>
    </div>
    <?php endif; ?>

    <?php foreach ($pluginUpdates as $pkg): ?>
    <div class="iw-upd__row">
        <div class="iw-upd__icon iw-upd__icon--muted"><?= render_icon('puzzle', 22) ?></div>
        <div class="iw-upd__body">
            <div class="iw-upd__name">
                <strong><?= e($pkg['name'] ?? $pkg['slug'] ?? '') ?></strong>
                <span class="iw-upd__ver">
                    v<?= e($pkg['version_installed'] ?? '') ?>
                    <?= render_icon('arrow-right', 12) ?>
                    <span class="iw-upd__ver-to">v<?= e($pkg['version_latest'] ?? '') ?></span>
                </span>
            </div>
            <div class="iw-upd__badges">
                <span class="iw-upd__badge iw-upd__badge--meta">Plugin</span>
            </div>
        </div>
        <div class="iw-upd__action">
            <a href="<?= admin_url('marketplace/detail/' . e($pkg['slug'] ?? '')) ?>" class="iw-upd__btn iw-upd__btn--ghost">Details</a>
        </div>
    </div>
    <?php endforeach; ?>

    <?php foreach ($themeUpdates as $pkg): ?>
    <div class="iw-upd__row">
        <div class="iw-upd__icon iw-upd__icon--muted"><?= render_icon('layers', 22) ?></div>
        <div class="iw-upd__body">
            <div class="iw-upd__name">
                <strong><?= e($pkg['name'] ?? $pkg['slug'] ?? '') ?></strong>
                <span class="iw-upd__ver">
                    v<?= e($pkg['version_installed'] ?? '') ?>
                    <?= render_icon('arrow-right', 12) ?>
                    <span class="iw-upd__ver-to">v<?= e($pkg['version_latest'] ?? '') ?></span>
                </span>
            </div>
            <div class="iw-upd__badges">
                <span class="iw-upd__badge iw-upd__badge--meta">Theme</span>
            </div>
        </div>
        <div class="iw-upd__action">
            <a href="<?= admin_url('marketplace/detail/' . e($pkg['slug'] ?? '')) ?>" class="iw-upd__btn iw-upd__btn--ghost">Details</a>
        </div>
    </div>
    <?php endforeach; ?>
</div>
