{% extends "base.html" %}
{% block title %}{{ t('title_package_detail', request.state.lang, pkg_name, pkg_version) }}{% endblock %}
{% block breadcrumbs %}
<div class="breadcrumbs">
    <a href="/">{{ t('breadcrumb_home', request.state.lang) }}</a>
    <span class="separator">/</span>
    <a href="/packages">{{ t('breadcrumb_packages', request.state.lang) }}</a>
    <span class="separator">/</span>
    <span>{{ pkg_name }} v{{ pkg_version }}</span>
</div>
{% endblock %}
{% block content %}
<h1>{{ pkg_name }} <small>v{{ pkg_version }}</small></h1>

<article class="scan-info-block">
    <h2>{{ t('heading_scans_count', request.state.lang, scans|length) }}</h2>
    <table class="compact">
        <thead>
            <tr><th>{{ t('col_id', request.state.lang) }}</th><th>{{ t('col_repo', request.state.lang) }}</th><th>{{ t('col_status', request.state.lang) }}</th><th>{{ t('col_findings', request.state.lang) }}</th><th>{{ t('col_time', request.state.lang) }}</th></tr>
        </thead>
        <tbody>
        {% for s in scans %}
            <tr>
                <td><a href="/scans/{{ s.id }}">#{{ s.id }}</a></td>
                <td>{{ s.repository }}</td>
                <td>
                    {% with status=s.status %}{% include "_status_badge.html" %}{% endwith %}
                </td>
                <td>{% if s.flagged %}<span class="flagged">{{ s.total_findings }}</span>{% else %}<span class="clean">0</span>{% endif %}</td>
                <td>{{ s.started_at.strftime('%Y-%m-%d %H:%M') if s.started_at }}</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
</article>

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

{% if findings %}
    <div id="findings-container">
    {% for f in 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 %}
