<?php
declare(strict_types=1);
header('Content-Type: application/json; charset=utf-8');
require_once __DIR__ . '/../admin/db.php';

$id = (int)($_GET['ruleta_id'] ?? 1);

$stmt = $conn->prepare("SELECT
  imagen_ruleta, imagen_puntero,
  banner_1, banner_2, banner_3, banner_4,
  background_image, fullscreen_icon
  FROM ruletas WHERE id=? LIMIT 1");
$stmt->bind_param("i", $id);
$stmt->execute();
$res = $stmt->get_result();

if (!$row = $res->fetch_assoc()) {
  http_response_code(404);
  echo json_encode(['error' => 'Ruleta no encontrada']);
  exit;
}

function norm(?string $p): ?string {
  if (!$p) return null;
  $p = str_replace('\\', '/', $p);
  if ($p[0] !== '/') $p = '/' . ltrim($p, '/');
  return $p;
}
$docroot = rtrim($_SERVER['DOCUMENT_ROOT'] ?? '', '/');

$fields = [
  'imagen_ruleta','imagen_puntero',
  'banner_1','banner_2','banner_3','banner_4',
  'background_image','fullscreen_icon'
];
$out = [];
foreach ($fields as $f) {
  $v = norm($row[$f] ?? null);
  $out[$f] = ($v && file_exists($docroot . $v)) ? $v : null;
}

echo json_encode($out);
