<?php

require __DIR__ . '/bootstrap.php';

use KeyLair\ClaimService;
use KeyLair\Helpers;
use KeyLair\RateLimiter;
use KeyLair\Config;

$token = $_GET['token'] ?? '';

if (!preg_match('/^[a-f0-9]{32}$/', $token)) {
    http_response_code(404);
    require __DIR__ . '/not_found.php';
    exit;
}

if (RateLimiter::tooManyRequests('claim_view', 60, 60)) {
    http_response_code(429);
    die('Too many requests. Please wait a moment and try again.');
}

$link = ClaimService::findLinkByToken($token);

if (!$link) {
    http_response_code(404);
    require __DIR__ . '/not_found.php';
    exit;
}

$products = ClaimService::productsForLink((int) $link['id']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Your Review Keys — <?= Helpers::e(\KeyLair\Settings::siteName()) ?></title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@500;600;700&family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@500;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="/assets/css/tokens.css">
<link rel="stylesheet" href="/assets/css/claim-overview.css">
<?= \KeyLair\Settings::faviconTag() ?>
</head>
<body>
<div class="page">
  <div class="brandbar"><?= \KeyLair\Settings::brandIcon() ?><?= Helpers::e(\KeyLair\Settings::siteName()) ?> · Review Access</div>

  <div class="intro">
    <h1>Your review keys</h1>
    <p class="subtitle">This link grants access to keys for the titles below. Pick a product to choose your platform and claim your key.</p>
  </div>

  <?php if (empty($products)): ?>
    <p class="empty-note">No products are currently available on this link.</p>
  <?php else: ?>
  <div class="product-list">
    <?php foreach ($products as $product):
        $done = $product['claims_used'] >= $product['max_claims'];
    ?>
    <a class="product-row" href="/claim/<?= Helpers::e($token) ?>/<?= Helpers::e($product['slug']) ?>">
      <div class="row-thumb">
        <?php if ($product['cover_image_path']): ?>
          <img src="/uploads/products/<?= Helpers::e($product['cover_image_path']) ?>" alt="">
        <?php endif; ?>
      </div>
      <div class="row-body">
        <p class="row-title"><?= Helpers::e($product['name']) ?></p>
        <span class="row-status<?= $done ? ' done' : '' ?>">
          <?= $done ? 'All keys claimed' : (int) $product['claims_used'] . ' / ' . (int) $product['max_claims'] . ' claimed' ?>
        </span>
      </div>
      <svg class="row-arrow" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M9 6l6 6-6 6"/></svg>
    </a>
    <?php endforeach; ?>
  </div>
  <?php endif; ?>

  <footer>
    Having trouble? Contact the person who sent you this link.<br>
    Powered by <a href="https://www.keylair.com" target="_blank" rel="noopener"><?= Helpers::e(\KeyLair\Settings::siteName()) ?></a><br>
    <a href="/imprint">Imprint</a> · <a href="/privacy">Privacy</a>
  </footer>
</div>
</body>
</html>
