{% extends "shared/frontend/base.html" %}

{% block title %}ESET Oversigt - Hardware - BMC Hub{% endblock %}

{% block extra_css %}
<style>
    .page-header {
        margin-bottom: 2rem;
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 1rem;
        flex-wrap: wrap;
    }

    .section-card {
        background: var(--bg-card);
        border-radius: 12px;
        border: 1px solid rgba(0,0,0,0.1);
        padding: 1.5rem;
        margin-bottom: 2rem;
    }

    .section-title {
        font-size: 1.2rem;
        font-weight: 700;
        margin-bottom: 1rem;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .table thead th {
        font-size: 0.85rem;
        text-transform: uppercase;
        letter-spacing: 0.02em;
        color: var(--text-secondary);
    }

    .badge-severity {
        font-size: 0.75rem;
        padding: 0.35rem 0.6rem;
        border-radius: 999px;
        color: white;
        background: #dc3545;
    }

    .badge-high {
        background: #fd7e14;
    }

    .badge-severe {
        background: #6f42c1;
    }

    .empty-state {
        padding: 1.5rem;
        border: 1px dashed rgba(0,0,0,0.2);
        border-radius: 10px;
        text-align: center;
        color: var(--text-secondary);
        background: var(--bg-body);
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid" style="margin-top: 2rem; max-width: 1400px;">
    <div class="page-header">
        <h1>🛡️ ESET Oversigt</h1>
        <div class="d-flex gap-2">
            <a href="/hardware/eset/import" class="btn btn-outline-secondary">Import</a>
            <a href="/hardware/eset/test" class="btn btn-outline-secondary">ESET Test</a>
            <a href="/hardware" class="btn btn-outline-secondary">Tilbage til hardware</a>
        </div>
    </div>
    {% if current_user %}
    <div class="mb-3 text-muted small">
        Logget ind som {{ current_user.full_name or current_user.username or current_user.email }}
    </div>
    {% endif %}

    <div class="section-card">
        <div class="section-title">🔗 Matchede enheder</div>
        {% if matches %}
        <div class="table-responsive">
            <table class="table table-hover align-middle">
                <thead>
                    <tr>
                        <th>Hardware</th>
                        <th>Serial</th>
                        <th>ESET UUID</th>
                        <th>ESET Gruppe</th>
                        <th>Kontakt</th>
                        <th>Company</th>
                        <th>Kunde</th>
                        <th>Opdateret</th>
                    </tr>
                </thead>
                <tbody>
                    {% for row in matches %}
                    <tr>
                        <td>
                            <a href="/hardware/{{ row.id }}">
                                {{ row.brand or '' }} {{ row.model or '' }}
                            </a>
                        </td>
                        <td>{{ row.serial_number or '-' }}</td>
                        <td style="max-width: 220px; word-break: break-all;">{{ row.eset_uuid or '-' }}</td>
                        <td style="max-width: 220px; word-break: break-all;">{{ row.eset_group or '-' }}</td>
                        <td>
                            {% if row.contact_id %}
                                {{ row.first_name or '' }} {{ row.last_name or '' }}
                            {% else %}
                                -
                            {% endif %}
                        </td>
                        <td>{{ row.user_company or '-' }}</td>
                        <td>{{ row.customer_name or '-' }}</td>
                        <td>{{ row.updated_at or '-' }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
        <div class="empty-state">Ingen matchede enheder fundet endnu.</div>
        {% endif %}
    </div>

    <div class="section-card">
        <div class="section-title">🚨 Kritiske incidents (seneste 5)</div>
        {% if incidents %}
        <div class="table-responsive">
            <table class="table table-hover align-middle">
                <thead>
                    <tr>
                        <th>Severity</th>
                        <th>Status</th>
                        <th>Device UUID</th>
                        <th>Detected</th>
                        <th>Last Seen</th>
                        <th>Handling</th>
                    </tr>
                </thead>
                <tbody>
                    {% for inc in incidents %}
                    <tr>
                        <td>
                            {% set sev = (inc.severity or '')|lower %}
                            <span class="badge-severity {% if sev == 'high' %}badge-high{% elif sev == 'severe' %}badge-severe{% endif %}">
                                {{ inc.severity or 'critical' }}
                            </span>
                        </td>
                        <td>{{ inc.status or '-' }}</td>
                        <td style="max-width: 220px; word-break: break-all;">{{ inc.device_uuid or '-' }}</td>
                        <td>{{ inc.detected_at or '-' }}</td>
                        <td>{{ inc.last_seen or '-' }}</td>
                        <td>
                            {% set case_title = 'ESET incident ' ~ (inc.severity or 'critical') ~ ' - ' ~ (inc.device_uuid or 'ukendt enhed') %}
                            {% set case_description =
                                'Kilde: ESET incidents | ' ~
                                'Severity: ' ~ (inc.severity or '-') ~ ' | ' ~
                                'Status: ' ~ (inc.status or '-') ~ ' | ' ~
                                'Device UUID: ' ~ (inc.device_uuid or '-') ~ ' | ' ~
                                'Incident UUID: ' ~ (inc.incident_uuid or '-') ~ ' | ' ~
                                'Detected: ' ~ (inc.detected_at or '-') ~ ' | ' ~
                                'Last Seen: ' ~ (inc.last_seen or '-')
                            %}
                            <a
                                href="/sag/new?title={{ case_title | urlencode }}&description={{ case_description | urlencode }}{% if inc.customer_id %}&customer_id={{ inc.customer_id }}{% endif %}"
                                class="btn btn-sm btn-outline-primary"
                            >
                                Opret sag
                            </a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
        <div class="empty-state">Ingen kritiske incidents lige nu.</div>
        {% endif %}
    </div>
</div>
{% endblock %}
