<?php
require_once __DIR__ . "/config.inc.php";

header('Content-Type: application/json; charset=utf-8');

$brand_id = (int)($_GET['brand_id'] ?? 0);
if ($brand_id <= 0) {
    echo json_encode([]);
    exit;
}

$sql = "
  (SELECT display_model AS model
   FROM products
   WHERE brand_id = ?)
  UNION
  (SELECT model_name AS model
   FROM compatible_models
   WHERE brand_id = ?)
  ORDER BY model ASC
";
$stmt = db()->prepare($sql);
$stmt->bind_param("ii", $brand_id, $brand_id);
$stmt->execute();
$res = $stmt->get_result();

$models = [];
while ($row = $res->fetch_assoc()) {
    $models[] = $row['model'];
}
$stmt->close();

echo json_encode($models);