<?php
require_once __DIR__.'/../inc/config.php'; require_once __DIR__.'/../inc/db.php';
header('Content-Type: application/json');
$q=strtolower(trim($_GET['q']??'')); if($q===''){ echo json_encode(['a'=>'Soalan kosong']); exit; }
$st=db()->prepare("SELECT a FROM faq WHERE active=1 AND (lower(q) LIKE '%' || ? || '%' OR lower(a) LIKE '%' || ? || '%') ORDER BY id LIMIT 1");
$st->execute([$q,$q]);
$r=$st->fetch();
echo json_encode(['a'=>$r?$r['a']:'Tiada jawapan tepat. Cuba soalan lain.']);