<?php
require_once __DIR__ . '/lib/news-lib.php';

$id = $_GET['id'] ?? null;
$file = news_data_dir() . basename(str_replace('\\', '/', (string)$id));

if (!file_exists($file)) {
    http_response_code(404);
    exit('Article not found');
}

$parsed = news_parse_file($file);
$date        = $parsed['date'];
$title       = $parsed['title'];
$categories  = $parsed['categories'];
$description = $parsed['description'];
$body        = news_render_body_html($parsed['body']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= htmlspecialchars($title) ?></title>
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: #f0f0f0; color: #1a1a1a; font-size: 14px; line-height: 1.7; padding: 24px 20px 48px; }
.article-card { background: #fff; border: 1px solid #d8d8d8; border-radius: 12px; overflow: hidden; max-width: 860px; margin: 0 auto; }
.article-header { background: #141414; padding: 20px 24px; }
.article-date { font-size: 12px; color: #9a9a9a; margin-bottom: 6px; }
.article-title { font-size: 18px; font-weight: 700; color: #fff; }
.news-cat { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 9px; border-radius: 20px; background: #333; color: #ddd; margin-right: 4px; }
.article-desc { font-size: 12px; color: #aaa; }
.article-body { padding: 24px 28px; }
.article-footer { padding: 14px 24px; border-top: 1px solid #eee; background: #f5f5f5; }
.btn-back { display: inline-block; padding: 7px 16px; border-radius: 7px; border: 1px solid #bbb; background: #fff; color: #1a1a1a; font-size: 12px; text-decoration: none; }
.btn-back:hover { background: #eee; }
</style>
</head>
<body>
<div class="article-card">
  <div class="article-header">
    <div class="article-date"><?= htmlspecialchars($date) ?></div>
    <div class="article-title"><?= htmlspecialchars($title) ?></div>
    <?php if ($categories !== [] || $description !== ''): ?>
    <div class="article-meta" style="margin-top:10px;">
      <?= news_category_labels_html($categories) ?>
      <?php if ($description !== ''): ?><span class="article-desc"><?= htmlspecialchars($description) ?></span><?php endif; ?>
    </div>
    <?php endif; ?>
  </div>
  <div class="article-body"><?= $body ?></div>
  <div class="article-footer">
    <a href="/index.php" class="btn-back">◀ Lab home</a>
  </div>
</div>
</body>
</html>
