<?php
// ============================================================
//  ajax/search_editors.php
//  Search journal editors / editorial board members
// ============================================================
require_once __DIR__ . '/../config/db.php';
header('Content-Type: application/json');

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

try {
    $stmt = db()->prepare(
        "SELECT editor_id, CONCAT(first_name,' ',last_name) AS name,
                institution, expertise_areas AS expertise
         FROM journal_editors
         WHERE (first_name LIKE ? OR last_name LIKE ?
                OR institution LIKE ? OR expertise_areas LIKE ?)
           AND is_active = 1
         ORDER BY last_name ASC
         LIMIT 10"
    );
    $like = '%' . $q . '%';
    $stmt->execute([$like, $like, $like, $like]);
    $editors = $stmt->fetchAll();
    jsonResponse(true, '', ['editors' => $editors]);
} catch (PDOException $e) {
    jsonResponse(false, 'DB error: ' . $e->getMessage());
}