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

$ids = array_filter(array_map('intval', explode(',', $_GET['ids'] ?? '')));
if (empty($ids)) { echo json_encode([]); exit; }

$placeholders = implode(',', array_fill(0, count($ids), '?'));
$stmt = db()->prepare("SELECT * FROM properties WHERE id IN ($placeholders)");
$stmt->execute(array_values($ids));
$rows = $stmt->fetchAll();

$out = [];
foreach ($rows as $p) {
    $category = get_category_by_slug_id((int)$p['category_id']);
    $district = get_district_by_id((int)$p['district_id']);
    $img = get_property_primary_image((int)$p['id']);
    $out[] = [
        'id' => (int)$p['id'],
        'title' => $p['title'],
        'slug' => $p['slug'],
        'url' => '/properties/' . $p['slug'],
        'image' => '/' . ($img ?: 'assets/images/properties/villa-ext-a.jpg'),
        'status' => $p['status'],
        'price_label' => format_price((float)$p['price'], $p['currency'], $p['price_period']),
        'bedrooms' => (int)$p['bedrooms'],
        'bathrooms' => (int)$p['bathrooms'],
        'land_size_sqm' => (int)$p['land_size_sqm'],
        'building_size_sqm' => (int)$p['building_size_sqm'],
        'category' => $category['name'] ?? '',
        'district' => $district['name'] ?? '',
        'investment_score' => (int)$p['investment_score'],
    ];
}
echo json_encode($out);
