<?php
require_once __DIR__ . '/includes/config.php';

$slug = $_GET['slug'] ?? '';
$post = q1("SELECT p.*, c.name cat_name, c.slug cat_slug FROM posts p LEFT JOIN categories c ON c.id=p.category_id WHERE p.slug = :s AND p.status='published'", [':s' => $slug]);

if (!$post) {
    http_response_code(404);
    $pageTitle = 'Contenuto non trovato | ' . SITE_NAME;
    require __DIR__ . '/includes/header.php';
    echo '<section class="page-hero"><div class="container"><h1>404</h1><p>Il contenuto che cerchi non esiste o è stato spostato.</p><a class="btn btn-gold" style="margin-top:20px" href="'.url('blog.php').'">Torna al Blog</a></div></section>';
    require __DIR__ . '/includes/footer.php';
    exit;
}

exec_q("UPDATE posts SET views = views + 1 WHERE id = :id", [':id' => (int)$post['id']]);

$related = q("SELECT p.*, c.name cat_name FROM posts p LEFT JOIN categories c ON c.id=p.category_id WHERE p.status='published' AND p.id != :id AND (p.category_id = :cat OR p.type = :type) ORDER BY p.published_at DESC LIMIT 3",
    [':id'=>(int)$post['id'], ':cat'=>(int)($post['category_id'] ?? 0), ':type'=>$post['type']]);

$pageTitle = ($post['meta_title'] ?: $post['title']) . ' | ' . SITE_NAME;
$pageDesc  = $post['meta_desc'] ?: ($post['excerpt'] ?: excerpt_from($post['body']));
require __DIR__ . '/includes/header.php';
?>
<article class="container article">
  <div style="margin-top:40px">
    <a href="<?= url('blog.php') ?>" class="muted" style="font-size:.9rem">← Blog</a>
    <div style="margin:18px 0 6px"><span class="badge <?= e($post['type']) ?>"><?= e($post['type']) ?></span> <?php if($post['cat_name']): ?><span class="muted" style="margin-left:8px"><?= e($post['cat_name']) ?></span><?php endif; ?></div>
  </div>
  <h1><?= e($post['title']) ?></h1>
  <div class="post-meta">
    <span><?= e($post['author']) ?></span>
    <span><?= it_date($post['published_at']) ?></span>
    <span><?= reading_time($post['body']) ?> min di lettura</span>
    <span><?= (int)$post['views'] ?> letture</span>
  </div>
  <?php if($post['cover']): ?><img src="<?= e(uploads_url('images/'.$post['cover'])) ?>" alt="<?= e($post['title']) ?>" style="border-radius:var(--radius-lg);margin-bottom:30px"><?php endif; ?>
  <div class="content"><?= $post['body'] ?></div>

  <?php if($post['tags']): ?>
  <div style="margin-top:40px;display:flex;gap:8px;flex-wrap:wrap">
    <?php foreach (array_filter(array_map('trim', explode(',', $post['tags']))) as $tag): ?>
      <span class="badge blog">#<?= e($tag) ?></span>
    <?php endforeach; ?>
  </div>
  <?php endif; ?>

  <div style="margin-top:50px;background:var(--ink);color:var(--paper);border-radius:var(--radius-lg);padding:40px;text-align:center">
    <h3 style="color:var(--paper);font-size:1.5rem">Vuoi sapere quanto potresti risparmiare TU?</h3>
    <p style="color:#b9c2d6">Consulenza di 15-20 minuti. Ti dico anche NO se non conviene.</p>
    <a class="btn btn-gold btn-lg" style="margin-top:14px" href="<?= e(wa('Ho letto un articolo, voglio i miei numeri')) ?>" target="_blank" rel="noopener">Prenota la Consulenza</a>
  </div>
</article>

<?php if($related): ?>
<section class="section" style="background:var(--paper-2)">
  <div class="container">
    <div class="section-head left"><h2 style="font-size:1.8rem">Continua a <span class="it">leggere</span></h2></div>
    <div class="post-grid">
      <?php foreach ($related as $p): ?>
      <a class="post-card reveal" href="<?= url('post.php?slug='.urlencode($p['slug'])) ?>">
        <div class="thumb"><?php if($p['cover']): ?><img src="<?= e(uploads_url('images/'.$p['cover'])) ?>" alt="<?= e($p['title']) ?>"><?php else: ?><span class="ph">F</span><?php endif; ?></div>
        <div class="body">
          <span class="cat"><span class="badge <?= e($p['type']) ?>"><?= e($p['type']) ?></span></span>
          <h3><?= e($p['title']) ?></h3>
          <p><?= e($p['excerpt'] ?: excerpt_from($p['body'])) ?></p>
        </div>
      </a>
      <?php endforeach; ?>
    </div>
  </div>
</section>
<?php endif; ?>
<?php require __DIR__ . '/includes/footer.php'; ?>
