<?php
require_once __DIR__ . '/includes/config.php';
$pageTitle   = 'GitHub Trending — dispelled.ca';
$currentPage = 'github';

$LANG_COLORS = [
    'TypeScript' => '#3178c6', 'JavaScript' => '#f1e05a', 'Python'  => '#3572A5',
    'Rust'       => '#dea584', 'Go'         => '#00ADD8', 'C++'     => '#f34b7d',
    'C'          => '#555555', 'Java'       => '#b07219', 'Kotlin'  => '#A97BFF',
    'Swift'      => '#F05138', 'Ruby'       => '#701516', 'PHP'     => '#4F5D95',
    'Shell'      => '#89e051', 'HTML'       => '#e34c26', 'CSS'     => '#563d7c',
    'Dart'       => '#00B4AB', 'Zig'        => '#ec915c', 'Nix'     => '#7e7eff',
    'Lua'        => '#000080', 'Haskell'    => '#5e5086', 'Elixir'  => '#6e4a7e',
];

$cacheFile = sys_get_temp_dir() . '/dispelled_github.json';
$repos     = null;

if (file_exists($cacheFile) && (time() - filemtime($cacheFile)) < 900) {
    $repos = json_decode(file_get_contents($cacheFile), true);
}

if (!$repos) {
    $since = date('Y-m-d', strtotime('-7 days'));
    $apiUrl = "https://api.github.com/search/repositories?q=created:>{$since}&sort=stars&order=desc&per_page=25";
    $ctx = stream_context_create(['http' => [
        'timeout' => 10,
        'header'  => "User-Agent: dispelled.ca/1.0\r\nAccept: application/vnd.github.v3+json\r\n",
    ]]);
    $raw = @file_get_contents($apiUrl, false, $ctx);
    if ($raw) {
        $json  = json_decode($raw, true);
        $repos = [];
        foreach ($json['items'] ?? [] as $r) {
            $repos[] = [
                'name'        => $r['full_name'],
                'url'         => $r['html_url'],
                'description' => $r['description'] ?? null,
                'stars'       => $r['stargazers_count'],
                'language'    => $r['language'] ?? null,
                'topics'      => array_slice($r['topics'] ?? [], 0, 4),
            ];
        }
        file_put_contents($cacheFile, json_encode($repos));
    }
}

require_once __DIR__ . '/includes/header.php';
?>

<h1 class="page-title">GitHub Trending</h1>
<p class="page-subtitle">Top new repositories from the past 7 days.</p>

<?php if (!$repos): ?>
  <p style="color:var(--primary-light);font-family:monospace">Could not load GitHub data.</p>
<?php else: ?>
<div class="gh-list">
  <?php foreach ($repos as $repo):
    $langColor = $LANG_COLORS[$repo['language'] ?? ''] ?? '#888888';
  ?>
  <a href="<?= htmlspecialchars($repo['url']) ?>" target="_blank" rel="noreferrer" class="gh-card">
    <div class="gh-card-inner">
      <div class="gh-card-body">
        <h2 class="gh-name"><?= htmlspecialchars($repo['name']) ?></h2>
        <?php if ($repo['description']): ?>
          <p class="gh-desc"><?= htmlspecialchars($repo['description']) ?></p>
        <?php endif ?>
        <div class="gh-meta">
          <?php if ($repo['language']): ?>
            <span class="gh-lang">
              <span class="gh-lang-dot" style="background:<?= $langColor ?>"></span>
              <?= htmlspecialchars($repo['language']) ?>
            </span>
          <?php endif ?>
          <?php foreach ($repo['topics'] as $t): ?>
            <span class="gh-topic"><?= htmlspecialchars($t) ?></span>
          <?php endforeach ?>
        </div>
      </div>
      <div class="gh-stars">&#9733; <?= number_format($repo['stars']) ?></div>
    </div>
  </a>
  <?php endforeach ?>
</div>
<p class="data-attr">Data via GitHub public API &middot; Repos created in the last 7 days, sorted by stars.</p>
<?php endif ?>

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