{% extends "base.html" %}
{% block title %}{{ t('title_scan_detail', request.state.lang, scan.id) }}{% endblock %}
{% block breadcrumbs %}
<div class="breadcrumbs">
    <a href="/">{{ t('breadcrumb_home', request.state.lang) }}</a>
    <span class="separator">/</span>
    <a href="/scans">{{ t('breadcrumb_scans', request.state.lang) }}</a>
    <span class="separator">/</span>
    <span>{{ t('scan_detail_title', request.state.lang, scan.id) }}</span>
</div>
{% endblock %}
{% block content %}
<h1>{{ t('scan_detail_title', request.state.lang, scan.id) }}</h1>

<article class="scan-info-block">
    <div class="scan-info-grid">
        <div><strong>{{ t('scan_info_package', request.state.lang) }}</strong><br><a href="/packages/{{ scan.package_name | urlencode }}/{{ scan.package_version | urlencode }}">{{ scan.package_name }}</a></div>
        <div><strong>{{ t('scan_info_version', request.state.lang) }}</strong><br>{{ scan.package_version }}</div>
        <div><strong>{{ t('scan_info_ecosystem', request.state.lang) }}</strong><br>{{ scan.ecosystem }}</div>
        <div><strong>{{ t('scan_info_repository', request.state.lang) }}</strong><br>{{ scan.repository }}</div>
        <div><strong>{{ t('scan_info_status', request.state.lang) }}</strong><br>
            {% with status=scan.status %}{% include "_status_badge.html" %}{% endwith %}
        </div>
        <div><strong>{{ t('scan_info_sha256', request.state.lang) }}</strong><br><code class="sha256">{{ scan.sha256 or '-' }}</code></div>
        <div><strong>{{ t('scan_info_started', request.state.lang) }}</strong><br>{{ scan.started_at.strftime('%Y-%m-%d %H:%M') if scan.started_at }}</div>
        <div><strong>{{ t('scan_info_finished', request.state.lang) }}</strong><br>{{ scan.finished_at.strftime('%Y-%m-%d %H:%M') if scan.finished_at }}</div>
        {% if scan.initiator %}<div><strong>{{ t('scan_info_initiated', request.state.lang) }}</strong><br>{{ scan.initiator }}</div>{% endif %}
        {% if scan.source_ip %}<div><strong>{{ t('scan_info_source_ip', request.state.lang) }}</strong><br>{{ scan.source_ip }}</div>{% endif %}
    </div>
    {% if scan.error_message %}<div class="scan-error"><strong>{{ t('scan_info_error', request.state.lang) }}:</strong> {{ scan.error_message }}</div>{% endif %}
</article>

<h2 style="margin-bottom: 0.75rem;">{{ t('heading_findings_count', request.state.lang, scan.findings|length) }}</h2>

{% if scan.findings %}
    <div id="findings-container">
    {% for f in scan.findings %}
    <div class="finding-block finding-{{ f.data.severity|lower }}">
        <div class="finding-summary">
            <strong class="severity-{{ f.data.severity }}">[{{ f.data.severity }}]</strong>
            <strong>{{ f.data.rule }}</strong>
            {% if f.data.location %}<small> @ {{ f.data.location }}</small>{% endif %}
        </div>
        <div class="finding-body">
            <p>{{ f.data.message }}</p>
            {% if f.data.code %}
            <div class="code-toolbar">
                <button class="copy-btn" onclick="copyCode(this, 'code-{{ f.id }}')">{{ t('btn_copy', request.state.lang) }}</button>
            </div>
            <pre><code id="code-{{ f.id }}">{{ f.data.code }}</code></pre>
            {% endif %}

            {% if f.report and f.report.status == "analyzing" %}
            {% include "_llm_spinner.html" %}
            {% elif f.report and f.report.verdict %}
            <div class="llm-report llm-{{ f.report.verdict }}">
                <div class="llm-header">
                    <span class="llm-badge llm-badge-{{ f.report.verdict }}">{{ f.report.verdict }}</span>
                    {% if f.report.severity_rating %}
                    <span class="llm-severity">{{ f.report.severity_rating }}</span>
                    {% endif %}
                    {% if config.llm_enabled and not config.llm_auto_analyze %}
                    <span class="llm-retry"
                        hx-post="/api/v1/findings/{{ f.id }}/analyze?retry=1"
                        hx-target="closest .llm-report"
                        hx-swap="outerHTML"
                        hx-indicator="closest .llm-report">{{ t('llm_retry', request.state.lang) }}</span>
                    {% endif %}
                </div>
                <p class="llm-summary">{{ f.report.summary }}</p>
                <p class="llm-analysis">{{ f.report.analysis }}</p>
                <p class="llm-disclaimer">{{ t('llm_disclaimer', request.state.lang) }}</p>
            </div>
            {% elif config.llm_enabled and not config.llm_auto_analyze %}
            <div class="llm-actions" id="llm-{{ f.id }}">
                <button class="outline"
                    hx-post="/api/v1/findings/{{ f.id }}/analyze"
                    hx-target="#llm-{{ f.id }}"
                    hx-swap="outerHTML"
                    hx-indicator="#llm-spinner-{{ f.id }}">
                    <span id="llm-spinner-{{ f.id }}" class="htmx-indicator" style="display:none;">
                        <span class="spinner"></span>
                    </span>
                    {{ t('btn_analyze_llm', request.state.lang) }}
                </button>
            </div>
            {% endif %}
        </div>
    </div>
    {% endfor %}
    </div>
{% else %}
    <p class="empty-state">{{ t('empty_no_findings', request.state.lang) }}</p>
{% endif %}
{% endblock %}
