<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/bootstrap.php';
$dashboardPage  = 'index';
$dashboardTitle = dbnToolsT('dash_title_overview', dbnToolsCurrentLanguage());
$dashboardLead  = dbnToolsT('dash_lead_overview',  dbnToolsCurrentLanguage());
require_once __DIR__ . '/../includes/layout_dashboard.php';
?>
<section class="dash-kpis" id="dashKpis" aria-label="Corpus statistics">
    <div class="dash-kpi">
        <p class="dash-kpi__label" id="kpiLabelDocs"></p>
        <p class="dash-kpi__value" data-kpi="documents">—</p>
        <p class="dash-kpi__hint" id="kpiHintQuota"></p>
    </div>
    <div class="dash-kpi">
        <p class="dash-kpi__label" id="kpiLabelChunks"></p>
        <p class="dash-kpi__value" data-kpi="chunks">—</p>
        <p class="dash-kpi__hint" id="kpiHintSearchable"></p>
    </div>
    <div class="dash-kpi">
        <p class="dash-kpi__label" id="kpiLabelReady"></p>
        <p class="dash-kpi__value" data-kpi="ready">—</p>
        <p class="dash-kpi__hint" id="kpiHintTotal"></p>
    </div>
    <div class="dash-kpi">
        <p class="dash-kpi__label" id="kpiLabelLast"></p>
        <p class="dash-kpi__value" data-kpi="last_upload" style="font-size: 1.05rem;">—</p>
        <p class="dash-kpi__hint" id="kpiHintDate"></p>
    </div>
</section>

<section class="dash-card">
    <div class="dash-card__head">
        <h2 id="getStartedTitle"></h2>
    </div>
    <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 1rem;">
        <a class="dash-btn dash-btn--primary" href="/dashboard/upload.php" id="btnUpload"></a>
        <a class="dash-btn" href="/dashboard/chat.php" id="btnAsk"></a>
        <a class="dash-btn" href="/dashboard/documents.php" id="btnBrowse"></a>
    </div>
</section>

<section class="dash-card">
    <div class="dash-card__head">
        <h2 id="recentTitle"></h2>
        <div class="dash-card__actions">
            <a href="/dashboard/documents.php" class="dash-btn" id="btnSeeAll"></a>
        </div>
    </div>
    <div id="dashRecent" class="dash-loading"></div>
</section>

<script>
(function () {
    'use strict';
    const I18N = window.DBN_I18N || {};
    const api  = window.DBN_DASHBOARD.apiBase;
    const loc  = I18N.locale || 'en-GB';

    document.getElementById('kpiLabelDocs').textContent      = I18N.kpi_docs || 'Documents';
    document.getElementById('kpiHintQuota').textContent      = I18N.kpi_of_quota || 'of quota';
    document.getElementById('kpiLabelChunks').textContent    = I18N.kpi_chunks || 'Passages indexed';
    document.getElementById('kpiHintSearchable').textContent = I18N.kpi_searchable || 'searchable pieces';
    document.getElementById('kpiLabelReady').textContent     = I18N.kpi_ready || 'Ready';
    document.getElementById('kpiHintTotal').textContent      = I18N.kpi_of_total || 'of total';
    document.getElementById('kpiLabelLast').textContent      = I18N.kpi_last || 'Last upload';
    document.getElementById('kpiHintDate').textContent       = I18N.kpi_date_label || 'date';
    document.getElementById('getStartedTitle').textContent   = I18N.get_started || 'Get started';
    document.getElementById('recentTitle').textContent       = I18N.recent_activity || 'Recent activity';
    document.getElementById('btnUpload').textContent         = I18N.upload_docs_btn || '📥 Upload documents';
    document.getElementById('btnAsk').textContent            = I18N.ask_btn || '💬 Ask a legal question';
    document.getElementById('btnBrowse').textContent         = I18N.browse_btn || '📚 Browse corpus';
    document.getElementById('btnSeeAll').textContent         = I18N.see_all || 'See all →';
    document.getElementById('dashRecent').textContent        = I18N.loading || 'Loading…';

    function fmtDate(s) {
        if (!s) return '—';
        try {
            return new Date(s.replace(' ', 'T') + 'Z').toLocaleDateString(loc,
                { day: 'numeric', month: 'short', year: 'numeric' });
        } catch (_) { return s; }
    }
    function fmtNum(n) { return n == null ? '—' : Number(n).toLocaleString(loc); }

    function statusPill(status) {
        const cls = {
            ready: 'dash-status--ready', pending: 'dash-status--pending',
            processing: 'dash-status--processing', error: 'dash-status--error',
        }[status] || 'dash-status--pending';
        const label = {
            ready: I18N.status_ready || 'Ready', pending: I18N.status_pending || 'Pending',
            processing: I18N.status_processing || 'Processing', error: I18N.status_error || 'Error',
        }[status] || status;
        return '<span class="dash-status ' + cls + '">' + label + '</span>';
    }

    fetch(api + '/documents.php?action=list&limit=100', { credentials: 'same-origin' })
        .then(r => r.json())
        .then(data => {
            if (!data.ok) throw new Error(data.error?.message || 'Failed');
            const docs = data.documents || [];
            const total = data.total;
            const chunks = docs.reduce((s, d) => s + (d.chunk_count || 0), 0);
            const ready  = docs.filter(d => d.status === 'ready').length;
            const last   = docs[0] ? docs[0].created_at : null;

            document.querySelector('[data-kpi="documents"]').textContent  = fmtNum(total);
            document.querySelector('[data-kpi="chunks"]').textContent     = fmtNum(chunks);
            document.querySelector('[data-kpi="ready"]').textContent      = ready + ' / ' + docs.length;
            document.querySelector('[data-kpi="last_upload"]').textContent = fmtDate(last);

            const recent = document.getElementById('dashRecent');
            if (!docs.length) {
                recent.className = 'dash-empty';
                recent.innerHTML = '<span class="dash-empty__icon">📭</span>'
                    + (I18N.empty_docs || 'No documents yet.') + ' '
                    + '<a href="/dashboard/upload.php">' + (I18N.empty_docs_link || 'Upload your first') + '</a>.';
                return;
            }
            recent.className = '';
            recent.innerHTML = '';
            const table = document.createElement('table');
            table.className = 'dash-doctable';
            table.innerHTML = '<thead><tr>'
                + '<th>' + (I18N.th_title || 'Title') + '</th>'
                + '<th>' + (I18N.th_status || 'Status') + '</th>'
                + '<th>' + (I18N.th_chunks || 'Passages') + '</th>'
                + '<th>' + (I18N.th_added || 'Added') + '</th>'
                + '</tr></thead>';
            const tbody = document.createElement('tbody');
            docs.slice(0, 8).forEach(doc => {
                const tr = document.createElement('tr');
                tr.addEventListener('click', () => location.href = '/dashboard/document.php?id=' + doc.id);
                const safe = (s) => String(s ?? '').replace(/[&<>"]/g, c => ({ '&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;' }[c]));
                tr.innerHTML =
                    '<td><span class="dash-doctable__title">' + safe(doc.title) + '</span>'
                    + (doc.source_tool ? '<div class="dash-doctable__meta">via ' + safe(doc.source_tool) + '</div>' : '') + '</td>'
                    + '<td>' + statusPill(doc.status) + '</td>'
                    + '<td>' + fmtNum(doc.chunk_count) + '</td>'
                    + '<td>' + fmtDate(doc.created_at) + '</td>';
                tbody.appendChild(tr);
            });
            table.appendChild(tbody);
            recent.appendChild(table);
        })
        .catch(err => {
            const recent = document.getElementById('dashRecent');
            recent.className = 'dash-error';
            recent.textContent = (I18N.error_loading || 'Could not load: ') + err.message;
        });
})();
</script>

<?php require_once __DIR__ . '/../includes/layout_dashboard_footer.php'; ?>
