{% extends "/base.html" %}
{% block title %}{{ result.target }} (ランク{{ result.rank }}) - Nercone TLS Test{% endblock %}
{% block title_suffix %}TLS Test{% endblock %}
{% block description %}TLS Test の結果ページです。対象: {{ result.target }} / ランク: {{ result.rank }}。{% endblock %}
{% block header_desc %}Results{% endblock %}
{% block extra_head %}
        <link rel="stylesheet" href="/tools/tls-test/assets/tls-test.css">
{% endblock %}
{% block content %}
            {% set rank_color_map = {
                'SSS': 'bright-green', 'SS': 'bright-green', 'S': 'bright-green',
                'A': 'green', 'B': 'green', 'C': 'green',
                'D': 'bright-yellow', 'E': 'bright-yellow', 'F': 'bright-yellow',
                'G': 'yellow', 'H': 'yellow', 'I': 'yellow',
                'J': 'bright-orange', 'K': 'bright-orange', 'L': 'bright-orange',
                'M': 'orange', 'N': 'orange',
                'O': 'bright-red', 'P': 'bright-red',
                'Q': 'red', 'R': 'purple'
            } %}
            {% set rank_color = rank_color_map.get(result.rank or job.rank, 'tx') %}
            <div class="block tls-summary">
                <div class="tls-rank-badge text-{{ rank_color }}">
                    <span class="tls-rank-letters">{{ result.rank or job.rank or '?' }}</span>
                    <span class="tls-rank-score">{{ "%.2f"|format(result.score or job.score or 0) }}</span>
                </div>
                <div class="tls-summary-meta flex-1">
                    <h1 class="tls-target">{{ result.target or job.target }}</h1>
                    <p class="font-small text-tx-alt">host={{ result.host }} port={{ result.port }}</p>
                    <p class="font-small text-tx-alt">実施: {{ job.started_at | default("") }} / 所要時間: {{ "%.2f"|format(result.duration or 0) }}秒 / テストID: <code>{{ test_id }}</code></p>
                    <p class="font-small text-tx-alt">結果は7日間保持され、その後自動的に削除されます。</p>
                    {% if result.error %}
                    <p class="text-bright-red">{{ result.error }}</p>
                    {% endif %}
                </div>
            </div>

            {% set category_titles = {
                'protocol': 'SSL/TLS Versions',
                'cipher': 'Cipher Suites',
                'kex': 'Key Exchange',
                'cert': 'Certificate',
                'trust': 'Trust Stores',
                'hsts': 'HSTS / Preload',
                'caa': 'CAA',
                'http': 'HTTP',
                'vuln': 'Vulnerabilities',
                'compat': 'Client Compatibility',
                'connectivity': 'Connectivity',
                'engine': 'Engine'
            } %}
            {% for cat_key, findings in categories.items() %}
            <div class="block">
                <h2>{{ category_titles.get(cat_key, cat_key|capitalize) }}</h2>
                {% for f in findings %}
                <p class="tls-finding">
                    <span class="text-{{ f.color }}">[{{ f.severity_label }}]</span>
                    <span class="font-bold">{{ f.title }}</span>
                    {% if f.detail %}<span class="text-tx-alt font-small">— {{ f.detail }}</span>{% endif %}
                </p>
                {% endfor %}
            </div>
            {% endfor %}

            {% if result.data and result.data.handshake_simulation %}
            <div class="block">
                <h2>Handshake Simulation</h2>
                <table class="tls-table">
                    <thead>
                        <tr><th>Client</th><th>Version</th><th>Cipher</th></tr>
                    </thead>
                    <tbody>
                        {% for s in result.data.handshake_simulation %}
                        <tr>
                            <td>{{ s.client }}</td>
                            <td>
                                {% if s.connected %}
                                <span class="text-bright-green">{{ s.negotiated_version }}</span>
                                {% else %}
                                <span class="text-bright-red">failed</span>
                                {% endif %}
                            </td>
                            <td class="font-small text-tx-alt">
                                {% if s.connected %}{{ s.negotiated_cipher }}{% else %}{{ s.error }}{% endif %}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% endif %}

            <div class="block">
                <h2>Raw JSON</h2>
                <p class="font-small text-tx-alt">API でも同じデータを取得できます: <code>GET /api/tools/tls-test/results/{{ test_id }}</code></p>
                <details>
                    <summary>Show raw result JSON</summary>
                    <pre class="tls-raw"><code>{{ result | tojson(indent=2) }}</pre>
                </details>
            </div>
{% endblock %}
