<?php
declare(strict_types=1);
$toolName     = 'legal-analysis';
$toolTitle    = 'Legal Analysis';
$toolKind     = 'Deep Legal Q&A';
$toolBadge    = 'Two-pass';
$extraScripts = ['assets/js/legal-analysis.js'];
require_once __DIR__ . '/includes/layout.php';

$laLang  = dbnToolsCurrentLanguage();
$laT     = static fn(string $k): string => dbnToolsT($k, $laLang);
$laI18n  = [
    'docTypeAuto'        => $laT('la_doc_type_auto'),
    'runButton'          => $laT('la_run_button'),
    'runButtonBusy'      => $laT('la_run_button_busy'),
    'extractingFiles'    => $laT('la_extracting_files'),
    'needInput'          => $laT('la_need_input'),
    'errorPrefix'        => $laT('la_error_prefix'),
    'serverReturned'     => $laT('la_server_returned'),
    'pass1'              => $laT('la_pipeline_pass1'),
    'pass2'              => $laT('la_pipeline_pass2'),
    'pass3'              => $laT('la_pipeline_pass3'),
    'pass1Extracting'    => $laT('la_pass1_extracting'),
    'pass1Found'         => $laT('la_pass1_found'),
    'pass2Asking'        => $laT('la_pass2_asking'),
    'pass2Answered'      => $laT('la_pass2_answered'),
    'pass3Synthesis'     => $laT('la_pass3_synthesis'),
    'waiting'            => $laT('la_waiting'),
    'searchingCorpus'    => $laT('la_searching_corpus'),
    'askingFinetune'     => $laT('la_asking_finetune'),
    'overall'            => $laT('la_overall'),
    'nextSteps'          => $laT('la_next_steps'),
    'answerHeader'       => $laT('la_answer_header'),
    'legalBasis'         => $laT('la_legal_basis'),
    'extractingStatus'   => $laT('la_extracting_status'),
    'synthesisingStatus' => $laT('la_synthesising_status'),
    'emptyIssues'        => $laT('la_empty_issues'),
];
?>
                <form id="laForm" class="tool-form" novalidate>

                    <div class="lang-switcher" id="laLangSwitcher" role="group" aria-label="UI language">
                        <button type="button" class="lang-btn la-lang-btn <?= $laLang === 'en' ? 'is-active' : '' ?>" data-lang="en">&#127468;&#127463; EN</button>
                        <button type="button" class="lang-btn la-lang-btn <?= $laLang === 'no' ? 'is-active' : '' ?>" data-lang="no">&#127475;&#127476; NO</button>
                        <button type="button" class="lang-btn la-lang-btn <?= $laLang === 'uk' ? 'is-active' : '' ?>" data-lang="uk">&#127482;&#127462; UK</button>
                        <button type="button" class="lang-btn la-lang-btn <?= $laLang === 'pl' ? 'is-active' : '' ?>" data-lang="pl">&#127477;&#127473; PL</button>
                    </div>

                    <div class="control-row" id="laDocTypeControl">
                        <span class="control-label"><?= htmlspecialchars($laT('la_doc_type_label')) ?></span>
                        <label><input type="radio" name="laDocType" value="auto" checked> <?= htmlspecialchars($laT('la_doc_type_auto')) ?></label>
                        <label><input type="radio" name="laDocType" value="barnevernet"> Barnevernet</label>
                        <label><input type="radio" name="laDocType" value="adopsjon"> Adopsjon</label>
                        <label><input type="radio" name="laDocType" value="emergency"> Akutt-plassering</label>
                        <label><input type="radio" name="laDocType" value="samvær"> Samvær</label>
                        <label><input type="radio" name="laDocType" value="fylkesnemnd"> Fylkesnemnd</label>
                        <label><input type="radio" name="laDocType" value="other"> <?= htmlspecialchars($laT('la_doc_type_other')) ?></label>
                    </div>

                    <p class="upload-hint"><?= htmlspecialchars($laT('la_engine_hint')) ?></p>

                    <div id="docPickerSection" class="doc-picker-section">
                        <button type="button" id="docPickerBtn" class="doc-picker-btn" aria-haspopup="dialog">
                            <svg class="doc-picker-btn__icon" width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><rect x="2" y="1" width="9" height="12" rx="1.5" stroke="currentColor" stroke-width="1.4"/><path d="M5 5h5M5 8h3" stroke="currentColor" stroke-width="1.3" stroke-linecap="round"/><rect x="7" y="9" width="6" height="5" rx="1" fill="white" stroke="currentColor" stroke-width="1.3"/><path d="M9 11h2M9 12.5h1" stroke="currentColor" stroke-width="1" stroke-linecap="round"/></svg>
                            <span><?= htmlspecialchars($laT('doc_picker_btn')) ?></span>
                        </button>
                        <div id="docPickerChips" class="doc-picker-chips" aria-label="Selected documents"></div>
                        <input type="hidden" id="docPickerIds" name="doc_ids" value="">
                    </div>

                    <div class="upload-zone" id="laUploadZone" role="region" aria-label="File upload">
                        <input type="file" id="laUploadInput" multiple accept=".pdf,.docx,.txt" aria-label="Choose files to analyse">
                        <div id="laUploadPrompt" class="upload-prompt">
                            <span class="upload-icon" aria-hidden="true">&#8679;</span>
                            <p>Drop up to 5 files here, or <label for="laUploadInput" class="upload-browse">browse</label></p>
                            <p class="upload-hint"><strong>PDF</strong>, <strong>DOCX</strong>, <strong>TXT</strong> &mdash; text extracted in memory, never stored</p>
                        </div>
                        <div id="laUploadFileInfo" class="upload-file is-hidden">
                            <ul id="laUploadFileList" class="upload-file-list"></ul>
                            <button type="button" id="laUploadClear" class="upload-clear">&times; Clear files</button>
                        </div>
                    </div>

                    <label class="input-label" for="laInput"><?= htmlspecialchars($laT('la_input_label')) ?> <small class="control-hint"><?= htmlspecialchars($laT('la_input_hint')) ?></small></label>
                    <textarea id="laInput" name="text" rows="8" placeholder="<?= htmlspecialchars($laT('la_input_placeholder')) ?>"></textarea>

                    <div class="form-footer">
                        <p id="laStatus" class="form-status" role="status" aria-live="polite"></p>
                        <button id="laRunButton" type="submit"><?= htmlspecialchars($laT('la_run_button')) ?></button>
                    </div>
                </form>

                <section id="laResults" class="results" aria-live="polite">
                    <div class="empty-state">
                        <h3><?= htmlspecialchars($laT('la_ready_title')) ?></h3>
                        <p><?= htmlspecialchars($laT('la_ready_intro')) ?></p>
                        <p class="upload-hint"><?= htmlspecialchars($laT('la_ready_pipeline')) ?></p>
                    </div>
                </section>

                <script>
                    window.DBN_LA_I18N = <?= json_encode($laI18n, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP) ?>;
                    window.DBN_LA_LANG = <?= json_encode($laLang) ?>;
                </script>

                <!-- Hidden stubs so tools.js element refs don't crash on this page -->
                <div class="is-hidden" id="languageControl" aria-hidden="true">
                    <input type="radio" name="language" value="en" <?= $laLang === 'en' ? 'checked' : '' ?>>
                    <input type="radio" name="language" value="no" <?= $laLang === 'no' ? 'checked' : '' ?>>
                    <input type="radio" name="language" value="uk" <?= $laLang === 'uk' ? 'checked' : '' ?>>
                    <input type="radio" name="language" value="pl" <?= $laLang === 'pl' ? 'checked' : '' ?>>
                </div>
                <div class="is-hidden" id="redactionControl" aria-hidden="true"></div>
                <div class="is-hidden" id="audioZone" aria-hidden="true">
                    <input type="file" id="audioInput" style="display:none">
                    <div id="audioPrompt"></div>
                    <div id="audioFileInfo"><ol id="audioQueueList"></ol><button type="button" id="audioClear"></button></div>
                </div>
                <div class="is-hidden" id="diarizeControl" aria-hidden="true">
                    <input type="checkbox" id="diarizeCheck">
                    <input type="number" id="numSpeakersInput">
                </div>
                <div class="is-hidden" id="transcribeLangControl" aria-hidden="true"><input type="radio" name="transcribeLang" value="no" checked></div>
                <div class="is-hidden" id="vocabControl" aria-hidden="true">
                    <div id="vocabPresets"></div>
                    <textarea id="initPromptInput"></textarea>
                </div>
                <div class="is-hidden" id="aliasSection" aria-hidden="true">
                    <button type="button" id="addAliasRow"></button>
                    <div id="aliasRows"></div>
                </div>
                <div class="is-hidden" id="exemptSection" aria-hidden="true">
                    <button type="button" id="addExemptRow"></button>
                    <div id="exemptRows"></div>
                </div>
                <div class="is-hidden" id="uploadZone" aria-hidden="true">
                    <input type="file" id="uploadInput">
                    <div id="uploadPrompt"></div>
                    <div id="uploadFileInfo"><ul id="uploadFileList"></ul><button type="button" id="uploadClear"></button></div>
                </div>
<?php require_once __DIR__ . '/includes/layout_footer.php'; ?>
