<?php

declare(strict_types=1);

header('Content-Type: text/html; charset=utf-8');

$root = __DIR__;
$checks = [];

$checks[] = [
    'label' => 'PHP version (8.0+)',
    'ok' => version_compare(PHP_VERSION, '8.0.0', '>='),
    'detail' => PHP_VERSION,
];

$checks[] = [
    'label' => 'vendor/autoload.php',
    'ok' => is_file($root . '/vendor/autoload.php'),
    'detail' => is_file($root . '/vendor/autoload.php') ? 'found' : 'missing — upload vendor/ from zip',
];

$checks[] = [
    'label' => 'index.php',
    'ok' => is_file($root . '/index.php'),
    'detail' => 'entry point',
];

$checks[] = [
    'label' => 'router.php',
    'ok' => is_file($root . '/router.php'),
    'detail' => 'router',
];

$checks[] = [
    'label' => 'config/config.php',
    'ok' => is_file($root . '/config/config.php'),
    'detail' => 'config',
];

$writable = ['uploads/contracts', 'uploads/signatures', 'uploads/signed', 'database'];
foreach ($writable as $dir) {
    $path = $root . '/' . $dir;
    if (!is_dir($path)) {
        @mkdir($path, 0755, true);
    }
    $checks[] = [
        'label' => $dir . ' writable',
        'ok' => is_dir($path) && is_writable($path),
        'detail' => is_dir($path) ? (is_writable($path) ? 'ok' : 'chmod 755/775 required') : 'folder missing',
    ];
}

$exts = ['gd', 'mbstring', 'curl', 'openssl'];
foreach ($exts as $ext) {
    $checks[] = [
        'label' => 'PHP extension: ' . $ext,
        'ok' => extension_loaded($ext),
        'detail' => extension_loaded($ext) ? 'loaded' : 'missing',
    ];
}

$scriptName = str_replace('\\', '/', (string)($_SERVER['SCRIPT_NAME'] ?? ''));
$basePath = rtrim(str_replace('\\', '/', dirname($scriptName)), '/');
if ($basePath === '/' || $basePath === '.') {
    $basePath = '';
}

$checks[] = [
    'label' => 'Detected base path',
    'ok' => true,
    'detail' => $basePath !== '' ? $basePath : '/ (root)',
];

$allOk = !in_array(false, array_column($checks, 'ok'), true);
?>
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Karvi — Server Check</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 720px; margin: 2rem auto; padding: 0 1rem; }
        h1 { font-size: 1.4rem; }
        table { width: 100%; border-collapse: collapse; margin: 1rem 0; }
        td, th { border: 1px solid #ddd; padding: 8px 10px; text-align: left; }
        .ok { color: #0a7; font-weight: bold; }
        .bad { color: #c00; font-weight: bold; }
        .note { background: #fff8e8; border: 1px solid #ecdca0; padding: 12px; border-radius: 8px; }
    </style>
</head>
<body>
    <h1>Karvi Partner Contract — Server Check</h1>
    <p>Status: <span class="<?= $allOk ? 'ok' : 'bad' ?>"><?= $allOk ? 'OK — ready' : 'Fix issues below' ?></span></p>

    <table>
        <tr><th>Check</th><th>Result</th><th>Detail</th></tr>
        <?php foreach ($checks as $row): ?>
        <tr>
            <td><?= htmlspecialchars($row['label'], ENT_QUOTES, 'UTF-8') ?></td>
            <td class="<?= $row['ok'] ? 'ok' : 'bad' ?>"><?= $row['ok'] ? 'OK' : 'FAIL' ?></td>
            <td><?= htmlspecialchars((string)$row['detail'], ENT_QUOTES, 'UTF-8') ?></td>
        </tr>
        <?php endforeach; ?>
    </table>

    <div class="note">
        <strong>Subfolder install?</strong> In <code>.htaccess</code> set:<br>
        <code>RewriteBase <?= htmlspecialchars($basePath !== '' ? $basePath . '/' : '/your-subfolder/', ENT_QUOTES, 'UTF-8') ?></code>
    </div>

    <p><a href="index.php">Open app</a> &nbsp;|&nbsp; <a href="install.php">Run install.php</a> &nbsp;|&nbsp; <a href="index.php?debug=1">Debug</a></p>
    <p><small>Delete check.php after setup.</small></p>
</body>
</html>
