<?php
declare(strict_types=1);

$pageId = 'trails';
$pageTitle = 'Bully Room Trails — Pet Stories Around the World';
$pageDescription = 'Watch public Bully Room Trails: quick pet stories, market drops, new pets, Tails, rescue updates, and community highlights.';
$pageCanonical = 'https://bullyroom.online/trails';
$ogTitle = 'Bully Room Trails — Pet Stories Around the World';
$ogDescription = $pageDescription;
$ogImage = 'https://bullyroom.online/images/pets-meet.webp';
$twitterTitle = $ogTitle;
$twitterDescription = $ogDescription;
$twitterImage = $ogImage;

$PUBLIC_STORIES_ENDPOINT = 'https://us-east1-bullyroom2.cloudfunctions.net/publicStoriesFeed';
$trails = [];

function br_trails_escape(string $value): string {
  return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}


function br_trails_trim(string $value, int $limit): string {
  $clean = trim($value);
  if (function_exists('mb_strimwidth')) {
    return mb_strimwidth($clean, 0, $limit, '…', 'UTF-8');
  }
  return strlen($clean) > $limit ? substr($clean, 0, max(0, $limit - 3)) . '...' : $clean;
}

function br_trails_fetch_json(string $endpoint, array $params): ?array {
  $url = $endpoint . '?' . http_build_query($params);

  if (function_exists('curl_init')) {
    $ch = curl_init($url);
    curl_setopt_array($ch, [
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_TIMEOUT => 8,
      CURLOPT_CONNECTTIMEOUT => 3,
      CURLOPT_SSL_VERIFYPEER => true,
      CURLOPT_HTTPHEADER => [
        'Accept: application/json',
        'User-Agent: BullyRoom-Trails-Globe/1.0',
      ],
    ]);
    $body = curl_exec($ch);
    $http = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($body === false || $http < 200 || $http >= 400) return null;
  } else {
    $context = stream_context_create([
      'http' => [
        'method' => 'GET',
        'timeout' => 8,
        'header' => "Accept: application/json\r\nUser-Agent: BullyRoom-Trails-Globe/1.0\r\n",
      ],
    ]);
    $body = @file_get_contents($url, false, $context);
    if ($body === false) return null;
  }

  $json = json_decode($body, true);
  return is_array($json) ? $json : null;
}

$data = br_trails_fetch_json($PUBLIC_STORIES_ENDPOINT, ['limit' => 18]);
if (is_array($data) && !empty($data['ok']) && is_array($data['items'] ?? null)) {
  $trails = array_slice($data['items'], 0, 18);
}

require_once __DIR__ . '/../includes/br_layout_top.php';
?>
<style>
  .trailsHero{display:grid;gap:18px;text-align:center;padding:28px 0 18px}
  .trailsHero h1{max-width:920px;margin:0 auto;font-size:clamp(38px,7vw,78px);line-height:.95;letter-spacing:-.06em}
  .trailsHero p{max-width:760px;margin:0 auto;color:var(--muted);font-size:18px;line-height:1.7}
  .trailGlobeWrap{position:relative;min-height:620px;margin:28px auto 10px;display:grid;place-items:center;overflow:hidden;border-radius:34px;border:1px solid var(--border);background:radial-gradient(circle at 50% 45%,rgba(37,99,235,.22),transparent 28%),radial-gradient(circle at 20% 20%,rgba(220,31,255,.18),transparent 30%),radial-gradient(circle at 80% 70%,rgba(0,255,163,.13),transparent 28%),rgba(255,255,255,.035);box-shadow:var(--shadow2)}
  .trailGlobe{position:absolute;width:min(62vw,520px);height:min(62vw,520px);border-radius:50%;background:radial-gradient(circle at 35% 28%,rgba(255,255,255,.30),rgba(6,182,212,.18) 22%,rgba(37,99,235,.12) 48%,rgba(0,0,0,.55) 74%),linear-gradient(140deg,rgba(0,255,163,.18),rgba(37,99,235,.04),rgba(220,31,255,.14));border:1px solid rgba(255,255,255,.18);box-shadow:inset -35px -45px 80px rgba(0,0,0,.55),0 0 70px rgba(37,99,235,.25),0 0 120px rgba(220,31,255,.12)}
  .trailGlobe:before{content:"";position:absolute;inset:9%;border-radius:50%;background:repeating-linear-gradient(0deg,rgba(255,255,255,.08) 0 1px,transparent 1px 38px),repeating-linear-gradient(90deg,rgba(255,255,255,.055) 0 1px,transparent 1px 46px);mask-image:radial-gradient(circle,#000 64%,transparent 68%)}
  .trailGlobe:after{content:"";position:absolute;inset:16%;border-radius:52% 48% 56% 44%;background:linear-gradient(135deg,rgba(0,255,163,.24),rgba(255,255,255,.05),rgba(255,122,0,.20));filter:blur(.3px);opacity:.55;transform:rotate(-18deg)}
  .trailOrbit{position:relative;width:min(92vw,1040px);height:580px;z-index:2}
  .trailTile{position:absolute;display:block;width:126px;text-decoration:none;color:#fff;transform:translate(-50%,-50%);outline:none}
  .trailTile:nth-child(1){left:50%;top:8%}.trailTile:nth-child(2){left:68%;top:14%}.trailTile:nth-child(3){left:82%;top:29%}.trailTile:nth-child(4){left:86%;top:52%}.trailTile:nth-child(5){left:76%;top:76%}.trailTile:nth-child(6){left:58%;top:88%}.trailTile:nth-child(7){left:38%;top:86%}.trailTile:nth-child(8){left:20%;top:74%}.trailTile:nth-child(9){left:12%;top:52%}.trailTile:nth-child(10){left:16%;top:30%}.trailTile:nth-child(11){left:31%;top:14%}.trailTile:nth-child(12){left:50%;top:50%}.trailTile:nth-child(n+13){display:none}
  .trailRing{position:relative;width:104px;height:104px;margin:0 auto 8px;padding:3px;border-radius:24px;background:linear-gradient(135deg,#00ffa3,#06b6d4,#dc1fff,#ff2d95);box-shadow:0 0 24px rgba(6,182,212,.22),0 12px 28px rgba(0,0,0,.42)}
  .trailRing img,.trailFallback{display:block;width:100%;height:100%;object-fit:cover;border-radius:21px;background:#07080d;border:1px solid rgba(255,255,255,.16)}
  .trailFallback{display:grid;place-items:center;font-weight:900;font-size:28px;color:#fff}
  .trailPlay{position:absolute;right:-8px;bottom:-8px;width:32px;height:32px;border-radius:50%;display:grid;place-items:center;background:#d72638;border:2px solid #050505;box-shadow:0 0 18px rgba(215,38,56,.45)}
  .trailLabel{display:block;text-align:center;font-size:12px;font-weight:900;line-height:1.2;text-shadow:0 2px 14px rgba(0,0,0,.8)}
  .trailMeta{display:block;margin-top:2px;text-align:center;color:rgba(255,255,255,.72);font-size:11px;font-weight:700}
  .trailGrid{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:16px;margin-top:28px}
  .trailCard{display:grid;gap:10px;padding:14px;border-radius:22px;background:var(--panel);border:1px solid var(--border);box-shadow:var(--shadow2);text-decoration:none;color:var(--text)}
  .trailCard img,.trailCardFallback{width:100%;aspect-ratio:16/10;object-fit:cover;border-radius:16px;background:#06070b}.trailCardFallback{display:grid;place-items:center;color:#fff;font-size:34px}
  .trailCard strong{font-size:15px}.trailCard span{color:var(--muted);font-size:13px;line-height:1.45}
  .emptyTrails{max-width:720px;margin:20px auto;padding:22px;text-align:center;border:1px solid var(--border);border-radius:22px;background:var(--panel);color:var(--muted)}
  @media(max-width:760px){.trailGlobeWrap{min-height:520px}.trailOrbit{height:500px}.trailTile{width:98px}.trailRing{width:82px;height:82px;border-radius:20px}.trailRing img,.trailFallback{border-radius:17px}.trailGrid{grid-template-columns:1fr}.trailTile:nth-child(n+9){display:none}}
</style>

<section class="trailsHero">
  <span class="eyebrow">Trails around the world</span>
  <h1>Watch pet stories orbit Bully Room.</h1>
  <p>Public Trails are short pet moments, market drops, Tails, rescue updates, and community highlights. Tap a tile to open the story link.</p>
</section>

<?php if (empty($trails)): ?>
  <div class="emptyTrails">No public Trails are available right now. Open the app to create one or check back soon.</div>
<?php else: ?>
  <section class="trailGlobeWrap" aria-label="Public Bully Room Trails globe">
    <div class="trailGlobe" aria-hidden="true"></div>
    <div class="trailOrbit">
      <?php foreach (array_slice($trails, 0, 12) as $item): ?>
        <?php
          $title = trim((string)($item['title'] ?? 'Trail'));
          $author = trim((string)($item['authorUsername'] ?? ''));
          $img = trim((string)($item['imageUrl'] ?? ''));
          $url = trim((string)($item['shareUrl'] ?? ''));
          if ($url === '') $url = '/s/story-' . rawurlencode((string)($item['id'] ?? ''));
        ?>
        <a class="trailTile" href="<?= br_trails_escape($url) ?>" aria-label="Open Trail <?= br_trails_escape($title) ?>">
          <span class="trailRing">
            <?php if ($img !== ''): ?>
              <img src="<?= br_trails_escape($img) ?>" alt="" loading="lazy" decoding="async">
            <?php else: ?>
              <span class="trailFallback">BR</span>
            <?php endif; ?>
            <span class="trailPlay">▶</span>
          </span>
          <span class="trailLabel"><?= br_trails_escape(br_trails_trim($title, 34)) ?></span>
          <?php if ($author !== ''): ?><span class="trailMeta">@<?= br_trails_escape($author) ?></span><?php endif; ?>
        </a>
      <?php endforeach; ?>
    </div>
  </section>

  <section class="trailGrid" aria-label="Latest public Trails">
    <?php foreach ($trails as $item): ?>
      <?php
        $title = trim((string)($item['title'] ?? 'Trail'));
        $caption = trim((string)($item['caption'] ?? 'Open this Bully Room Trail.'));
        $img = trim((string)($item['imageUrl'] ?? ''));
        $url = trim((string)($item['shareUrl'] ?? ''));
        if ($url === '') $url = '/s/story-' . rawurlencode((string)($item['id'] ?? ''));
      ?>
      <a class="trailCard" href="<?= br_trails_escape($url) ?>">
        <?php if ($img !== ''): ?>
          <img src="<?= br_trails_escape($img) ?>" alt="<?= br_trails_escape($title) ?>" loading="lazy" decoding="async">
        <?php else: ?>
          <span class="trailCardFallback">BR</span>
        <?php endif; ?>
        <strong><?= br_trails_escape($title) ?></strong>
        <span><?= br_trails_escape(br_trails_trim($caption, 120)) ?></span>
      </a>
    <?php endforeach; ?>
  </section>
<?php endif; ?>

<?php require_once __DIR__ . '/../includes/br_layout_bottom.php'; ?>
