<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Forensic Artifact: {{ report.query | truncate(40) }}</title>
    <style>
{{ css_content | safe }}
    </style>
</head>
<body>
    
    <!-- ===== METADATA DASHBOARD ===== -->
    <div class="metadata-dashboard">
        <div class="dashboard-header">
            Jasper Forensic Report (v{{ report.version }}) | Audit Log
        </div>
        <div class="dashboard-body">
            <div class="dashboard-column">
                <div class="meta-row"><span class="meta-label">QUERY HASH:</span> <span class="meta-value">{{ report.query | hash }}</span></div>
                <div class="meta-row"><span class="meta-label">TIMESTAMP:</span> <span class="meta-value">{{ report.timestamp.strftime('%Y-%m-%d %H:%M:%S UTC') }}</span></div>
                <div class="meta-row"><span class="meta-label">ENTITIES:</span> <span class="meta-value">{{ report.tickers | join(', ') or 'N/A' }}</span></div>
                <div class="meta-row"><span class="meta-label">MODE:</span> <span class="meta-value">{{ report.report_mode | upper }}</span></div>
            </div>
            <div class="dashboard-column">
                <div class="meta-row">
                    <span class="meta-label">DATA RETRIEVAL:</span>
                    {% set completed_count = report.audit_trail | selectattr('status', 'equalto', 'completed') | list | length %}
                    {% set total_count = report.audit_trail | length %}
                    {% if total_count == 0 %}
                    <span class="ledger-entry status-failed">NO DATA</span>
                    {% elif completed_count == total_count %}
                    <span class="ledger-entry status-passed">SUCCESS</span>
                    {% elif completed_count > 0 %}
                    <span class="ledger-entry status-warning">PARTIAL ({{ completed_count }}/{{ total_count }})</span>
                    {% else %}
                    <span class="ledger-entry status-failed">FAILED</span>
                    {% endif %}
                </div>
                <div class="meta-row">
                    <span class="meta-label">VALIDATION:</span> 
                    <span class="ledger-entry {% if report.is_valid %}status-passed{% else %}status-failed{% endif %}">
                        {% if report.is_valid %}PASSED{% else %}FAILED{% endif %}
                    </span>
                </div>
                <div class="meta-row">
                    <span class="meta-label">CONFIDENCE:</span> 
                    <span class="ledger-entry {% if report.confidence_score > 0.8 %}status-passed{% else %}status-failed{% endif %}">
                        {{ report.confidence_score | round(2) }}
                    </span>
                </div>
            </div>
        </div>
    </div>

    <h1>Forensic Analysis: {{ report.query }}</h1>

    <!-- 1 EVIDENCE MATRIX -->
    <section class="forensic-section">
        <div class="section-title">1. Evidence Matrix</div>
        <table>
            <thead>
                <tr>
                    <th style="width: 10%;">ID</th>
                    <th style="width: 25%;">Metric</th>
                    <th style="width: 20%;">Value</th>
                    <th style="width: 15%;">Period</th>
                    <th style="width: 15%;">Source</th>
                    <th style="width: 15%;">Status</th>
                </tr>
            </thead>
            <tbody>
                {% for item in report.evidence_log %}
                <tr>
                    <td class="monospace">{{ item.id }}</td>
                    <td>{{ item.metric }}</td>
                    <td class="bold">{{ item.value }}</td>
                    <td>{{ item.period }}</td>
                    <td>{{ item.source }}</td>
                    <td><span class="ledger-entry {% if item.status == 'VERIFIED' %}status-passed{% else %}status-failed{% endif %}">{{ item.status }}</span></td>
                </tr>
                {% else %}
                <tr><td colspan="6" style="text-align: center;">No structured evidence log available.</td></tr>
                {% endfor %}
            </tbody>
        </table>
    </section>

    <!-- 2 LIMITATION MATRIX (Foregrounded) -->
    <section class="forensic-section">
        <div class="section-title">2. Limitation Matrix</div>
        <table>
            <thead>
                <tr>
                    <th style="width: 30%;">Category</th>
                    <th style="width: 70%;">Constraint / Risk Mapping</th>
                </tr>
            </thead>
            <tbody>
                {% for category, constraint in report.logic_constraints.items() %}
                <tr>
                    <td class="bold">{{ category | upper }}</td>
                    <td>{{ constraint }}</td>
                </tr>
                {% endfor %}
                {% for issue in report.validation_issues %}
                <tr>
                    <td class="bold">VALIDATION_SIGNAL</td>
                    <td class="text-danger">{{ issue }}</td>
                </tr>
                {% endfor %}
                {% if not report.logic_constraints and not report.validation_issues %}
                <tr><td colspan="2">No formal constraints identified.</td></tr>
                {% endif %}
            </tbody>
        </table>
    </section>

    <div class="page-break"></div>

    <!-- 3 INFERENCE MAP -->
    <section class="forensic-section">
        <div class="section-title">3. Inference Map (Claim-to-Data)</div>
        <table>
            <thead>
                <tr>
                    <th style="width: 40%;">Claim</th>
                    <th style="width: 15%;">Evidence IDs</th>
                    <th style="width: 30%;">Logic Path</th>
                    <th style="width: 15%;">Confidence</th>
                </tr>
            </thead>
            <tbody>
                {% for inf in report.inference_map %}
                <tr>
                    <td class="bold">{{ inf.claim }}</td>
                    <td>
                        {% for eid in inf.evidence_ids %}
                        <span class="evidence-tag">{{ eid }}</span>
                        {% endfor %}
                    </td>
                    <td class="logic-path">{{ inf.logic_path }}</td>
                    <td class="text-right monospace">{{ (inf.confidence * 100) | round(0) }}%</td>
                </tr>
                {% else %}
                <tr><td colspan="4" style="text-align: center;">No structured inference map available.</td></tr>
                {% endfor %}
            </tbody>
        </table>
    </section>

    <!-- 4 SYNTHESIS CONTEXT -->
    <section class="forensic-section">
        <div class="section-title">4. Analytical Synthesis</div>
        <div class="synthesis-content">
            {{ synthesis_html | safe }}
        </div>
    </section>

    <div class="page-break"></div>

    <!-- 5 AUDIT TRAIL -->
    <section class="forensic-section">
        <div class="section-title">5. Execution Audit Trail</div>
        <table>
            <thead>
                <tr>
                    <th style="width: 15%;">Task ID</th>
                    <th style="width: 45%;">Operation</th>
                    <th style="width: 20%;">Tool</th>
                    <th style="width: 20%;">Status</th>
                </tr>
            </thead>
            <tbody>
                {% for task in report.audit_trail %}
                <tr>
                    <td class="monospace">{{ task.task_id }}</td>
                    <td>{{ task.description }}</td>
                    <td>{{ task.tool }}</td>
                    <td><span class="ledger-entry {% if task.status == 'completed' %}status-passed{% else %}status-failed{% endif %}">{{ task.status | upper }}</span></td>
                </tr>
                {% else %}
                <tr><td colspan="4" style="text-align: center;">No execution audit trail available.</td></tr>
                {% endfor %}
            </tbody>
        </table>
    </section>

    <footer style="margin-top: 50pt; border-top: 1pt solid #cbd5e0; padding-top: 10pt; font-size: 8pt; color: #718096; text-align: center;">
        Jasper Intelligence Engine | SEC-Compliant Data Traceability | Confidential Analyst Output
        <br/><span class="monospace">{{ report.query | hash }}</span>
    </footer>

</body>
</html>
