<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$file = __DIR__ . '/admin/posts.json';

if (!file_exists($file)) {
  http_response_code(404);
  echo json_encode(['error' => 'File not found']);
  exit;
}

$data = file_get_contents($file);
$manualNews = json_decode($data, true);

if (json_last_error() !== JSON_ERROR_NONE) {
  http_response_code(500);
  echo json_encode([
    'error' => 'JSON decode failed',
    'message' => json_last_error_msg()
  ]);
  exit;
}

// Add unique IDs if missing
foreach ($manualNews as $i => &$post) {
  if (!isset($post['id'])) {
    $post['id'] = 'm' . $i;
  }
}

header('Content-Type: application/json');
echo json_encode($manualNews, JSON_PRETTY_PRINT);
