<?php
require_once __DIR__ . '/includes/functions.php';
$perPage = 12;
$page = max(1, (int)($_GET['page'] ?? 1));
$q = trim((string)($_GET['q'] ?? ''));
$platform = strtolower(trim((string)($_GET['platform'] ?? 'all')));
$sort = (string)($_GET['sort'] ?? 'newest');
$options = ['search'=>$q,'platform'=>$platform,'sort'=>$sort];
$total = countReleases($options);
$pages = max(1, (int)ceil($total / $perPage));
$page = min($page, $pages);
$downloads = getReleases($options + ['limit'=>$perPage,'offset'=>($page-1)*$perPage]);
$featured = getReleases(['featured'=>true,'limit'=>3,'sort'=>'popular']);
$platforms = platforms();
$totalDownloads = totalDownloads();
?>
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>PhantomNET Downloads</title><meta name="description" content="PhantomNET download archive for console and PC resources."><link rel="stylesheet" href="assets/css/style.css"></head><body>
<div class="noise"></div>
<header class="site-header"><div class="shell nav-wrap"><a class="brand" href="index.php"><span class="brand-mark">P</span><span><strong>PhantomNET</strong><small>Downloads</small></span></a><nav><a class="active" href="index.php">Downloads</a><a href="#library">Library</a><a href="admin/login.php">Admin</a></nav></div></header>
<main>
<section class="hero shell"><div class="hero-copy"><span class="eyebrow">PUBLIC ARCHIVE</span><h1>Old-gen releases.<br><span>Built properly.</span></h1><p>A cleaner home for tools, resources and community releases with search, screenshots, metadata and tracked downloads.</p><div class="hero-actions"><a class="primary-btn" href="#library">Browse library</a><a class="ghost-btn" href="?sort=popular#library">Most downloaded</a></div></div><div class="hero-stats"><div><strong><?= $total ?></strong><span>Files</span></div><div><strong><?= count($platforms) ?></strong><span>Platforms</span></div><div><strong><?= number_format($totalDownloads) ?></strong><span>Downloads</span></div></div></section>
<?php if ($featured): ?><section class="shell featured"><div class="section-heading"><div><span class="eyebrow">FEATURED</span><h2>Highlighted releases</h2></div></div><div class="featured-grid"><?php foreach($featured as $item): ?><a class="featured-card" href="release.php?id=<?= (int)$item['id'] ?>"><?php if($item['screenshot_url']): ?><img src="<?= htmlspecialchars($item['screenshot_url']) ?>" alt=""><?php else: ?><div class="featured-placeholder">PN</div><?php endif; ?><div class="featured-overlay"><span><?= htmlspecialchars(strtoupper($item['platform'])) ?><?= $item['game'] ? ' • '.htmlspecialchars(strtoupper($item['game'])) : '' ?></span><h3><?= htmlspecialchars($item['title']) ?></h3><small><?= number_format((int)$item['downloads']) ?> downloads</small></div></a><?php endforeach; ?></div></section><?php endif; ?>
<section class="shell library" id="library"><div class="section-heading"><div><span class="eyebrow">LIBRARY</span><h2>Browse downloads</h2></div><span><?= number_format($total) ?> result<?= $total===1?'':'s' ?></span></div>
<form class="controls" method="get" action="#library"><div class="search-box"><span>⌕</span><input name="q" value="<?= htmlspecialchars($q) ?>" type="search" placeholder="Search title, game, tags or filename..."></div><select name="platform"><option value="all">All platforms</option><?php foreach($platforms as $p): ?><option value="<?= htmlspecialchars($p) ?>" <?= $platform===$p?'selected':'' ?>><?= htmlspecialchars(strtoupper($p)) ?></option><?php endforeach; ?></select><select name="sort"><option value="newest" <?= $sort==='newest'?'selected':'' ?>>Newest</option><option value="popular" <?= $sort==='popular'?'selected':'' ?>>Most downloaded</option><option value="name" <?= $sort==='name'?'selected':'' ?>>Name A-Z</option><option value="oldest" <?= $sort==='oldest'?'selected':'' ?>>Oldest</option></select><button class="primary-btn" type="submit">Filter</button></form>
<div class="download-grid"><?php if(!$downloads): ?><div class="empty-state"><strong>No releases matched.</strong><p>Try a different search or platform.</p></div><?php endif; ?><?php foreach($downloads as $item): ?><article class="download-card"><div class="file-icon"><?= strtoupper(substr($item['extension'] ?: 'F',0,3)) ?></div><div class="file-main"><div class="file-topline"><span class="tag"><?= htmlspecialchars(strtoupper($item['platform'])) ?></span><?php if($item['game']): ?><span class="tag muted"><?= htmlspecialchars(strtoupper($item['game'])) ?></span><?php endif; ?><?php if($item['featured']): ?><span class="tag gold">FEATURED</span><?php endif; ?></div><h3><a href="release.php?id=<?= (int)$item['id'] ?>"><?= htmlspecialchars($item['title']) ?></a></h3><p><?= htmlspecialchars($item['description'] ?: $item['filename']) ?></p><div class="file-meta"><span><?= htmlspecialchars($item['size']) ?></span><span><?= number_format((int)$item['downloads']) ?> downloads</span><span>Updated <?= htmlspecialchars($item['modified_label']) ?></span></div></div><a class="download-btn" href="release.php?id=<?= (int)$item['id'] ?>">View release</a></article><?php endforeach; ?></div>
<?php if($pages>1): ?><div class="pagination"><?php for($i=1;$i<=$pages;$i++): ?><a class="<?= $i===$page?'active':'' ?>" href="?<?= http_build_query(['q'=>$q,'platform'=>$platform,'sort'=>$sort,'page'=>$i]) ?>#library"><?= $i ?></a><?php endfor; ?></div><?php endif; ?></section>
</main><footer><div class="shell footer-inner"><span>© <?= date('Y') ?> PhantomNET</span><span>Archive • Tools • Releases</span></div></footer></body></html>
