<?php

declare(strict_types=1);

require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/mvc.php';
require_once __DIR__ . '/includes/auth.php';
require_once __DIR__ . '/includes/api_service.php';

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

$checks = [];
$loggedIn = auth_check();

function live_check(string $label, bool $ok, string $detail): void
{
    global $checks;
    $checks[] = ['label' => $label, 'ok' => $ok, 'detail' => $detail];
}

live_check('PHP cURL', function_exists('curl_init'), function_exists('curl_init') ? 'enabled' : 'MISSING — enable curl in php.ini');
live_check('API enabled', api_is_configured(), 'base_url=' . (api_config()['base_url'] ?? ''));
live_check('Auth enabled', api_auth_enabled(), api_auth_enabled() ? 'ON' : 'OFF');
live_check('Admin session token', $loggedIn, $loggedIn ? 'Bearer token present' : 'Login required — open /admin/login/');

if ($loggedIn) {
    foreach (['partner', 'website', 'pos'] as $type) {
        try {
            $count = count(api_fetch_partner_contracts($type));
            live_check("GET contracts (type={$type})", true, "count={$count}");
        } catch (Throwable $e) {
            live_check("GET contracts (type={$type})", false, api_error_message($e));
        }
    }

    live_check('Customer sign API', api_feature_enabled('sign_submit'), api_feature_enabled('sign_submit') ? 'ON' : 'OFF');
    live_check('PDF viewed API', api_feature_enabled('pdf_viewed'), api_feature_enabled('pdf_viewed') ? 'ON' : 'OFF');
    live_check('Link access API', api_feature_enabled('link_access'), api_feature_enabled('link_access') ? 'ON' : 'OFF');
}

$allOk = $checks !== [] && !in_array(false, array_column($checks, 'ok'), true);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Karvi Live API Check</title>
    <style>
        body{font-family:system-ui,sans-serif;margin:2rem;background:#f8fafc}
        table{width:100%;border-collapse:collapse;background:#fff;border-radius:12px;overflow:hidden}
        th,td{padding:12px;border-bottom:1px solid #e2e8f0;text-align:left;vertical-align:top}
        th{background:#0f172a;color:#fff}
        .ok{color:#15803d;font-weight:600}.fail{color:#b91c1c;font-weight:600}
        .banner{padding:12px 16px;border-radius:10px;margin-bottom:1rem}
        .banner.ok{background:#dcfce7;color:#166534}.banner.fail{background:#fee2e2;color:#991b1b}
        a{color:#2563eb}
    </style>
</head>
<body>
<h1>Karvi Live API Check</h1>
<p><a href="health.php">health.php</a> · <a href="<?= htmlspecialchars(app_path('/admin/login/')) ?>">Admin Login</a></p>
<div class="banner <?= $allOk ? 'ok' : 'fail' ?>">
    <?= $allOk ? 'All checks passed — APIs ready for live use.' : 'Some checks failed — login first or fix server config.' ?>
</div>
<table>
    <tr><th>Check</th><th>Status</th><th>Detail</th></tr>
    <?php foreach ($checks as $c): ?>
    <tr>
        <td><?= htmlspecialchars($c['label']) ?></td>
        <td class="<?= $c['ok'] ? 'ok' : 'fail' ?>"><?= $c['ok'] ? 'OK' : 'FAIL' ?></td>
        <td><?= htmlspecialchars($c['detail']) ?></td>
    </tr>
    <?php endforeach; ?>
</table>
<p style="color:#64748b;margin-top:1.5rem">
    Live flow: Login → Create contract → Copy customer link → Sign / PDF viewed syncs to karviapi1.de
</p>
</body>
</html>
