<?php
require_once __DIR__ . '/../includes/init.php';
require_once __DIR__ . '/../includes/plugins.php';
require_login();
$u = current_user();

$myLic = user_licenses((int)$u['id']);   // premium que compró (por slug)
$dl    = user_downloads((int)$u['id']);   // lo que descargó (slug:edition)

// Conjunto de plugins que el usuario tiene (comprados o descargados)
$slugs = array_keys($myLic);
foreach (array_keys($dl) as $k) { $slugs[] = explode(':', $k)[0]; }
$slugs = array_values(array_unique($slugs));

// ¿Cuántas actualizaciones hay en total?
$updates = 0;
foreach ($slugs as $s) {
    if (isset($myLic[$s]) && has_update($dl["$s:premium"] ?? null, plugin_latest($s, 'premium'))) $updates++;
    if (isset($dl["$s:free"]) && has_update($dl["$s:free"], plugin_latest($s, 'free'))) $updates++;
}

$page_title = 'Mis plugins';
require __DIR__ . '/../includes/header.php';
?>
<div class="player">
  <?php $active='builds'; require __DIR__ . '/../includes/panel_side.php'; ?>
  <main class="pcontent">
    <div class="phead"><div class="eyebrow" data-i18n="panelEyebrow">Mi biblioteca</div><h1 data-i18n="panelTitle">Mis plugins</h1><p data-i18n="panelIntro">Aquí están los plugins que compraste y descargaste. Descárgalos cuando quieras.</p></div>

    <?php if ($updates > 0): ?>
      <div class="alert alert-ok" style="display:flex;align-items:center;gap:10px">✨ <b><?= $updates ?> ✦</b> <span data-i18n="panelUpd">Descarga la última versión abajo.</span></div>
    <?php endif; ?>

    <?php if (!$slugs): ?>
      <div class="alert alert-info" data-i18n="panelEmpty">Aún no tienes plugins. Abre la wiki de un plugin y descárgalo o cómpralo.</div>
    <?php endif; ?>

    <?php foreach ($slugs as $slug): $p = plugin_get($slug); if (!$p) continue;
        $ownsPrem = isset($myLic[$slug]);
        $freeDl   = $dl["$slug:free"] ?? null;
        $premDl   = $dl["$slug:premium"] ?? null;
        $premLatest = plugin_latest($slug, 'premium');
        $freeLatest = plugin_latest($slug, 'free');
        $premUpd = $ownsPrem && has_update($premDl, $premLatest);
        $freeUpd = $freeDl && has_update($freeDl, $freeLatest);
    ?>
      <div class="build">
        <div class="bh">
          <span class="ico"><?= $p['icon'] ?></span>
          <div>
            <h3><?= e($p['name']) ?>
              <?php if ($ownsPrem): ?><?= plugin_badge($slug) ?><?php endif; ?>
              <?php if ($freeDl && !$ownsPrem): ?><span class="badge b-free">Free</span><?php endif; ?>
            </h3>
            <div class="meta"><a href="<?= e(BASE) ?>/<?= e($p['wiki']) ?>" style="color:var(--cyan)" data-i18n="panelWiki">Ver wiki / novedades →</a></div>
          </div>
        </div>

        <?php if ($ownsPrem && $premLatest): ?>
          <div class="brow">
            <span class="v"><?= e($premLatest['version']) ?></span>
            <span class="tag" data-i18n="tagPremium">Premium</span>
            <?php if ($premUpd): ?><span class="badge b-green">✨ <span data-i18n="tagUpdate">Actualización</span></span><?php endif; ?>
            <span class="bmeta"><?= e($premLatest['date']) ?> · <?= e($premLatest['size']) ?><?php if ($premDl && !$premUpd): ?> · <span data-i18n="tagHaveLatest">tienes la última</span><?php endif; ?></span>
            <span class="sp"><a class="btn primary sm" href="<?= e(BASE) ?>/panel/download.php?plugin=<?= e($slug) ?>&edition=premium&version=<?= e($premLatest['version']) ?>">⬇ <span data-i18n="btnDlPrem">Descargar Premium</span></a></span>
          </div>
        <?php endif; ?>

        <?php if ($freeDl && $freeLatest): ?>
          <div class="brow">
            <span class="v"><?= e($freeLatest['version']) ?></span>
            <span class="tag" style="background:rgba(52,210,242,.15);color:var(--cyan)" data-i18n="tagFree">Free</span>
            <?php if ($freeUpd): ?><span class="badge b-green">✨ <span data-i18n="tagUpdate">Actualización</span></span><?php endif; ?>
            <span class="bmeta"><?= e($freeLatest['date']) ?> · <?= e($freeLatest['size']) ?><?php if (!$freeUpd): ?> · <span data-i18n="tagHaveLatest">tienes la última</span><?php endif; ?></span>
            <span class="sp"><a class="btn primary sm" href="<?= e(BASE) ?>/panel/download.php?plugin=<?= e($slug) ?>&edition=free&version=<?= e($freeLatest['version']) ?>">⬇ <span data-i18n="btnDlFree">Descargar Free</span></a></span>
          </div>
        <?php endif; ?>
      </div>
    <?php endforeach; ?>
  </main>
</div>
<?php require __DIR__ . '/../includes/footer.php'; ?>
