<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Auswertungen — {{ app_name }}</title>
    <style>
        :root {
            --color-darkgray: #5a5a5a;
            --color-green: #889e33;
            --color-blue: #009da5;
            --color-lightgray: #bfbfbf;
            --color-bg: #f5f5f5;
            --color-orange: #F7941D;
            --color-red: #d00000;
        }
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body {
            font-family: 'Avenir', 'Segoe UI', sans-serif;
            color: var(--color-darkgray);
            line-height: 1.6;
            background: var(--color-bg);
        }
        .header {
            background: white;
            padding: 1rem 2rem;
            border-bottom: 1px solid var(--color-lightgray);
            display: flex;
            align-items: center;
            justify-content: space-between;
            flex-wrap: wrap;
            gap: 1rem;
        }
        .header h1 { color: var(--color-blue); font-size: 1.3rem; }
        .header nav a {
            color: var(--color-blue);
            text-decoration: none;
            margin-right: 1rem;
            font-size: 0.9rem;
        }
        .header nav a:hover { text-decoration: underline; }
        main { max-width: 1400px; margin: 1.5rem auto; padding: 0 2rem; }
        .controls {
            background: white;
            padding: 1rem;
            border-radius: 4px;
            margin-bottom: 1rem;
            display: flex;
            align-items: center;
            gap: 1rem;
            flex-wrap: wrap;
        }
        .controls label { font-size: 0.9rem; }
        .controls select, .controls button {
            padding: 0.4rem 0.7rem;
            border: 1px solid var(--color-lightgray);
            border-radius: 3px;
            font-size: 0.9rem;
            background: white;
            color: var(--color-darkgray);
            cursor: pointer;
        }
        .controls button.export {
            background: var(--color-blue);
            color: white;
            border-color: var(--color-blue);
        }
        .matrix-wrap { background: white; padding: 1rem; border-radius: 4px; overflow-x: auto; }
        table.matrix {
            border-collapse: collapse;
            width: 100%;
            font-size: 0.85rem;
        }
        table.matrix th, table.matrix td {
            border: 1px solid var(--color-lightgray);
            padding: 0.4rem 0.6rem;
            text-align: center;
        }
        table.matrix th {
            background: var(--color-bg);
            font-weight: 600;
            color: var(--color-darkgray);
        }
        table.matrix th.row-header {
            background: var(--color-blue);
            color: white;
            text-align: left;
            position: sticky;
            left: 0;
        }
        table.matrix .empty { color: var(--color-lightgray); }
        table.matrix .score-high { background: rgba(136, 158, 51, 0.25); font-weight: 600; }
        table.matrix .score-mid { background: rgba(247, 148, 29, 0.18); }
        table.matrix .score-low { background: rgba(208, 0, 0, 0.18); font-weight: 600; }
        .meta {
            font-size: 0.8rem;
            color: var(--color-lightgray);
            margin-top: 0.6rem;
        }
        .empty-state {
            background: white;
            padding: 2rem;
            text-align: center;
            border-radius: 4px;
            color: var(--color-lightgray);
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>Auswertungen — Bundesland × Partei × Wahlperiode</h1>
        <nav>
            <a href="/">← zurück zur Suche</a>
            <a href="/quellen">Quellen</a>
        </nav>
    </div>

    <main>
        <div class="controls">
            <label>Wahlperiode:
                <select id="wp-filter">
                    <option value="">— alle WPs —</option>
                    {% for wp in wahlperioden %}
                        <option value="{{ wp }}">{{ wp }}</option>
                    {% endfor %}
                </select>
            </label>
            <button id="reload">Anwenden</button>
            <button class="export" id="export-csv">CSV-Export (alle Daten)</button>
        </div>

        <div id="matrix-container" class="matrix-wrap">
            <div class="empty-state">Lade Matrix …</div>
        </div>

        <div class="meta" id="meta"></div>
    </main>

    <script>
        const wpFilter = document.getElementById('wp-filter');
        const reloadBtn = document.getElementById('reload');
        const exportBtn = document.getElementById('export-csv');
        const container = document.getElementById('matrix-container');
        const meta = document.getElementById('meta');

        function scoreClass(avg) {
            if (avg === null || avg === undefined) return '';
            if (avg >= 6) return 'score-high';
            if (avg >= 3) return 'score-mid';
            return 'score-low';
        }

        async function loadMatrix() {
            const wp = wpFilter.value;
            const url = wp
                ? `/api/auswertungen/matrix?wahlperiode=${encodeURIComponent(wp)}`
                : '/api/auswertungen/matrix';
            container.innerHTML = '<div class="empty-state">Lade Matrix …</div>';
            try {
                const r = await fetch(url);
                const data = await r.json();
                if (!data.bundeslaender.length) {
                    container.innerHTML = '<div class="empty-state">Keine Assessments für diesen Filter.</div>';
                    meta.textContent = '';
                    return;
                }
                let html = '<table class="matrix"><thead><tr><th class="row-header">Bundesland</th>';
                for (const partei of data.parteien) {
                    html += `<th>${partei}</th>`;
                }
                html += '</tr></thead><tbody>';
                for (const bl of data.bundeslaender) {
                    html += `<tr><th class="row-header">${bl}</th>`;
                    for (const partei of data.parteien) {
                        const cell = (data.cells[bl] || {})[partei];
                        if (cell) {
                            html += `<td class="${scoreClass(cell.avg)}">${cell.avg.toFixed(1)}<br><small>n=${cell.n}</small></td>`;
                        } else {
                            html += '<td class="empty">—</td>';
                        }
                    }
                    html += '</tr>';
                }
                html += '</tbody></table>';
                container.innerHTML = html;
                meta.textContent = `${data.total} Assessment(s) | Filter: ${data.filter_wp || 'alle WPs'}`;
            } catch (e) {
                container.innerHTML = `<div class="empty-state">Fehler beim Laden: ${e}</div>`;
            }
        }

        reloadBtn.addEventListener('click', loadMatrix);
        wpFilter.addEventListener('change', loadMatrix);
        exportBtn.addEventListener('click', () => {
            window.location.href = '/api/auswertungen/export.csv';
        });

        loadMatrix();
    </script>
</body>
</html>
