<?php
declare(strict_types=1);
if (session_status() === PHP_SESSION_NONE) session_start();
@header('Content-Type: text/html; charset=utf-8');

$root = realpath(dirname(__DIR__,2)); // /elms
$gamesDir = $root . '/uploads/games';
$gamesUrl = '/elms/uploads/games';
$tests = [];

function ok($b){ return $b? '✅' : '❌'; }
function row($k,$v){ echo "<tr><th style='text-align:left;padding:6px 10px;background:#f6f8ff;width:260px'>$k</th><td style='padding:6px 10px'>$v</td></tr>"; }
function write_test($gamesDir){
  $name='diagnostic_'.date('Ymd_His').'.html';
  $file=$gamesDir.'/'.$name;
  $html="<!doctype html><meta charset='utf-8'><title>OK</title><h1>Game Diagnostics OK</h1><p>If you can see this in browser as HTML (not download), web serving is fine.</p>";
  $ok=@file_put_contents($file,$html)!==false;
  return [$ok,$name,$file];
}

$phpver = PHP_VERSION;
$tests['PHP >= 7.4'] = version_compare(PHP_VERSION,'7.4.0','>=');

// Directories
$rootOk = is_dir($root);
$gamesExist = is_dir($gamesDir) || @mkdir($gamesDir, 0775, true);
$gamesWritable = is_writable($gamesDir);

// Try write test file
list($writeOk,$fname,$fpath) = write_test($gamesDir);

// Compose URL guess
$guessUrl = rtrim($gamesUrl,'/').'/'.$fname;

// Basic HTML
?><!doctype html>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Games Diagnostics</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<div class="container py-4">
  <h3>ELMS Games Diagnostics</h3>
  <p class="text-muted">Run this once and follow the fixes below.</p>

  <table class="table table-sm table-bordered w-auto">
    <?php row('PHP Version', htmlspecialchars($phpver).' '.ok($tests['PHP >= 7.4'])); ?>
    <?php row('ELMS root folder', htmlspecialchars($root).' '.ok($rootOk)); ?>
    <?php row('/uploads/games exists', htmlspecialchars($gamesDir).' '.ok($gamesExist)); ?>
    <?php row('/uploads/games writable', ok($gamesWritable)); ?>
    <?php row('Wrote test file', htmlspecialchars($fpath).' '.ok($writeOk)); ?>
    <?php row('Open test file URL', '<a target="_blank" href="'.htmlspecialchars($guessUrl).'">'.htmlspecialchars($guessUrl).'</a>'); ?>
  </table>

  <h5>Interpretation</h5>
  <ol>
    <li><b>If the test URL downloads</b> instead of opening, add <code>.htaccess</code> below to <code>/elms/uploads/games/</code>.</li>
    <li><b>If the test URL is 404</b>, your base path is different. Update the game URLs to match your actual web path to <code>/uploads/games</code>.</li>
    <li><b>If write failed or not writable</b>, fix ownership/permissions: <code>chown -R www-data:www-data /elms/uploads/games</code> then <code>chmod -R 775</code>.</li>
  </ol>

  <h5>.htaccess (must exist in /elms/uploads/games)</h5>
  <pre>Options -Indexes
AddDefaultCharset utf-8
AddType text/html .html
Header set X-Content-Type-Options "nosniff"</pre>

  <h5>Super quick data sanity checks</h5>
  <ul>
    <li><b>Word Search</b>: every row same length; each word actually appears in the grid.</li>
    <li><b>Crossword</b>: <code>rows</code> and <code>solution</code> same size; <code>rows</code> uses <code>#</code> for blocks, <code>.</code> for empties; <code>solution</code> uses letters and <code>#</code> (no <code>.</code>).</li>
  </ul>

  <p class="mt-4 text-muted">If you still get a blank page, check the browser console (F12) and paste the first error to me.</p>
</div>
