<?php
/**
 * Expects $p (property row from `properties` table) in scope.
 */
$img = get_property_primary_image((int)$p['id']);
$category = get_category_by_slug_id((int)$p['category_id']);
$district = get_district_by_id((int)$p['district_id']);
$isLand = in_array($category['slug'] ?? '', ['residential-land','development-land','agricultural-land'], true);
?>
<article class="card property-card">
  <a href="/properties/<?= e($p['slug']) ?>" class="media-frame" aria-label="View <?= e($p['title']) ?>">
    <img src="/<?= e($img ?? 'assets/images/properties/villa-ext-a.jpg') ?>" alt="<?= e($p['title']) ?>" loading="lazy" width="480" height="360">
  </a>
  <div class="badges">
    <span class="badge <?= $p['status'] === 'rent' ? 'badge-rent' : 'badge-sale' ?>"><?= $p['status'] === 'rent' ? 'For Rent' : 'For Sale' ?></span>
    <?php if (!empty($p['is_new'])): ?><span class="badge badge-new">New</span><?php endif; ?>
  </div>
  <button class="fav-btn" data-id="<?= (int)$p['id'] ?>" aria-pressed="false" aria-label="Save to favorites"><?= icon('heart', 18) ?></button>
  <div class="body">
    <div class="price"><?= format_price((float)$p['price'], $p['currency'], $p['price_period']) ?></div>
    <h3><a href="/properties/<?= e($p['slug']) ?>"><?= e($p['title']) ?></a></h3>
    <div class="loc"><?= icon('map-pin', 14) ?> <?= e($district['name'] ?? '') ?><?= !empty($p['sector']) ? ', ' . e($p['sector']) : '' ?></div>
    <div class="meta">
      <?php if (!$isLand): ?>
        <span><?= icon('bed', 14) ?> <?= (int)$p['bedrooms'] ?> Beds</span>
        <span><?= icon('bath', 14) ?> <?= (int)$p['bathrooms'] ?> Baths</span>
      <?php endif; ?>
      <span><?= icon('area', 14) ?> <?= (int)($p['land_size_sqm'] ?: $p['building_size_sqm']) ?> m²</span>
    </div>
    <div class="card-actions">
      <a href="/properties/<?= e($p['slug']) ?>" class="btn btn-outline btn-sm">View Details</a>
      <a href="/schedule-tour.php?property=<?= (int)$p['id'] ?>" class="btn btn-primary btn-sm">Book a Visit</a>
      <a href="<?= e(tel_link()) ?>" class="btn-icon-only card-action-icon" aria-label="Call now about <?= e($p['title']) ?>"><?= icon('phone', 15) ?></a>
      <a href="<?= e(whatsapp_link('Hello! I am interested in "' . $p['title'] . '" (' . rtrim(SITE_URL, '/') . '/properties/' . $p['slug'] . ').')) ?>" class="btn-icon-only card-action-icon card-action-icon--whatsapp" target="_blank" rel="noopener" aria-label="WhatsApp about <?= e($p['title']) ?>"><?= icon('whatsapp', 15) ?></a>
    </div>
  </div>
</article>
