<?php
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/includes/blog_posts.php';
startSecureSession();
$slug = trim($_GET['slug'] ?? '');
$post = ah_get_post($slug);
if (!$post) {
    header('Location: blog.php');
    exit;
}
$base = getBaseUrl();
$postUrl = $base . '/blog/' . $slug . '/';
$posts = ah_all_posts();
$others = array_values(array_filter($posts, fn($p) => $p['slug'] !== $slug));

$pageTitle = $post['title'];
$metaDesc  = $post['metaDesc'];
$navActive = 'blog';
$canonical = $base . '/blog/' . $slug . '/';
$ogImage   = 'assets/img/og-cover.svg';
$schemaArr = [
    '@context' => 'https://schema.org',
    '@type' => 'BlogPosting',
    'headline' => $post['title'],
    'description' => $post['metaDesc'],
    'datePublished' => $post['date'],
    'dateModified' => $post['date'],
    'author' => ['@type' => 'Organization', 'name' => 'AdityaHost'],
    'publisher' => ['@type' => 'Organization', 'name' => 'AdityaHost', 'logo' => ['@type' => 'ImageObject', 'url' => $base . '/assets/img/logo.svg']],
    'mainEntityOfPage' => $canonical,
];
$schemaOrg = '<script type="application/ld+json">' . json_encode($schemaArr, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . '</script>';

include __DIR__ . '/includes/public_header.php';
?>

<section class="post-hero <?= $post['slug'] === 'scam-warning-fake-domains' ? 'red' : '' ?>">
  <div class="wrap">
    <div class="crumb"><a href="blog.php">← All articles</a></div>
    <h1><?= htmlspecialchars($post['title']) ?></h1>
    <div class="post-meta">
      <span class="chip"><svg viewBox="0 0 24 24" fill="none" stroke-width="2" stroke-linecap="round"><rect x="3" y="4" width="18" height="17" rx="2"/><path d="M8 2v4M16 2v4M3 9h18"/></svg> <?= date('F j, Y', strtotime($post['date'])) ?></span>
      <span class="chip"><svg viewBox="0 0 24 24" fill="none" stroke-width="2" stroke-linecap="round"><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 3"/></svg> <?= $post['readMins'] ?> min read</span>
      <span class="chip"><svg viewBox="0 0 24 24" fill="none" stroke-width="2" stroke-linecap="round"><path d="M20 7h-9M14 17H5M17 12H3"/></svg> <?= htmlspecialchars($post['category']) ?></span>
    </div>
  </div>
</section>

<section class="section" style="padding-top:0">
  <div class="wrap post-wrap">
    <article class="prose">
      <div class="blog-cover" style="height:210px;border-radius:16px;margin:26px 0 24px"><?= ah_cover($post) ?></div>
      <?= $post['content'] ?>

      <div class="post-tags">
        <?php foreach (explode(',', $post['keywords']) as $k): ?><span><?= htmlspecialchars(trim($k)) ?></span><?php endforeach; ?>
      </div>

      <div class="author-card">
        <div class="av">A</div>
        <div>
          <b>AdityaHost Team</b><br>
          <small>We build simple payment infrastructure for India&rsquo;s creators — and write about it honestly.</small>
        </div>
      </div>
    </article>

    <aside class="toc">
      <h4>On this page</h4>
      <script>(function(){
        var h=document.querySelectorAll('.prose h2');var t=document.querySelector('.toc');
        if(!h.length){document.getElementById('tocRoot')?.remove();return;}
        h.forEach(function(el){var id=el.textContent.toLowerCase().replace(/[^a-z0-9]+/g,'-').replace(/^-|-$/g,'');el.id=id;var a=document.createElement('a');a.href='#'+id;a.textContent=el.textContent;t.insertBefore(a,document.querySelector('.toc-line'));});
      })();</script>
      <div class="toc-line"></div>
      <div class="mini">
        <b>Ready to accept payments?</b>
        <a href="register.php">Get API free</a>
        <a href="demo.php">Demo</a>
      </div>
    </aside>
  </div>
</section>

<?php if ($others): ?>
<section class="section section-soft">
  <div class="wrap">
    <div class="center reveal">
      <span class="eyebrow">Keep reading</span>
      <h2 class="section-title">More guides for you</h2>
    </div>
    <div class="blog-grid reveal" style="margin-top:44px">
      <?php foreach (array_slice($others, 0, 3) as $bp): ?>
      <a class="blog-card" href="blog-post.php?slug=<?= $bp['slug'] ?>">
        <div class="blog-cover"><?= ah_cover($bp) ?><span class="blog-cat"><?= htmlspecialchars($bp['category']) ?></span></div>
        <div class="blog-body">
          <div class="blog-meta"><span><?= date('M j, Y', strtotime($bp['date'])) ?></span><span class="sep"></span><span><?= $bp['readMins'] ?> min read</span></div>
          <h3><?= htmlspecialchars($bp['title']) ?></h3>
          <p class="blog-ex"><?= htmlspecialchars((function_exists("mb_substr") ? mb_substr($bp["metaDesc"], 0, 110) : substr($bp["metaDesc"], 0, 110))) ?>…</p>
          <span class="blog-read">Read guide <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"><path d="M5 12h14M13 6l6 6-6 6"/></svg></span>
        </div>
      </a>
      <?php endforeach; ?>
    </div>
  </div>
</section>
<?php endif; ?>

<?php include __DIR__ . '/includes/public_footer.php'; ?>