<?php
require_once __DIR__ . '/../includes/bootstrap.php';
require_once __DIR__ . '/../includes/entity.php';

if ($_SERVER['REQUEST_METHOD'] !== 'GET') json_error('Método no permitido.', 405);

$id = isset($_GET['id']) ? (int) $_GET['id'] : null;
$slug = isset($_GET['slug']) ? (string) $_GET['slug'] : null;

if ($id !== null) {
    require_admin($config);
    $post = entity_get_by_id($pdo, 'posts', $id);
} elseif ($slug !== null && $slug !== '') {
    $isAdmin = current_admin($config) !== null;
    $post = entity_get_by_slug($pdo, 'posts', $slug, entity_lang($_GET['lang'] ?? null), $isAdmin);
} else {
    json_error('Debes indicar id o slug.', 422);
}

if (!$post) json_error('No encontrado.', 404);

json_ok(['post' => $post]);
