<?php
require_once __DIR__ . '/../includes/functions.php';
header('Content-Type: application/json');

$q = trim($_GET['q'] ?? '');
if (mb_strlen($q) < 2) { echo json_encode([]); exit; }

$like = '%' . $q . '%';
$stmt = db()->prepare('SELECT title, slug, sector FROM properties WHERE title LIKE ? OR sector LIKE ? OR neighborhood LIKE ? ORDER BY is_featured DESC LIMIT 6');
$stmt->execute([$like, $like, $like]);
$rows = $stmt->fetchAll();

$results = array_map(fn($r) => [
    'label' => $r['title'] . ($r['sector'] ? ' — ' . $r['sector'] : ''),
    'url' => '/properties/' . $r['slug'],
], $rows);

echo json_encode($results);
