<?php

if (PHP_SAPI !== 'cli') {
    http_response_code(404);
    exit;
}

$failures = [];
if (PHP_VERSION_ID < 80300 || PHP_VERSION_ID >= 80400) {
    $failures[] = 'Use PHP 8.3.x. Detected: ' . PHP_VERSION;
}

foreach (['mysqli', 'fileinfo', 'gd', 'mbstring', 'iconv', 'zlib', 'openssl', 'session', 'json'] as $extension) {
    if (!extension_loaded($extension)) {
        $failures[] = 'Missing PHP extension: ' . $extension;
    }
}

foreach ([__DIR__ . '/admin/employeepics', __DIR__ . '/admin/product_wording'] as $directory) {
    if (!is_dir($directory) || !is_writable($directory)) {
        $failures[] = 'Directory is not writable: ' . $directory;
    }
}

if ($failures !== []) {
    fwrite(STDERR, "Preflight failed:\n- " . implode("\n- ", $failures) . "\n");
    exit(1);
}

fwrite(STDOUT, "PHP 8.3 preflight passed.\n");
