<?php
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/seo.php';

$slug = $_GET['slug'] ?? '';
$category = get_category_by_slug($slug);
if (!$category) { http_response_code(404); include __DIR__ . '/../404.php'; exit; }

$stmt = db()->prepare('SELECT * FROM properties WHERE category_id = ? ORDER BY is_featured DESC, created_at DESC');
$stmt->execute([$category['id']]);
$properties = $stmt->fetchAll();

$seo = [
    'title' => $category['name'] . ' in Kigali, Rwanda | ' . SITE_NAME,
    'description' => $category['description'] . ' Browse verified ' . strtolower($category['name']) . ' listings in Kigali.',
    'path' => '/category/' . $category['slug'],
];
$extraSchema = schema_breadcrumb([
    ['name' => 'Properties', 'url' => '/properties'],
    ['name' => $category['name'], 'url' => '/category/' . $category['slug']],
]);
include __DIR__ . '/../includes/header.php';
?>
<section class="section-pad-sm">
  <div class="container">
    <?= breadcrumb_html([
        ['name' => 'Properties', 'url' => '/properties'],
        ['name' => $category['name'], 'url' => '/category/' . $category['slug']],
    ]) ?>
    <div class="flex items-center gap-4 mb-4">
      <span class="icon-wrap" style="width:56px;height:56px;border-radius:999px;background:var(--color-cream);display:flex;align-items:center;justify-content:center;color:var(--color-accent-dark);"><?= category_icon($category['icon']) ?></span>
      <div>
        <div class="eyebrow" style="margin-bottom:0;">Property Category</div>
        <h1 style="font-size:var(--fs-2xl);margin-bottom:0;"><?= e($category['name']) ?></h1>
      </div>
    </div>
    <p class="lede" style="max-width:70ch;"><?= e($category['description']) ?> Currently <?= count($properties) ?> verified listing<?= count($properties) === 1 ? '' : 's' ?> available.</p>

    <?php if (empty($properties)): ?>
      <div class="form-card text-center mt-8">
        <h3>No <?= e(strtolower($category['name'])) ?> listed right now</h3>
        <p class="text-muted">New listings in this category are added regularly — be the first to know.</p>
        <a href="/vip-alerts" class="btn btn-accent mt-4">Join VIP Property Alerts</a>
      </div>
    <?php else: ?>
      <div class="grid grid-3 mt-8">
        <?php foreach ($properties as $p): ?>
          <?php include __DIR__ . '/../includes/partials/property-card.php'; ?>
        <?php endforeach; ?>
      </div>
    <?php endif; ?>
  </div>
</section>
<?php include __DIR__ . '/../includes/footer.php'; ?>
