<?php
declare(strict_types=1);

$db = new SQLite3(__DIR__ . '/../data/blog.sqlite', SQLITE3_OPEN_READONLY);
$db->busyTimeout(3000);

if ($slug === 'blog') {
    // Blog listing
    $posts_result = $db->query("SELECT slug, title, excerpt, created_at FROM posts WHERE published=1 ORDER BY created_at DESC");
    $posts = [];
    while ($row = $posts_result->fetchArray(SQLITE3_ASSOC)) { $posts[] = $row; }
    $db->close();

    $page_title = 'Hardwood Floor Tips & Advice | Floor It Hardwood Floors Blog';
    $page_meta  = 'Expert hardwood floor tips from Floor It Hardwood Floors. Refinishing, restoration, care advice for Buffalo and Erie County homeowners.';
    require __DIR__ . '/../components/_header.php';
    ?>
    <section class="page-hero section section--dark">
      <div class="container">
        <div class="page-hero-content">
          <span class="eyebrow">Hardwood Floor Tips</span>
          <h1>The Floor It Blog</h1>
          <p class="lead">Expert advice on hardwood floor refinishing, restoration, and care from the Buffalo area's most experienced team.</p>
        </div>
      </div>
    </section>
    <section class="section section--light">
      <div class="container">
        <?php if ($posts): ?>
        <div class="grid grid--3col blog-grid">
          <?php foreach ($posts as $post): ?>
          <article class="card card--blog">
            <div class="card-body">
              <h2><a href="/blog/<?= htmlspecialchars($post['slug'], ENT_QUOTES) ?>/"><?= htmlspecialchars($post['title'], ENT_QUOTES) ?></a></h2>
              <p><?= htmlspecialchars($post['excerpt'] ?? '', ENT_QUOTES) ?></p>
              <a href="/blog/<?= htmlspecialchars($post['slug'], ENT_QUOTES) ?>/" class="card-link">Read more &rarr;</a>
            </div>
          </article>
          <?php endforeach; ?>
        </div>
        <?php else: ?>
        <p>No posts yet. Check back soon.</p>
        <?php endif; ?>
      </div>
    </section>
    <section class="section section--amber cta-section">
      <div class="container cta-section-inner">
        <div class="cta-section-text">
          <h2>Questions About Your Floors?</h2>
          <p>Contact our team for a free estimate and expert advice.</p>
        </div>
        <a href="/contact/" class="btn btn--primary btn--lg">Request an Estimate</a>
      </div>
    </section>
    <?php
} else {
    // Individual post
    $post = $db->querySingle("SELECT * FROM posts WHERE slug='" . SQLite3::escapeString($slug) . "' AND published=1", true);
    $db->close();

    if (!$post) {
        http_response_code(404);
        $page_title = '404: Post Not Found | Floor It Hardwood Floors';
        $page_meta  = '';
        require __DIR__ . '/../components/_header.php';
        echo '<section class="section section--light"><div class="container"><h1>Post Not Found</h1><p><a href="/blog/">Back to blog</a></p></div></section>';
        require __DIR__ . '/../components/_footer.php';
        exit;
    }

    $page_title = $post['title'] . ' | Floor It Hardwood Floors';
    $page_meta  = $post['excerpt'] ?? '';
    require __DIR__ . '/../components/_header.php';
    ?>
    <section class="page-hero section section--dark">
      <div class="container">
        <div class="page-hero-content">
          <span class="eyebrow">Floor It Blog</span>
          <h1><?= htmlspecialchars($post['title'], ENT_QUOTES) ?></h1>
          <p class="lead"><?= htmlspecialchars($post['excerpt'] ?? '', ENT_QUOTES) ?></p>
        </div>
      </div>
    </section>
    <section class="section section--light">
      <div class="container blog-post-body">
        <?= $post['body_html'] ?>
        <p style="margin-top:2rem;"><a href="/blog/">Back to blog</a></p>
      </div>
    </section>
    <section class="section section--amber cta-section">
      <div class="container cta-section-inner">
        <div class="cta-section-text">
          <h2>Ready to Restore Your Floors?</h2>
          <p>Request a free estimate from our Buffalo team.</p>
        </div>
        <a href="/contact/" class="btn btn--primary btn--lg">Get a Free Estimate</a>
      </div>
    </section>
    <?php
}

require __DIR__ . '/../components/_footer.php'; ?>