<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/FreeTier.php';
require_once __DIR__ . '/includes/CaseStore.php';
require_once __DIR__ . '/includes/CaseResults.php';

dbnToolsRequirePageAuth($_SERVER['REQUEST_URI'] ?? '/');

$uiLang = dbnToolsCurrentLanguage();
$userId = (int)($_SESSION['dbn_tools_sso_uid'] ?? 0);
if ($userId <= 0) {
    header('Location: /dashboard.php');
    exit;
}

$id = (int)($_GET['id'] ?? 0);
$result = $id > 0 ? CaseResults::get($userId, $id) : null;

if (!$result) {
    http_response_code(404);
    ?><!doctype html><html lang="<?= htmlspecialchars($uiLang) ?>"><head>
    <meta charset="utf-8"><title>Ikke funnet — Min Sak</title>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Crimson+Pro:wght@400;700&family=IBM+Plex+Sans:wght@400;500;600&display=swap">
    <link rel="stylesheet" href="assets/css/tools.css">
    <link rel="stylesheet" href="assets/css/dbn-tools-redesign.css">
    </head><body><main style="max-width:720px;margin:4rem auto;padding:0 1.5rem;text-align:center;font-family:'IBM Plex Sans',sans-serif;">
        <h1 style="font-family:'Crimson Pro',serif;color:#00205B;">Analysen finnes ikke</h1>
        <p>Den lagrede analysen ble ikke funnet, eller du har ikke tilgang til den.</p>
        <p><a href="/min-sak.php" style="color:#00205B;font-weight:600;">← Tilbake til Min Sak</a></p>
    </main></body></html><?php
    exit;
}

$toolSlug   = (string)$result['tool'];
$toolLabel  = CaseResults::toolLabel($toolSlug);
$toolIcon   = CaseResults::toolIcon($toolSlug);
$title      = (string)($result['title'] ?? $toolLabel);
$createdAt  = (string)$result['created_at'];
$usedCase   = !empty($result['used_case_context']);
$caseDocIds = is_array($result['case_doc_ids'] ?? null) ? $result['case_doc_ids'] : [];
$input      = is_array($result['input_payload'] ?? null) ? $result['input_payload'] : [];
$output     = is_array($result['output_payload'] ?? null) ? $result['output_payload'] : [];

// Look up doc filenames for the chunks that contributed
$caseDocs = [];
if (!empty($caseDocIds)) {
    try {
        $ownerId = CaseStore::caseResolveClientId($userId);
        $allDocs = CaseStore::listDocs($ownerId);
        $byId    = [];
        foreach ($allDocs as $d) {
            $byId[(int)$d['id']] = $d;
        }
        foreach ($caseDocIds as $docId) {
            if (isset($byId[(int)$docId])) {
                $caseDocs[] = $byId[(int)$docId];
            }
        }
    } catch (Throwable $e) {
        $caseDocs = [];
    }
}

// Best-effort extraction of the primary human-readable output (fallback for unknown tools)
$primaryOutput = '';
foreach (['draft', 'response', 'answer', 'what_we_found', 'text', 'transcript', 'redacted_text', 'summary', 'markdown'] as $k) {
    if (!empty($output[$k]) && is_string($output[$k])) {
        $primaryOutput = (string)$output[$k];
        break;
    }
}

/**
 * Render a list of strings as an unordered list within a collapsible block.
 */
function crListBlock(string $heading, array $items): string {
    if (empty($items)) return '';
    $lis = implode('', array_map(fn($v) => '<li>' . htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8') . '</li>', $items));
    return '<h3 style="font-family:\'Crimson Pro\',serif;font-size:1.05rem;color:#00205B;margin:1rem 0 0.35rem;">'
        . htmlspecialchars($heading, ENT_QUOTES, 'UTF-8')
        . '</h3><ul style="margin:0 0 0.5rem;padding-left:1.3rem;line-height:1.6;">' . $lis . '</ul>';
}

function crField(string $label, string $value): string {
    if ($value === '') return '';
    return '<p><strong>' . htmlspecialchars($label, ENT_QUOTES, 'UTF-8') . ':</strong> '
        . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '</p>';
}
?><!doctype html>
<html lang="<?= htmlspecialchars($uiLang) ?>">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= htmlspecialchars($title) ?> — Min Sak</title>
    <meta name="robots" content="noindex, nofollow">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Crimson+Pro:wght@400;700&family=IBM+Plex+Sans:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap">
    <link rel="stylesheet" href="assets/css/tools.css">
    <link rel="stylesheet" href="assets/css/dbn-tools-redesign.css">
    <style>
        .cr-shell { max-width: 980px; margin: 0 auto; padding: 2rem 1.5rem 4rem; font-family: 'IBM Plex Sans', sans-serif; }
        .cr-header { display: flex; align-items: flex-start; gap: 1rem; margin-bottom: 1.25rem; }
        .cr-icon { font-size: 2.4rem; line-height: 1; }
        .cr-title-row { flex: 1; min-width: 0; }
        .cr-title { font-family: 'Crimson Pro', serif; font-size: 1.9rem; margin: 0; color: #00205B; cursor: pointer; }
        .cr-title[contenteditable="true"]:focus { outline: 2px solid #00205B; outline-offset: 4px; }
        .cr-meta { color: #6b7280; margin: 0.4rem 0 0; font-size: 0.9rem; }
        .cr-tag { display: inline-block; background: #dbeafe; color: #1e3a8a; padding: 1px 8px; border-radius: 999px; font-size: 0.75rem; font-weight: 600; }
        .cr-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; margin: 1.25rem 0 2rem; }
        .cr-btn { padding: 8px 14px; border-radius: 6px; font-size: 0.9rem; font-weight: 600; border: 1px solid #00205B; background: #fff; color: #00205B; cursor: pointer; text-decoration: none; display: inline-block; }
        .cr-btn-primary { background: #00205B; color: #fff; }
        .cr-btn-danger  { border-color: #b91c1c; color: #b91c1c; }
        .cr-section { background: #fff; border: 1px solid #e5e7eb; border-radius: 10px; padding: 1.5rem; margin-bottom: 1.25rem; }
        .cr-section h2 { font-family: 'Crimson Pro', serif; margin: 0 0 0.75rem; font-size: 1.3rem; color: #00205B; }
        .cr-output { white-space: pre-wrap; line-height: 1.6; color: #1f2937; font-size: 1rem; }
        .cr-output-empty { color: #6b7280; font-style: italic; }
        .cr-docs { display: grid; grid-template-columns: 1fr; gap: 0.5rem; }
        .cr-doc { padding: 0.65rem 0.85rem; background: #f9fafb; border-radius: 6px; display: flex; align-items: center; gap: 0.6rem; }
        .cr-doc-name { flex: 1; font-weight: 500; color: #1f2937; }
        details.cr-collapse > summary { cursor: pointer; color: #00205B; font-weight: 600; padding: 0.5rem 0; }
        details.cr-collapse pre { background: #f3f4f6; padding: 1rem; border-radius: 6px; overflow-x: auto; font-family: 'JetBrains Mono', monospace; font-size: 0.85rem; max-height: 400px; }
    </style>
</head>
<body>
<main class="cr-shell">
    <p style="margin:0 0 1rem;color:#6b7280;font-size:0.85rem;text-transform:uppercase;letter-spacing:0.06em;">
        <a href="/min-sak.php" style="color:inherit;">← Min Sak</a>
    </p>

    <header class="cr-header">
        <div class="cr-icon"><?= htmlspecialchars($toolIcon) ?></div>
        <div class="cr-title-row">
            <h1 class="cr-title" id="crTitle" contenteditable="false" data-id="<?= (int)$result['id'] ?>">
                <?= htmlspecialchars($title) ?>
            </h1>
            <p class="cr-meta">
                <?= htmlspecialchars($toolLabel) ?>
                · <?= htmlspecialchars(date('j. F Y, H:i', strtotime($createdAt))) ?>
                <?php if ($usedCase): ?>
                    · <span class="cr-tag">Bruk min sak</span>
                <?php endif; ?>
                <?php if (!empty($result['model'])): ?>
                    · <span style="font-family:'JetBrains Mono',monospace;font-size:0.8rem;color:#6b7280;"><?= htmlspecialchars((string)$result['model']) ?></span>
                <?php endif; ?>
            </p>
        </div>
    </header>

    <div class="cr-actions">
        <button type="button" class="cr-btn cr-btn-primary" id="crRerun" data-tool="<?= htmlspecialchars($toolSlug) ?>" data-id="<?= (int)$result['id'] ?>">Kjør på nytt</button>
        <button type="button" class="cr-btn" id="crRename">Endre navn</button>
        <a class="cr-btn" href="/api/case/result-export.php?id=<?= (int)$result['id'] ?>" download>Eksporter JSON</a>
        <button type="button" class="cr-btn cr-btn-danger" id="crDelete" data-id="<?= (int)$result['id'] ?>">Slett</button>
    </div>

    <?php if (!empty($caseDocs)): ?>
    <section class="cr-section">
        <h2>Saksdokumenter brukt</h2>
        <div class="cr-docs">
            <?php foreach ($caseDocs as $d): ?>
                <div class="cr-doc">
                    <div>📄</div>
                    <div class="cr-doc-name"><?= htmlspecialchars((string)$d['filename']) ?></div>
                    <div style="color:#6b7280;font-size:0.85rem;">
                        <?php if (!empty($d['page_count'])): ?><?= (int)$d['page_count'] ?> sider<?php endif; ?>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </section>
    <?php endif; ?>

    <section class="cr-section">
        <h2>Resultat</h2>

        <?php if ($toolSlug === 'summarize'): ?>
            <?php if (!empty($output['what_we_found'])): ?>
                <p style="line-height:1.7;"><?= htmlspecialchars((string)$output['what_we_found']) ?></p>
            <?php endif; ?>
            <?= crListBlock('Key Facts',   (array)($output['key_facts']   ?? [])) ?>
            <?= crListBlock('Dates',       (array)($output['dates']       ?? [])) ?>
            <?= crListBlock('Parties',     (array)($output['parties']     ?? [])) ?>
            <?= crListBlock('Legal References', (array)($output['legal_references_detected'] ?? [])) ?>
            <?= crListBlock('What Remains Uncertain', (array)($output['what_remains_uncertain'] ?? [])) ?>
            <?php if (!empty($output['next_practical_step'])): ?>
                <h3 style="font-family:'Crimson Pro',serif;font-size:1.05rem;color:#00205B;margin:1rem 0 0.35rem;">Next Practical Step</h3>
                <p><?= htmlspecialchars((string)$output['next_practical_step']) ?></p>
            <?php endif; ?>

        <?php elseif ($toolSlug === 'ask'): ?>
            <?php if (!empty($output['answer'])): ?>
                <div class="cr-output" style="margin-bottom:1rem;"><?= htmlspecialchars((string)$output['answer']) ?></div>
            <?php endif; ?>
            <?php if (!empty($output['citation_notes']) && is_array($output['citation_notes'])): ?>
                <h3 style="font-family:'Crimson Pro',serif;font-size:1.05rem;color:#00205B;margin:1rem 0 0.5rem;">Citations</h3>
                <?php foreach ($output['citation_notes'] as $cite): if (!is_array($cite)) continue; ?>
                    <div style="border-left:3px solid #d8dde7;padding:0.4rem 0.75rem;margin-bottom:0.6rem;">
                        <?php if (!empty($cite['title'])): ?><strong><?= htmlspecialchars((string)$cite['title']) ?></strong><br><?php endif; ?>
                        <?php if (!empty($cite['citation'])): ?><em style="font-size:0.85rem;color:#667085;"><?= htmlspecialchars((string)$cite['citation']) ?></em><br><?php endif; ?>
                        <?php if (!empty($cite['why_it_matters'])): ?><span style="font-size:0.9rem;"><?= htmlspecialchars((string)$cite['why_it_matters']) ?></span><?php endif; ?>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
            <?= crListBlock('What Remains Uncertain', (array)($output['what_remains_uncertain'] ?? [])) ?>

        <?php elseif ($toolSlug === 'redact'): ?>
            <?php if (!empty($output['redacted_text'])): ?>
                <pre class="cr-output" style="white-space:pre-wrap;background:#f9fafb;padding:1rem;border-radius:6px;max-height:500px;overflow-y:auto;"><?= htmlspecialchars((string)$output['redacted_text']) ?></pre>
            <?php endif; ?>
            <?php if (!empty($output['entity_counts']) && is_array($output['entity_counts'])): ?>
                <h3 style="font-family:'Crimson Pro',serif;font-size:1.05rem;color:#00205B;margin:1rem 0 0.5rem;">Entities Redacted</h3>
                <table style="border-collapse:collapse;font-size:0.9rem;width:auto;">
                    <?php foreach ($output['entity_counts'] as $cat => $count): ?>
                        <tr>
                            <td style="padding:0.2rem 1rem 0.2rem 0;font-weight:600;"><?= htmlspecialchars((string)$cat) ?></td>
                            <td style="padding:0.2rem 0;color:#374151;"><?= (int)$count ?></td>
                        </tr>
                    <?php endforeach; ?>
                </table>
            <?php endif; ?>
            <?php if (!empty($output['redaction_map'])): ?>
                <details class="cr-collapse" style="margin-top:1rem;">
                    <summary>Redaction Map</summary>
                    <pre><?= htmlspecialchars(json_encode($output['redaction_map'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) ?: '{}') ?></pre>
                </details>
            <?php endif; ?>

        <?php elseif ($toolSlug === 'transcribe'): ?>
            <?php
                $speakerRoles = is_array($output['speaker_roles'] ?? null) ? $output['speaker_roles'] : [];
                $segments     = is_array($output['segments']      ?? null) ? $output['segments']      : [];
            ?>
            <?= crField('Engine',    (string)($output['model']    ?? '')) ?>
            <?= crField('Language',  strtoupper((string)($output['language'] ?? ''))) ?>
            <?php if (!empty($output['duration_sec'])): ?>
                <?php $dur = (int)$output['duration_sec']; $m = intdiv($dur, 60); $s = $dur % 60; ?>
                <?= crField('Duration', ($m > 0 ? "{$m}m " : '') . "{$s}s") ?>
            <?php endif; ?>
            <?php if (!empty($speakerRoles)): ?>
                <p><strong>Speakers:</strong> <?= implode(', ', array_map(
                    fn($id, $role) => htmlspecialchars("$id: $role", ENT_QUOTES, 'UTF-8'),
                    array_keys($speakerRoles), array_values($speakerRoles)
                )) ?></p>
            <?php endif; ?>
            <?php if (!empty($output['transcript'])): ?>
                <div class="cr-output" style="max-height:420px;overflow-y:auto;background:#f9fafb;padding:1rem;border-radius:6px;margin-top:0.75rem;white-space:pre-wrap;"><?= htmlspecialchars((string)$output['transcript']) ?></div>
            <?php endif; ?>
            <?php if (!empty($segments)): ?>
                <details class="cr-collapse" style="margin-top:1rem;">
                    <summary>Segments (<?= count($segments) ?>)</summary>
                    <div style="overflow-x:auto;max-height:400px;overflow-y:auto;">
                    <table style="border-collapse:collapse;font-size:0.83rem;width:100%;">
                        <?php foreach ($segments as $seg): if (!is_array($seg)) continue; ?>
                            <tr style="border-bottom:1px solid #f3f4f6;">
                                <td style="padding:0.3rem 0.7rem 0.3rem 0;white-space:nowrap;color:#6b7280;font-family:'JetBrains Mono',monospace;"><?= number_format((float)($seg['start'] ?? 0), 1) ?>s</td>
                                <?php if (!empty($seg['speaker'])): ?>
                                    <td style="padding:0.3rem 0.7rem;white-space:nowrap;font-weight:600;color:#374151;"><?= htmlspecialchars((string)$seg['speaker']) ?></td>
                                <?php endif; ?>
                                <td style="padding:0.3rem 0;"><?= htmlspecialchars((string)($seg['text'] ?? '')) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </table>
                    </div>
                </details>
            <?php endif; ?>

        <?php elseif ($toolSlug === 'timeline'): ?>
            <?php if (!empty($output['what_we_found'])): ?>
                <p style="line-height:1.7;"><?= htmlspecialchars((string)$output['what_we_found']) ?></p>
            <?php endif; ?>
            <?php if (!empty($output['events']) && is_array($output['events'])): ?>
                <div style="overflow-x:auto;margin-top:0.75rem;">
                <table style="border-collapse:collapse;width:100%;font-size:0.88rem;">
                    <thead><tr style="border-bottom:2px solid #e5e7eb;text-align:left;">
                        <th style="padding:0.4rem 0.75rem 0.4rem 0;">Date</th>
                        <th style="padding:0.4rem 0.75rem;">Actor</th>
                        <th style="padding:0.4rem 0.75rem;">Event</th>
                        <th style="padding:0.4rem 0 0.4rem 0.75rem;">Confidence</th>
                    </tr></thead>
                    <tbody>
                    <?php foreach ($output['events'] as $ev): if (!is_array($ev)) continue; ?>
                        <tr style="border-bottom:1px solid #f3f4f6;">
                            <td style="padding:0.4rem 0.75rem 0.4rem 0;white-space:nowrap;font-family:'JetBrains Mono',monospace;font-size:0.82rem;"><?= htmlspecialchars((string)($ev['date'] ?? '—')) ?></td>
                            <td style="padding:0.4rem 0.75rem;color:#374151;"><?= htmlspecialchars((string)($ev['actor'] ?? '')) ?></td>
                            <td style="padding:0.4rem 0.75rem;"><?= htmlspecialchars((string)($ev['event'] ?? '')) ?></td>
                            <td style="padding:0.4rem 0 0.4rem 0.75rem;">
                                <span style="font-size:0.78rem;padding:1px 6px;border-radius:999px;background:<?= ($ev['confidence'] ?? '') === 'high' ? '#dcfce7' : (($ev['confidence'] ?? '') === 'medium' ? '#fef9c3' : '#fee2e2') ?>;color:#374151;"><?= htmlspecialchars((string)($ev['confidence'] ?? '')) ?></span>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
                </div>
            <?php endif; ?>
            <?= crListBlock('What Remains Uncertain', (array)($output['what_remains_uncertain'] ?? [])) ?>

        <?php elseif ($toolSlug === 'translate'): ?>
            <?php if (!empty($output['translated_text'])): ?>
                <div class="cr-block">
                    <div style="display:flex;align-items:center;gap:0.75rem;margin-bottom:0.6rem;">
                        <h3 style="margin:0">Translation</h3>
                        <?php if (!empty($output['source_lang']) && !empty($output['target_lang'])): ?>
                            <span style="font-size:0.8rem;font-family:monospace;background:#f1f5f9;border:1px solid #cbd5e1;border-radius:4px;padding:0.2rem 0.5rem;color:#475569;"><?= htmlspecialchars(strtoupper((string)$output['source_lang'])) ?> → <?= htmlspecialchars(strtoupper((string)$output['target_lang'])) ?></span>
                        <?php endif; ?>
                    </div>
                    <pre style="white-space:pre-wrap;font-size:0.93rem;line-height:1.75;border:1px solid #cbd5e1;border-radius:8px;padding:1.1rem 1.4rem;background:#f8fafc;color:#1e293b;max-height:60vh;overflow-y:auto;"><?= htmlspecialchars((string)$output['translated_text']) ?></pre>
                </div>
            <?php endif; ?>
            <?php if (!empty($output['annotations']) && is_array($output['annotations'])): ?>
                <div class="cr-block">
                    <h3>Legal term notes</h3>
                    <?php foreach ($output['annotations'] as $ann): if (!is_array($ann)) continue; ?>
                        <div style="background:#f8fafc;border-left:3px solid #0f766e;padding:0.45rem 0.75rem;margin-bottom:0.45rem;border-radius:0 6px 6px 0;font-size:0.87rem;">
                            <strong style="font-family:monospace;color:#0e7490;"><?= htmlspecialchars((string)($ann['term'] ?? '')) ?></strong>
                            <?php if (!empty($ann['explanation'])): ?> — <?= htmlspecialchars((string)$ann['explanation']) ?><?php endif; ?>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
            <?php if (!empty($output['disclaimer'])): ?>
                <p style="font-size:0.85rem;color:#64748b;margin-top:1rem;"><em><?= htmlspecialchars((string)$output['disclaimer']) ?></em></p>
            <?php endif; ?>

        <?php elseif ($toolSlug === 'legal-analysis'): ?>
            <?php if (!empty($output['overall_assessment'])): ?>
                <div class="cr-block">
                    <h3>Overall assessment</h3>
                    <p style="line-height:1.6"><?= htmlspecialchars((string)$output['overall_assessment']) ?></p>
                </div>
            <?php endif; ?>
            <?php if (!empty($output['next_steps']) && is_array($output['next_steps'])): ?>
                <?= crListBlock('Next steps', $output['next_steps']) ?>
            <?php endif; ?>
            <?php if (!empty($output['issues']) && is_array($output['issues'])): ?>
                <div class="cr-block">
                    <h3>Legal issues</h3>
                    <?php foreach ($output['issues'] as $iss): if (!is_array($iss)) continue;
                        $sev = (string)($iss['severity'] ?? 'medium');
                        $sevColor = $sev === 'high' ? '#fee2e2' : ($sev === 'low' ? '#dcfce7' : '#fef3c7');
                        $sevText  = $sev === 'high' ? '#b91c1c' : ($sev === 'low' ? '#166534' : '#92400e');
                    ?>
                        <article style="border:1px solid #d8dde7;border-radius:8px;padding:0.85rem 1rem;margin-bottom:0.75rem;">
                            <div style="display:flex;gap:0.5rem;align-items:center;margin-bottom:0.4rem;">
                                <span style="font-weight:700;color:#64748b;font-size:0.85rem;">#<?= (int)($iss['id'] ?? 0) ?></span>
                                <span style="font-size:0.7rem;font-weight:700;letter-spacing:0.04em;padding:0.15rem 0.5rem;border-radius:999px;background:<?= $sevColor ?>;color:<?= $sevText ?>;"><?= htmlspecialchars(strtoupper($sev)) ?></span>
                            </div>
                            <h4 style="margin:0.2rem 0 0.4rem;font-size:1.02rem;"><?= htmlspecialchars((string)($iss['question'] ?? '')) ?></h4>
                            <?php if (!empty($iss['brief_context'])): ?>
                                <p style="font-size:0.85rem;color:#64748b;margin:0 0 0.5rem"><em><?= htmlspecialchars((string)$iss['brief_context']) ?></em></p>
                            <?php endif; ?>
                            <div style="margin-top:0.5rem;padding-top:0.5rem;border-top:1px solid #eef2f7;">
                                <h5 style="margin:0 0 0.3rem;font-size:0.78rem;text-transform:uppercase;letter-spacing:0.06em;color:#64748b;">Svar (dbn-legal-agent-v3)</h5>
                                <p style="margin:0;line-height:1.55;white-space:pre-wrap;"><?= htmlspecialchars((string)($iss['answer'] ?? '')) ?></p>
                            </div>
                            <?php if (!empty($iss['legal_basis'])): ?>
                                <div style="margin-top:0.5rem;font-size:0.85rem;color:#475569;background:#f8fafc;padding:0.3rem 0.55rem;border-radius:5px;display:inline-block;">
                                    <strong>Lovgrunnlag:</strong> <?= htmlspecialchars((string)$iss['legal_basis']) ?>
                                </div>
                            <?php endif; ?>
                            <?php if (!empty($iss['what_to_check'])): ?>
                                <p style="margin:0.55rem 0 0;font-size:0.8rem;color:#94a3b8;"><em><?= htmlspecialchars((string)$iss['what_to_check']) ?></em></p>
                            <?php endif; ?>
                        </article>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
            <?php if (!empty($output['disclaimer'])): ?>
                <p style="font-size:0.85rem;color:#64748b;margin-top:1rem;"><em><?= htmlspecialchars((string)$output['disclaimer']) ?></em></p>
            <?php endif; ?>

        <?php else: ?>
            <?php if ($primaryOutput !== ''): ?>
                <div class="cr-output"><?= htmlspecialchars($primaryOutput) ?></div>
            <?php else: ?>
                <p class="cr-output-empty">Dette verktøyet returnerer strukturert output — se rådata under.</p>
            <?php endif; ?>
        <?php endif; ?>

        <details class="cr-collapse" style="margin-top:1.25rem;">
            <summary>Vis rådata (JSON)</summary>
            <pre><?= htmlspecialchars(json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?: '{}') ?></pre>
        </details>
    </section>

    <section class="cr-section">
        <h2>Input</h2>
        <details class="cr-collapse" open>
            <summary>Vis input som ble sendt</summary>
            <pre><?= htmlspecialchars(json_encode($input, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?: '{}') ?></pre>
        </details>
    </section>
</main>

<script>
(function () {
    const titleEl = document.getElementById('crTitle');
    const renameBtn = document.getElementById('crRename');
    const deleteBtn = document.getElementById('crDelete');
    const rerunBtn  = document.getElementById('crRerun');

    renameBtn.addEventListener('click', () => {
        titleEl.contentEditable = 'true';
        titleEl.focus();
        document.execCommand('selectAll', false, null);
    });

    titleEl.addEventListener('blur', async () => {
        if (titleEl.contentEditable !== 'true') return;
        titleEl.contentEditable = 'false';
        const newTitle = (titleEl.textContent || '').trim();
        if (newTitle === '') { return; }
        try {
            await fetch('/api/case/result-action.php', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ action: 'rename', id: <?= (int)$result['id'] ?>, title: newTitle }),
            });
        } catch (e) { /* silent */ }
    });

    titleEl.addEventListener('keydown', (e) => {
        if (e.key === 'Enter') { e.preventDefault(); titleEl.blur(); }
    });

    deleteBtn.addEventListener('click', async () => {
        if (!confirm('Slette denne analysen for godt?')) return;
        try {
            const res = await fetch('/api/case/result-action.php', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ action: 'delete', id: <?= (int)$result['id'] ?> }),
            });
            const json = await res.json();
            if (json.ok) window.location.href = '/min-sak.php';
            else alert(json.error?.message || 'Sletting feilet.');
        } catch (e) { alert('Nettverksfeil: ' + e.message); }
    });

    // Re-run: just navigate to the tool page with a hash that the tool can pick up if it wants to.
    // For MVP, we deep-link back to the tool — the user can re-fill from input shown below.
    rerunBtn.addEventListener('click', () => {
        const tool = rerunBtn.getAttribute('data-tool');
        const path = {
            'korrespond':    '/korrespond.php',
            'advocate':      '/advocate.php',
            'barnevernet':   '/barnevernet.php',
            'deep-research': '/deep-research.php',
            'discrepancy':   '/discrepancy.php',
            'timeline':      '/timeline.php',
            'summarize':     '/summarize.php',
            'ask':           '/ask.php',
            'redact':        '/redact.php',
            'transcribe':    '/transcribe.php',
            'legal-analysis':'/legal-analysis.php',
            'translate':     '/translate.php',
        }[tool] || '/dashboard.php';
        window.location.href = path + '?rerun=' + <?= (int)$result['id'] ?>;
    });
})();
</script>
</body>
</html>
