<?php
// Diagnose-pagina — VERWIJDEREN na gebruik.
// Doel: laat zien of PHP de data correct leest op de server.

error_reporting(E_ALL);
ini_set('display_errors', '1');

$here = __DIR__;
echo "<!doctype html><meta charset='utf-8'><title>Site check</title>";
echo "<style>body{font-family:ui-monospace,monospace;background:#111;color:#eee;padding:24px;line-height:1.6}h2{color:#c97e3f;margin-top:24px;font-family:Georgia,serif}.ok{color:#7fc97f}.err{color:#ff7f6e}pre{background:#222;padding:14px;border-left:3px solid #c97e3f;overflow:auto}</style>";

echo "<h1>Site diagnose</h1>";

echo "<h2>1. PHP info</h2>";
echo "PHP versie: <b>" . phpversion() . "</b><br>";
echo "Document root: " . ($_SERVER['DOCUMENT_ROOT'] ?? 'onbekend') . "<br>";
echo "__DIR__: $here<br>";

echo "<h2>2. Bestanden aanwezig?</h2>";
$paths = [
  'data/designs.json'      => "$here/data/designs.json",
  'data/te-koop.json'      => "$here/data/te-koop.json",
  'includes/config.php'    => "$here/includes/config.php",
  'includes/data.php'      => "$here/includes/data.php",
  'includes/i18n.php'      => "$here/includes/i18n.php",
  'images/designs/ map'    => "$here/images/designs",
];
foreach ($paths as $name => $p) {
  $exists = file_exists($p);
  $rd     = $exists ? is_readable($p) : false;
  $size   = $exists && is_file($p) ? filesize($p) : '';
  echo $exists && $rd
    ? "<span class='ok'>✓</span> $name — $size bytes<br>"
    : "<span class='err'>✗</span> $name — NIET leesbaar<br>";
}

echo "<h2>3. designs.json laden</h2>";
$jsonPath = "$here/data/designs.json";
if (!file_exists($jsonPath)) {
  echo "<span class='err'>data/designs.json bestaat niet — uploaden!</span>";
} else {
  $raw = file_get_contents($jsonPath);
  $arr = json_decode($raw, true);
  if (json_last_error() !== JSON_ERROR_NONE) {
    echo "<span class='err'>JSON parse error: " . json_last_error_msg() . "</span>";
  } else {
    echo "<span class='ok'>✓</span> JSON geparsed: " . count($arr) . " ontwerpen<br>";
    echo "Eerste ontwerp: <code>" . htmlspecialchars($arr[0]['title'] ?? '?') . "</code> met "
       . count($arr[0]['images'] ?? []) . " foto's";
  }
}

echo "<h2>4. data.php helper-functies</h2>";
if (file_exists("$here/includes/data.php")) {
  require_once "$here/includes/data.php";
  $designs = load_designs();
  echo "<span class='ok'>✓</span> load_designs() → " . count($designs) . " ontwerpen<br>";
  $groups = group_by_series($designs);
  echo "<span class='ok'>✓</span> group_by_series() → " . count($groups) . " series<br><br>";
  echo "Series met hun slug en modellen-aantal:<br><pre>";
  $i = 0;
  foreach ($groups as $g) {
    echo str_pad($g['name'], 24) . " slug=" . str_pad($g['slug'], 18) . " " . count($g['models']) . " modellen\n";
    if (++$i >= 10) break;
  }
  echo "...</pre>";

  echo "<h3>Test: find_serie_by_slug('noordkaper')</h3>";
  $s = find_serie_by_slug('noordkaper');
  if ($s) {
    echo "<span class='ok'>✓</span> Serie gevonden: <b>" . htmlspecialchars($s['name']) . "</b> met "
       . count($s['models']) . " modellen<br>";
    echo "Eerste 3 modellen:<pre>";
    foreach (array_slice($s['models'], 0, 3) as $m) {
      echo "  ID {$m['id']}: {$m['title']} (lengte: " . ($m['length_m'] ?? '?') . " m)\n";
    }
    echo "</pre>";
  } else {
    echo "<span class='err'>✗ Geen serie 'noordkaper' gevonden</span>";
  }
}

echo "<h2>5. Foto-paden</h2>";
$d1 = $designs[0] ?? null;
if ($d1 && !empty($d1['images'])) {
  $imgPath = "$here/images/designs/" . $d1['id'] . "/" . $d1['images'][0];
  $exists = file_exists($imgPath);
  echo $exists
    ? "<span class='ok'>✓</span> Eerste foto bestaat: $imgPath"
    : "<span class='err'>✗</span> Eerste foto NIET gevonden: $imgPath";
}

echo "<h2>6. Schrijfrechten</h2>";
foreach (['data', 'uploads'] as $dir) {
  $p = "$here/$dir";
  $w = is_writable($p);
  echo $w
    ? "<span class='ok'>✓</span> $dir/ is schrijfbaar<br>"
    : "<span class='err'>✗</span> $dir/ is NIET schrijfbaar — admin-paneel werkt niet<br>";
}

echo "<hr style='margin-top:30px;border-color:#444'><p style='color:#888'>Verwijder dit bestand na de test.</p>";
