<?php
declare(strict_types=1);

require_once __DIR__ . "/../../config/db.php";
require_once __DIR__ . "/../../config/auth.php";

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

$q = trim((string)($_GET['q'] ?? ''));

if ($q === '') {
  // initial small list (fast)
  $st = $pdo->query("SELECT id, name FROM schools ORDER BY name ASC LIMIT 200");
  echo json_encode($st->fetchAll(PDO::FETCH_ASSOC), JSON_UNESCAPED_UNICODE);
  exit;
}

$st = $pdo->prepare("
  SELECT id, name
  FROM schools
  WHERE name LIKE ? OR id LIKE ?
  ORDER BY name ASC
  LIMIT 200
");
$st->execute(["%{$q}%","%{$q}%"]);
echo json_encode($st->fetchAll(PDO::FETCH_ASSOC), JSON_UNESCAPED_UNICODE);
