<?php
/**
 * Shared "Bruk min sak som kontekst" toggle for tool forms.
 *
 * Renders nothing for free / unauthenticated / CaveauAI sessions.
 * For paid (Plus / Pro / active trial) users, renders a checkbox that defaults
 * to checked when they have any indexed case documents.
 *
 * The companion JS exposes `window.dbnGetUseMyCase()` for each tool's JS to call
 * when assembling its request payload — no per-tool plumbing beyond one read.
 *
 * Usage:
 *   <?php require_once __DIR__ . '/includes/case_toggle.php'; ?>
 * (Place inside the tool form, before the submit button.)
 *
 * Endpoint side: read `$input['use_my_case']` — already supported across the
 * five wired tools (korrespond, advocate/deep-research, barnevernet, discrepancy, timeline).
 */

if (!function_exists('dbnToolsIsAuthenticated')) {
    require_once __DIR__ . '/bootstrap.php';
}
require_once __DIR__ . '/FreeTier.php';
require_once __DIR__ . '/CaseStore.php';

$__caseToggleUserId = 0;
$__caseToggleEnabled = false;
$__caseToggleDocCount = 0;

if (dbnToolsIsAuthenticated() && dbnToolsIsFreeTier()) {
    $__caseToggleUserId = (int)($_SESSION['dbn_tools_sso_uid'] ?? 0);
    if ($__caseToggleUserId > 0) {
        $__caseToggleTier = FreeTier::tier($__caseToggleUserId);
        if (FreeTier::isPaidTier($__caseToggleTier)) {
            $__caseToggleEnabled = true;
            try {
                $__caseToggleOwnerId = CaseStore::caseResolveClientId($__caseToggleUserId);
                $__caseToggleDocCount = count(CaseStore::listDocs($__caseToggleOwnerId));
            } catch (Throwable $e) {
                $__caseToggleDocCount = 0;
            }
        }
    }
}

if (!$__caseToggleEnabled) {
    // Free / CaveauAI / unauthenticated — emit a no-op JS shim so tool code can call it safely.
    echo '<script>window.dbnGetUseMyCase = function () { return false; };</script>';
    return;
}

$defaultChecked = $__caseToggleDocCount > 0 ? 'checked' : '';
$docCountLabel = $__caseToggleDocCount === 1
    ? '1 dokument'
    : ($__caseToggleDocCount . ' dokumenter');
?>
<div class="control-row case-context-toggle" id="caseContextRow"
     style="margin: 0.75rem 0; padding: 0.85rem 1rem; background: #f0f7ff;
            border-left: 3px solid #00205B; border-radius: 6px; display: flex;
            align-items: center; gap: 0.7rem;">
    <label style="display: flex; align-items: center; gap: 0.55rem; cursor: pointer; flex: 1; margin: 0;">
        <input type="checkbox" id="useMyCaseToggle" name="use_my_case" value="1" <?= $defaultChecked ?>
               style="width: 18px; height: 18px; accent-color: #00205B;">
        <span style="font-weight: 600; color: #00205B;">Bruk min sak som kontekst</span>
        <span style="color: #6b7280; font-size: 0.85rem;">
            (<?= htmlspecialchars($docCountLabel) ?>)
        </span>
    </label>
    <a href="/min-sak.php" style="color: #00205B; font-size: 0.85rem; text-decoration: none; white-space: nowrap;">
        Min sak →
    </a>
</div>
<script>
(function () {
    var el = document.getElementById('useMyCaseToggle');
    window.dbnGetUseMyCase = function () {
        return el ? !!el.checked : false;
    };
})();
</script>
<?php
unset($__caseToggleUserId, $__caseToggleEnabled, $__caseToggleDocCount, $__caseToggleTier, $__caseToggleOwnerId);
?>
