<?php
// get_content.php
// Egy adott oldal mentett tartalmának visszaadása

header('Content-Type: application/json; charset=utf-8');

$page = isset($_GET['page']) ? trim($_GET['page']) : '';

if ($page === '') {
    http_response_code(400);
    echo json_encode([
        'success' => false,
        'error'   => 'Hiányzik a page paraméter.'
    ]);
    exit;
}

$filePath = __DIR__ . '/contents.json';

if (!file_exists($filePath)) {
    // Nincs még mentett tartalom, üres választ adunk
    echo json_encode([
        'success' => true,
        'exists'  => false,
        'data'    => null
    ]);
    exit;
}

$json = file_get_contents($filePath);
$contents = json_decode($json, true);

if (!is_array($contents) || !isset($contents[$page])) {
    echo json_encode([
        'success' => true,
        'exists'  => false,
        'data'    => null
    ]);
    exit;
}

echo json_encode([
    'success' => true,
    'exists'  => true,
    'data'    => $contents[$page]
]);
