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

require_once "index.php";


session_start();
header('Content-Type: application/json');

spl_autoload_register(function($c){
    $p = __DIR__ . "/classes/$c.php";
    if (file_exists($p)) require_once $p;
});

$out = [];

/* DB CHECK */
try {
    $db = DB::get();
    $out['db'] = "OK";
} catch (Exception $e) {
    $out['db'] = "FAIL: " . $e->getMessage();
}

/* CLASS CHECK */
$classes = ['DB','User','Image','Rating','Tag','Collection'];
foreach ($classes as $c) {
    $out['class_'.$c] = class_exists($c) ? "OK" : "MISSING";
}

/* FOLDER CHECK */
$folders = ['uploads','api','classes','public'];
foreach ($folders as $f) {
    $out['folder_'.$f] = is_dir(__DIR__ . "/$f") ? "OK" : "MISSING";
}

/* FILE WRITE CHECK */
$testFile = __DIR__ . "/uploads/test_write.txt";
$write = @file_put_contents($testFile, "test");
$out['write_uploads'] = $write !== false ? "OK" : "FAIL";
if ($write !== false) unlink($testFile);

/* SESSION CHECK */
$out['session'] = isset($_SESSION) ? "OK" : "FAIL";

/* API CHECK (basic existence) */
$apiFiles = ['upload','receive','rate','tag','collection'];
foreach ($apiFiles as $f) {
    $path = __DIR__ . "/api/$f.php";
    $out['api_'.$f] = file_exists($path) ? "OK" : "MISSING";
}

echo json_encode($out, JSON_PRETTY_PRINT);