{% extends 'emails/base_email_template.html' %}
{% load i18n %}

{% block title %}PX360 {{ period_label }} Digest{% endblock %}
{% block preheader %}PX360 {% if period == 'weekly' %}Weekly{% else %}Monthly{% endif %} Digest - {{ period_label }}{% endblock %}

{% block content %}
<p style="margin: 0 0 5px;"><strong>PX360 {% if period == 'weekly' %}Weekly{% else %}Monthly{% endif %} Digest</strong></p>
{% if hospital_name %}
<p style="margin: 0 0 20px; font-size: 14px; color: #666666;">{{ hospital_name }} - {{ period_label }}</p>
{% else %}
<p style="margin: 0 0 20px; font-size: 14px; color: #666666;">{{ period_label }}</p>
{% endif %}

{% if summary %}
<p style="margin: 0 0 5px;"><strong>{% trans "AI Executive Summary" %}</strong></p>
<p style="margin: 0 0 10px;">{{ summary.summary_en }}</p>
{% if summary.key_findings_en %}
{% for f in summary.key_findings_en %}
<p style="margin: 0 0 3px; font-size: 14px;">- {{ f }}</p>
{% endfor %}
{% endif %}
{% if summary.risk_level %}
<p style="margin: 0 0 20px; font-size: 14px;">Risk Level: {{ summary.risk_level|title }}</p>
{% else %}
<p style="margin: 0 0 20px;">&nbsp;</p>
{% endif %}
{% endif %}

<p style="margin: 0 0 10px;"><strong>{% trans "Key Metrics" %}</strong></p>
<table width="100%" cellpadding="8" cellspacing="0" border="1" style="border-collapse: collapse; border-color: #dddddd; font-size: 14px; margin-bottom: 20px;">
    <tr>
        <td style="text-align: center;"><strong>{{ metrics.total_complaints|default:"&mdash;" }}</strong><br><span style="font-size: 12px; color: #666666;">Complaints</span></td>
        <td style="text-align: center;"><strong>{{ metrics.sla_compliance|default:"&mdash;" }}%</strong><br><span style="font-size: 12px; color: #666666;">SLA Compliance</span></td>
        <td style="text-align: center;"><strong>{{ metrics.nps_score|default:"&mdash;" }}</strong><br><span style="font-size: 12px; color: #666666;">NPS Score</span></td>
    </tr>
    <tr>
        <td style="text-align: center;"><strong>{{ metrics.avg_survey_score|default:"&mdash;" }}</strong><br><span style="font-size: 12px; color: #666666;">Avg Survey</span></td>
        <td style="text-align: center;"><strong>{{ metrics.total_actions|default:"&mdash;" }}</strong><br><span style="font-size: 12px; color: #666666;">Actions</span></td>
        <td style="text-align: center;"><strong>{{ metrics.avg_resolution_hours|default:"&mdash;" }}h</strong><br><span style="font-size: 12px; color: #666666;">Avg Resolution</span></td>
    </tr>
</table>

{% if early_warnings %}
<p style="margin: 0 0 10px;"><strong>{% trans "Early Warning Alerts" %}</strong></p>
<table width="100%" cellpadding="8" cellspacing="0" border="1" style="border-collapse: collapse; border-color: #dddddd; font-size: 13px; margin-bottom: 20px;">
    <tr>
        <td style="font-weight: bold; background-color: #f9f9f9;">Department</td>
        <td style="font-weight: bold; background-color: #f9f9f9;">Risk</td>
        <td style="font-weight: bold; background-color: #f9f9f9;">Level</td>
        <td style="font-weight: bold; background-color: #f9f9f9;">Signals</td>
    </tr>
    {% for dept in early_warnings %}
    <tr>
        <td>{{ dept.department_name }}</td>
        <td>{{ dept.risk_score }}%</td>
        <td>{% if dept.risk_level == 'critical' %}Critical{% elif dept.risk_level == 'high' %}High{% elif dept.risk_level == 'medium' %}Medium{% else %}Low{% endif %}</td>
        <td>{{ dept.active_signals }}/5</td>
    </tr>
    {% endfor %}
</table>
{% endif %}

{% if recommendations %}
<p style="margin: 0 0 10px;"><strong>{% trans "AI Recommendations" %}</strong></p>
{% for rec in recommendations %}
<p style="margin: 0 0 3px;"><strong>{{ rec.category }}</strong> ({{ rec.complaint_count }} complaints)</p>
<p style="margin: 0 0 5px; font-size: 14px;">{{ rec.problem_summary_en }}</p>
{% if rec.recommended_actions_en %}
{% for action in rec.recommended_actions_en %}
<p style="margin: 0 0 3px; font-size: 14px; color: #666666;">- {{ action }}</p>
{% endfor %}
{% endif %}
{% if not forloop.last %}<p style="margin: 0 0 10px;">&nbsp;</p>{% endif %}
{% endfor %}
{% endif %}

{% if dashboard_url %}
<p style="margin: 20px 0 10px;">
    <a href="{{ dashboard_url }}" style="display: inline-block; padding: 12px 32px; font-size: 16px; font-weight: 600; color: #ffffff; text-decoration: none; border-radius: 6px; background-color: #005696;">
        {% trans "View Full Dashboard" %}
    </a>
</p>
{% endif %}

{% if summary and summary.summary_ar %}
<p style="margin: 25px 0 5px; direction: rtl; text-align: right;"><strong>الملخص العربي</strong></p>
<p style="margin: 0 0 10px; direction: rtl; text-align: right;">{{ summary.summary_ar }}</p>
{% if summary.key_findings_ar %}
{% for f in summary.key_findings_ar %}
<p style="margin: 0 0 3px; font-size: 14px; direction: rtl; text-align: right;">- {{ f }}</p>
{% endfor %}
{% endif %}
{% endif %}

<p style="margin: 20px 0 0; font-size: 12px; color: #999999;">
    This report was automatically generated by PX360 - Patient Experience Analytics Platform
</p>
{% endblock %}
