{% extends "base.html" %}

{% block title %}Dernier mail - Debug{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col">
            <h1><i class="fas fa-envelope"></i> Dernier mail reçu</h1>
            <p class="text-muted">Compte: {{ account.email }}</p>
        </div>
        <div class="col-auto">
            <a href="{{ url_for('outlook_test.index') }}" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> Retour
            </a>
        </div>
    </div>

    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">Informations du compte (DEBUG)</h5>
                </div>
                <div class="card-body">
                    <table class="table table-sm table-bordered">
                        <tr><th>ID</th><td><code>{{ account.id }}</code></td></tr>
                        <tr><th>Email</th><td><code>{{ account.email }}</code></td></tr>
                        <tr><th>Tenant ID</th><td><code>{{ account.tenant_id }}</code></td></tr>
                        <tr><th>Refresh Token</th><td><code style="font-size: 0.8em;">{{ account.refresh_token[:50] }}...</code></td></tr>
                        <tr><th>Statut</th><td><code>{{ account.connection_status }}</code></td></tr>
                        <tr><th>Actif</th><td><code>{{ account.is_active }}</code></td></tr>
                    </table>
                </div>
            </div>
        </div>
    </div>

    {% if mail_data %}
    <div class="row mt-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">Mail reçu (temps réel)</h5>
                </div>
                <div class="card-body">
                    <table class="table table-sm table-bordered">
                        <tr><th>Sujet</th><td><code>{{ mail_data.Subject | default('N/A') }}</code></td></tr>
                        <tr><th>Expéditeur</th><td><code>{{ mail_data.Sender.EmailAddress.Address if mail_data.Sender else 'N/A' }}</code></td></tr>
                        <tr><th>Date réception</th><td><code>{{ mail_data.ReceivedDateTime | paris_date }}</code></td></tr>
                        <tr><th>ID</th><td><code>{{ mail_data.Id | default('N/A') }}</code></td></tr>
                    </table>
                    <hr>
                    <h6>Corps du message (rendu HTML)</h6>
                    <div style="font-size: 0.85em; max-height: 400px; overflow: auto; border: 1px solid #ddd; padding: 10px; background: #fafafa;">
{% if mail_data.Body and mail_data.Body.Content %}{{ mail_data.Body.Content | safe }}{% elif mail_data.BodyPreview %}{{ mail_data.BodyPreview | safe }}{% else %}N/A{% endif %}
                    </div>
                    <hr>
                    <h6>Corps du message (texte brut - HTML supprimé)</h6>
                    <pre style="font-size: 0.85em; max-height: 400px; overflow: auto;">{% if mail_data.Body and mail_data.Body.Content %}{{ mail_data.Body.Content | striptags }}{% elif mail_data.BodyPreview %}{{ mail_data.BodyPreview | striptags }}{% else %}N/A{% endif %}</pre>
                </div>
            </div>
        </div>
    </div>
    {% elif mail %}
    <div class="row mt-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">Mail ID: {{ mail.id }}</h5>
                </div>
                <div class="card-body">
                    <table class="table table-sm table-bordered">
                        <tr><th>Message ID</th><td><code>{{ mail.message_id }}</code></td></tr>
                        <tr><th>Sujet</th><td><code>{{ mail.subject }}</code></td></tr>
                        <tr><th>Expéditeur</th><td><code>{{ mail.sender }}</code></td></tr>
                        <tr><th>Destinataire</th><td><code>{{ mail.recipient }}</code></td></tr>
                        <tr><th>Reçu le</th><td><code>{{ mail.received_at }}</code></td></tr>
                        <tr><th>Lu</th><td><code>{{ mail.is_read }}</code></td></tr>
                    </table>
                    <hr>
                    <h6>Corps du message (DEBUG)</h6>
                    <pre style="font-size: 0.85em; max-height: 400px; overflow: auto;">{{ mail.body }}</pre>
                </div>
            </div>
        </div>
    </div>
    {% else %}
    <div class="row mt-4">
        <div class="col-12">
            <div class="alert alert-warning">
                <i class="fas fa-exclamation-triangle"></i> Aucun mail trouvé pour ce compte.
            </div>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}