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

$topic = isset($_GET['topic']) ? (string)$_GET['topic'] : '';

if (!preg_match('/^[a-z0-9_]+$/', $topic)) {
    http_response_code(400);
    echo json_encode(['error' => 'Invalid help topic.']);
    exit;
}

$file = __DIR__ . '/topics/' . $topic . '.json';

if (!is_file($file)) {
    http_response_code(404);
    echo json_encode(['error' => 'Help content is not available for this section.']);
    exit;
}

$content = file_get_contents($file);
$decoded = json_decode($content, true);

if (!is_array($decoded) || empty($decoded['title']) || (!isset($decoded['text']) && !isset($decoded['body']))) {
    http_response_code(500);
    echo json_encode(['error' => 'This help topic is not configured correctly.']);
    exit;
}

echo json_encode($decoded, JSON_UNESCAPED_SLASHES);
