<?php
// ============================================================
//  ajax/search_keywords.php
//  Returns matching keywords from master keywords table
// ============================================================
require_once __DIR__ . '/../config/db.php';
header('Content-Type: application/json');

$q = trim($_GET['q'] ?? '');
if (strlen($q) < 1) jsonResponse(true, '', ['keywords' => []]);

try {
    $stmt = db()->prepare(
        "SELECT keyword_id, keyword FROM keywords
         WHERE keyword LIKE ?
         ORDER BY keyword ASC
         LIMIT 12"
    );
    $stmt->execute(['%' . $q . '%']);
    $keywords = $stmt->fetchAll();
    jsonResponse(true, '', ['keywords' => $keywords]);
} catch (PDOException $e) {
    jsonResponse(false, 'DB error: ' . $e->getMessage());
}