<?php

require __DIR__ . '/../bootstrap.php';

use KeyLair\Auth;
use KeyLair\Database;
use KeyLair\Helpers;
use KeyLair\I18n;
use KeyLair\Platform;

$admin = Auth::requireLogin();
$isDemo = $admin['role'] === 'demo';
$pdo = Database::connection();

$rows = $pdo->query(
    "SELECT p.id AS product_id, p.name AS product_name, k.platform,
        COUNT(*) AS total,
        SUM(CASE WHEN k.status = 'available' AND k.pool = 'active' THEN 1 ELSE 0 END) AS available_active,
        SUM(CASE WHEN k.status = 'available' AND k.pool = 'inactive' THEN 1 ELSE 0 END) AS available_inactive
     FROM `keys` k
     JOIN products p ON p.id = k.product_id
     GROUP BY p.id, k.platform
     ORDER BY p.name ASC, k.platform ASC"
)->fetchAll();

$byProduct = [];
foreach ($rows as $r) {
    $byProduct[$r['product_id']]['name'] = $r['product_name'];
    $byProduct[$r['product_id']]['platforms'][$r['platform']] = [
        'total' => (int) $r['total'],
        'available_active' => (int) $r['available_active'],
        'available_inactive' => (int) $r['available_inactive'],
    ];
}

$pageTitle = I18n::t('keys.page_title');
$activeNav = 'keys';
require __DIR__ . '/includes/layout_header.php';
?>
<div class="topbar">
  <h1><?= Helpers::e(I18n::t('keys.page_title')) ?></h1>
  <?php if (Auth::canEdit()): ?>
    <div class="toolbar-2col-row" style="display:flex; gap:10px; flex-wrap:wrap;">
      <a href="keys_export_form.php" class="campaign-select" style="text-decoration:none; color:var(--text-muted);"><?= Helpers::e(I18n::t('keys.export_keys')) ?></a>
      <a href="keys_import.php" class="campaign-select" style="text-decoration:none; color:var(--signal); border-color:var(--signal-dim);"><?= Helpers::e(I18n::t('keys.import_keys')) ?></a>
    </div>
  <?php elseif ($isDemo): ?>
    <div class="toolbar-2col-row" style="display:flex; gap:10px; flex-wrap:wrap;">
      <span class="campaign-select" style="text-decoration:none; color:var(--text-faint); border-color:var(--signal-dim); cursor:not-allowed;"><?= Helpers::e(I18n::t('keys.export_keys')) ?></span>
      <span class="campaign-select" style="text-decoration:none; color:var(--text-faint); border-color:var(--signal-dim); cursor:not-allowed;"><?= Helpers::e(I18n::t('keys.import_keys')) ?></span>
    </div>
  <?php endif; ?>
</div>

<div class="panel">
  <?php if (empty($byProduct)): ?>
    <p style="color:var(--text-faint); font-size:13px;"><?= Helpers::e(I18n::t('keys.no_keys_yet')) ?></p>
  <?php else: ?>
  <table>
    <thead><tr><th><?= Helpers::e(I18n::t('keys.col_platform')) ?></th><th><?= Helpers::e(I18n::t('keys.col_released')) ?></th><th><?= Helpers::e(I18n::t('keys.col_held_back')) ?></th><th><?= Helpers::e(I18n::t('keys.col_total')) ?></th><th></th></tr></thead>
    <tbody>
      <?php $firstProduct = array_key_first($byProduct); ?>
      <?php foreach ($byProduct as $productId => $data): ?>
        <tr>
          <td colspan="5" style="font-weight:600; font-size:13px; color:var(--text-primary); padding-left:9px; <?= $productId === $firstProduct ? 'padding-top:14px;' : 'padding-top:18px; border-top:1px solid var(--line);' ?>"><?= Helpers::e($data['name']) ?></td>
        </tr>
        <?php foreach ($data['platforms'] as $platform => $counts): ?>
        <tr>
          <td data-label="<?= Helpers::e(I18n::t('keys.col_platform')) ?>"><span class="plat-tag"><?= Helpers::e(Platform::label($platform)) ?></span></td>
          <td class="time-cell" data-label="<?= Helpers::e(I18n::t('keys.data_label_active')) ?>" style="color:<?= $counts['available_active'] === 0 ? 'var(--danger)' : 'var(--signal)' ?>;"><?= $counts['available_active'] ?></td>
          <td class="time-cell" data-label="<?= Helpers::e(I18n::t('keys.data_label_inactive')) ?>" style="color:var(--key-gold);"><?= $counts['available_inactive'] ?></td>
          <td class="time-cell" data-label="<?= Helpers::e(I18n::t('keys.col_total')) ?>"><?= $counts['total'] ?></td>
          <td><a href="keys_pool.php?product_id=<?= (int) $productId ?>&platform=<?= Helpers::e($platform) ?>" style="color:var(--signal); font-size:12px; text-decoration:none;"><?= Helpers::e(I18n::t('keys.open_link')) ?></a></td>
        </tr>
        <?php endforeach; ?>
      <?php endforeach; ?>
    </tbody>
  </table>
  <?php endif; ?>
</div>

<?php require __DIR__ . '/includes/layout_footer.php'; ?>
