<!DOCTYPE html>
<html>
<head>
    <title>DMARC Report</title>
</head>
<body>
    {% if report.report_metadata.is_some() %}
        {% let meta = report.report_metadata.as_ref().unwrap() %}
        <b>Reporter:</b> {{ meta.org_name }}<br>
        <b>Contact:</b> {{ meta.email }}<br>
        <b>Report ID:</b> {{ meta.report_id }}<br>
        {% if meta.date_range.is_some() %}
            {% let dr = meta.date_range.as_ref().unwrap() %}
            <b>Date range:</b>
            {{ dr.begin }}
            to
            {{ dr.end }}
            <br>
        {% endif %}
    {% endif %}

    {% if report.policy_published.is_some() %}
        {% let pol = report.policy_published.as_ref().unwrap() %}
        <b>Policy Published:</b>
        <ul>
            <li>Domain: {{ pol.domain }}</li>
            <li>ADKIM: {{ pol.adkim }}</li>
            <li>ASPF: {{ pol.aspf }}</li>
            <li>Policy: {{ pol.p }}</li>
            <li>Subdomain Policy: {{ pol.sp }}</li>
            <li>Percent: {{ pol.pct }}</li>
        </ul>
    {% endif %}

    {% if report.record.is_some() %}
        <b>Records:</b>
        <table style="border-collapse:collapse;width:100%;font-size:0.95em;">
            <thead>
                <tr style="background:#f0f0f0;">
                    <th style="border:1px solid #bbb;padding:4px 8px;">Source IP</th>
                    <th style="border:1px solid #bbb;padding:4px 8px;">Count</th>
                    <th style="border:1px solid #bbb;padding:4px 8px;">Header From</th>
                    {% if report.has_envelope_to %}
                    <th style="border:1px solid #bbb;padding:4px 8px;">Envelope To</th>
                    {% endif %}
                    <th style="border:1px solid #bbb;padding:4px 8px;">Disposition</th>
                    <th style="border:1px solid #bbb;padding:4px 8px;">DKIM</th>
                    <th style="border:1px solid #bbb;padding:4px 8px;">SPF</th>
                    <th style="border:1px solid #bbb;padding:4px 8px;">Auth Results</th>
                </tr>
            </thead>
            <tbody>
                {% for rec in report.record.as_ref().unwrap() %}
                    <tr>
                        <td style="border:1px solid #bbb;padding:4px 8px;">{{ rec.source_ip }}</td>
                        <td style="border:1px solid #bbb;padding:4px 8px;">{{ rec.count }}</td>
                        <td style="border:1px solid #bbb;padding:4px 8px;">{{ rec.header_from }}</td>
                        {% if report.has_envelope_to %}
                        <td style="border:1px solid #bbb;padding:4px 8px;">{{ rec.envelope_to }}</td>
                        {% endif %}
                        <td style="border:1px solid #bbb;padding:4px 8px;">{{ rec.disposition }}</td>
                        <td style="border:1px solid #bbb;padding:4px 8px;">{{ rec.dkim }}</td>
                        <td style="border:1px solid #bbb;padding:4px 8px;">{{ rec.spf }}</td>
                        <td style="border:1px solid #bbb;padding:4px 8px;">
                            {% if rec.auth_results.is_some() %}
                                {% let auth = rec.auth_results.as_ref().unwrap() %}
                                {% for dkimres in auth.dkim %}
                                        <span style="white-space:nowrap;">
                                            DKIM: domain=<b>{{ dkimres.domain }}</b>
                                            selector=<b>{{ dkimres.selector }}</b>
                                            result=<b>{{ dkimres.result }}</b>
                                        </span><br>
                                    {% endfor %}

                                {% for spfres in auth.spf %}
                                        <span style="white-space:nowrap;">
                                            SPF: domain=<b>{{ spfres.domain }}</b>
                                            scope=<b>{{ spfres.scope }}</b>
                                            result=<b>{{ spfres.result }}</b>
                                        </span><br>
                                    {% endfor %}

                            {% for reason in rec.reason %}
                                <span style="white-space:nowrap;">Reason: {{ reason }}</span><br>
                            {% endfor %}

                            {% endif %}
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    {% endif %}

    {% if report.report_metadata.is_none() && report.policy_published.is_none() && report.record.is_none() %}
        <p>No DMARC summary found.</p>
    {% endif %}
</body>
</html>
