<?php
// ============================================================
//  ajax/get_specialties.php  —  Specialties for a journal
// ============================================================
require_once __DIR__ . '/../config/db.php';

header('Content-Type: application/json');

$journalId = (int)($_GET['journal_id'] ?? 0);
if (!$journalId) jsonResponse(false, 'Journal ID required');

try {
    $stmt = db()->prepare("SELECT * FROM journals WHERE journal_id = ?");
    $stmt->execute([$journalId]);
    $journal = $stmt->fetch();
    if (!$journal) jsonResponse(false, 'Journal not found');

    $stmt = db()->prepare(
        "SELECT specialty_id, name, description, about_url, editorial_board_url
         FROM specialties WHERE journal_id = ? AND is_active = 1 ORDER BY name"
    );
    $stmt->execute([$journalId]);
    $specialties = $stmt->fetchAll();

    jsonResponse(true, '', [
        'journal'     => $journal,
        'specialties' => $specialties,
    ]);
} catch (PDOException $e) {
    jsonResponse(false, 'DB error: ' . $e->getMessage());
}