{% load i18n %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% trans "Complaint Report" %} - {{ complaint.reference_number }}</title>
    <style>
        @page {
            size: A4;
            margin: 1.5cm;
            
            @top-left {
                content: "{{ css_system_name }}";
                font-size: 9pt;
                color: #667eea;
                font-weight: 600;
            }
            
            @top-right {
                content: "{{ complaint.reference_number }}";
                font-size: 9pt;
                color: #666;
            }
            
            @bottom-center {
                content: "{{ css_page_label }} " counter(page) " {{ css_of_label }} " counter(pages);
                font-size: 9pt;
                color: #999;
            }
            
            @bottom-left {
                content: "{{ css_generated_label }}: {% now 'M d, Y H:i' %}";
                font-size: 8pt;
                color: #999;
            }
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            font-size: 10pt;
            line-height: 1.6;
            color: #333;
        }

        /* Header Section */
        .report-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 25px;
            border-radius: 10px;
            margin-bottom: 20px;
            box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
        }

        .report-header h1 {
            font-size: 20pt;
            font-weight: 700;
            margin-bottom: 12px;
            letter-spacing: -0.5px;
        }

        .header-meta {
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
            font-size: 10pt;
            opacity: 0.95;
        }

        .header-meta-item {
            display: flex;
            align-items: center;
            gap: 5px;
        }

        .header-meta-item strong {
            font-weight: 600;
        }

        /* Status Badge */
        .status-badge {
            display: inline-block;
            padding: 6px 14px;
            border-radius: 20px;
            font-size: 9pt;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .status-open { background: #e3f2fd; color: #1565c0; }
        .status-in_progress { background: #fff3e0; color: #ef6c00; }
        .status-partially_resolved { background: #fff8e1; color: #f9a825; }
        .status-resolved { background: #e8f5e9; color: #2e7d32; }
        .status-closed { background: #f5f5f5; color: #616161; }
        .status-cancelled { background: #ffebee; color: #c62828; }

        .severity-badge {
            display: inline-block;
            padding: 4px 10px;
            border-radius: 15px;
            font-size: 8pt;
            font-weight: 700;
        }

        .severity-low { background: #e8f5e9; color: #2e7d32; }
        .severity-medium { background: #fff3e0; color: #ef6c00; }
        .severity-high { background: #ffebee; color: #c62828; }
        .severity-critical { background: #880e4f; color: #fff; }

        /* Section Styling */
        .section {
            margin-bottom: 20px;
            page-break-inside: avoid;
        }

        .section-title {
            font-size: 13pt;
            font-weight: 700;
            color: #667eea;
            border-bottom: 3px solid #667eea;
            padding-bottom: 8px;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .section-title .icon {
            font-size: 16pt;
        }

        /* Info Grid */
        .info-grid {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 12px;
            margin-bottom: 15px;
        }

        .info-card {
            background: #f8f9fa;
            padding: 12px 15px;
            border-radius: 8px;
            border-left: 4px solid #667eea;
        }

        .info-card.full-width {
            grid-column: 1 / -1;
        }

        .info-label {
            font-size: 8pt;
            color: #666;
            text-transform: uppercase;
            letter-spacing: 0.8px;
            margin-bottom: 4px;
            font-weight: 600;
        }

        .info-value {
            font-size: 10pt;
            color: #212529;
            font-weight: 500;
        }

        /* Content Boxes */
        .content-box {
            background: #fff;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            padding: 15px;
            line-height: 1.7;
        }

        .content-box.highlight {
            background: linear-gradient(135deg, #f3e5f5 0%, #e8eaf6 100%);
            border-left: 4px solid #7b1fa2;
        }

        /* Staff Section */
        .staff-box {
            background: linear-gradient(135deg, #e3f2fd 0%, #bbdefb 100%);
            border-radius: 8px;
            padding: 15px;
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .staff-avatar {
            width: 50px;
            height: 50px;
            background: #1976d2;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 20pt;
            font-weight: 700;
        }

        .staff-info {
            flex: 1;
        }

        .staff-name {
            font-size: 12pt;
            font-weight: 700;
            color: #1565c0;
        }

        .staff-meta {
            font-size: 9pt;
            color: #666;
            margin-top: 3px;
        }

        /* Explanation Section */
        .explanation-card {
            background: #fff;
            border: 2px solid #e0e0e0;
            border-radius: 10px;
            padding: 15px;
            margin-bottom: 15px;
            page-break-inside: avoid;
        }

        .explanation-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10px;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
        }

        .explanation-author {
            font-weight: 700;
            color: #333;
            font-size: 11pt;
        }

        .explanation-type {
            font-size: 8pt;
            color: #666;
        }

        .acceptance-badge {
            display: inline-block;
            padding: 5px 12px;
            border-radius: 15px;
            font-size: 8pt;
            font-weight: 700;
        }

        .acceptance-acceptable {
            background: #e8f5e9;
            color: #2e7d32;
        }

        .acceptance-not_acceptable {
            background: #ffebee;
            color: #c62828;
        }

        .acceptance-pending {
            background: #fff3e0;
            color: #ef6c00;
        }

        .explanation-content {
            line-height: 1.7;
            color: #444;
        }

        .explanation-meta {
            margin-top: 10px;
            padding-top: 10px;
            border-top: 1px dashed #ddd;
            font-size: 8pt;
            color: #888;
        }

        /* AI Analysis Section */
        .ai-section {
            background: linear-gradient(135deg, #f3e5f5 0%, #e8eaf6 100%);
            border-radius: 10px;
            padding: 20px;
        }

        .ai-box {
            background: white;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 12px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
        }

        .ai-box-label {
            font-size: 8pt;
            color: #7b1fa2;
            text-transform: uppercase;
            letter-spacing: 1px;
            font-weight: 700;
            margin-bottom: 8px;
        }

        .emotion-indicator {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-top: 8px;
        }

        .emotion-bar {
            flex: 1;
            height: 10px;
            background: #e0e0e0;
            border-radius: 5px;
            overflow: hidden;
        }

        .emotion-fill {
            height: 100%;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
        }

        .emotion-value {
            font-size: 9pt;
            font-weight: 700;
            color: #7b1fa2;
            min-width: 40px;
            text-align: right;
        }

        /* Resolution Section */
        .resolution-card {
            background: linear-gradient(135deg, #e8f5e9 0%, #c8e6c9 100%);
            border-radius: 10px;
            padding: 20px;
            border-left: 5px solid #4caf50;
        }

        .resolution-header {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 15px;
        }

        .resolution-icon {
            font-size: 24pt;
        }

        .resolution-title {
            font-size: 14pt;
            font-weight: 700;
            color: #2e7d32;
        }

        .resolution-content {
            line-height: 1.8;
            color: #1b5e20;
        }

        .resolution-meta {
            margin-top: 15px;
            padding-top: 15px;
            border-top: 1px dashed #81c784;
            font-size: 9pt;
            color: #388e3c;
        }

        /* Timeline */
        .timeline-entry {
            padding-left: 20px;
            border-left: 3px solid #667eea;
            margin-bottom: 12px;
            position: relative;
        }

        .timeline-entry::before {
            content: "";
            position: absolute;
            left: -6px;
            top: 5px;
            width: 10px;
            height: 10px;
            background: #667eea;
            border-radius: 50%;
        }

        .timeline-time {
            font-size: 8pt;
            color: #888;
            margin-bottom: 3px;
        }

        .timeline-content {
            font-size: 9pt;
            color: #444;
        }

        .timeline-author {
            font-size: 8pt;
            color: #667eea;
            margin-top: 3px;
        }

        /* PX Actions */
        .px-action-card {
            background: #fff3e0;
            border-radius: 8px;
            padding: 12px;
            margin-bottom: 10px;
            border-left: 4px solid #ff9800;
        }

        .px-action-title {
            font-weight: 700;
            color: #e65100;
            font-size: 10pt;
        }

        .px-action-meta {
            font-size: 8pt;
            color: #666;
            margin-top: 4px;
        }

        /* Footer */
        .report-footer {
            margin-top: 30px;
            padding-top: 20px;
            border-top: 2px solid #e0e0e0;
            text-align: center;
            color: #888;
            font-size: 9pt;
        }

        .report-footer .logo {
            font-size: 12pt;
            font-weight: 700;
            color: #667eea;
            margin-bottom: 8px;
        }

        /* Page Break */
        .page-break {
            page-break-before: always;
        }

        /* Utility Classes */
        .text-muted { color: #666; }
        .text-small { font-size: 8pt; }
        .mt-2 { margin-top: 10px; }
        .mb-2 { margin-bottom: 10px; }
        .d-flex { display: flex; }
        .align-center { align-items: center; }
        .gap-2 { gap: 10px; }
    </style>
</head>
<body>
    <!-- Header -->
    <div class="report-header">
        <h1>{{ complaint.title }}</h1>
        <div class="header-meta">
            <div class="header-meta-item">
                <strong>{% trans "Ref:" %}</strong> {{ complaint.reference_number }}
            </div>
            <div class="header-meta-item">
                <strong>{% trans "Status:" %}</strong>
                <span class="status-badge status-{{ complaint.status }}">{{ complaint.get_status_display }}</span>
            </div>
            <div class="header-meta-item">
                <strong>{% trans "Severity:" %}</strong>
                <span class="severity-badge severity-{{ complaint.severity }}">{{ complaint.get_severity_display }}</span>
            </div>
            <div class="header-meta-item">
                <strong>{% trans "Priority:" %}</strong> {{ complaint.get_priority_display }}
            </div>
        </div>
    </div>

    <!-- Patient & Location Information -->
    <div class="section">
        <h2 class="section-title"><span class="icon">👤</span> {% trans "Patient Information" %}</h2>
        <div class="info-grid">
            <div class="info-card">
                <div class="info-label">{% trans "Patient Name" %}</div>
                <div class="info-value">
                    {% if complaint.patient %}
                        {{ complaint.patient.get_full_name }}
                    {% else %}
                        {{ complaint.contact_name|default:"Anonymous" }}
                    {% endif %}
                </div>
            </div>
            <div class="info-card">
                <div class="info-label">{% trans "MRN / National ID" %}</div>
                <div class="info-value">
                    {% if complaint.patient %}
                        {{ complaint.patient.mrn }}
                    {% else %}
                        {{ complaint.national_id|default:"N/A" }}
                    {% endif %}
                </div>
            </div>
            <div class="info-card">
                <div class="info-label">{% trans "Hospital" %}</div>
                <div class="info-value">{{ complaint.hospital.name }}</div>
            </div>
            <div class="info-card">
                <div class="info-label">{% trans "Department" %}</div>
                <div class="info-value">{{ complaint.department.name|default:"Not Assigned" }}</div>
            </div>
            <div class="info-card">
                <div class="info-label">{% trans "Location" %}</div>
                <div class="info-value">
                    {% if complaint.legacy_location %}{{ complaint.legacy_location.name }}{% endif %}
                    {% if complaint.legacy_main_section %} / {{ complaint.legacy_main_section.name }}{% endif %}
                    {% if complaint.legacy_subsection %} / {{ complaint.legacy_subsection.name }}{% endif %}
                </div>
            </div>
            <div class="info-card">
                <div class="info-label">{% trans "Incident Date" %}</div>
                <div class="info-value">{{ complaint.incident_date|date:"M d, Y"|default:"Not Specified" }}</div>
            </div>
        </div>
    </div>

    <!-- Complaint Details -->
    <div class="section">
        <h2 class="section-title"><span class="icon">📝</span> {% trans "Complaint Details" %}</h2>
        <div class="info-grid">
            <div class="info-card">
                <div class="info-label">{% trans "Category" %}</div>
                <div class="info-value">{{ complaint.get_category_display|default:"Not Categorized" }}</div>
            </div>
            <div class="info-card">
                <div class="info-label">{% trans "Source" %}</div>
                <div class="info-value">{{ complaint.get_source_display }}</div>
            </div>
            <div class="info-card">
                <div class="info-label">{% trans "Created Date" %}</div>
                <div class="info-value">{{ complaint.created_at|date:"M d, Y H:i" }}</div>
            </div>
            <div class="info-card">
                <div class="info-label">{% trans "SLA Deadline" %}</div>
                <div class="info-value">{{ complaint.due_at|date:"M d, Y H:i" }}</div>
            </div>
        </div>
        
        <div class="content-box" style="margin-top: 15px;">
            <div class="info-label" style="margin-bottom: 8px;">{% trans "Description" %}</div>
            {{ complaint.description|linebreaks }}
        </div>
        
        {% if complaint.expected_result %}
        <div class="content-box highlight" style="margin-top: 15px;">
            <div class="info-label" style="margin-bottom: 8px;">{% trans "Patient Expected Result" %}</div>
            {{ complaint.expected_result|linebreaks }}
        </div>
        {% endif %}
    </div>

    <!-- Staff Assignment -->
    {% if complaint.staff %}
    <div class="section">
        <h2 class="section-title"><span class="icon">👨‍⚕️</span> {% trans "Staff Assignment" %}</h2>
        <div class="staff-box">
            <div class="staff-avatar">{{ complaint.staff.first_name|first|upper }}</div>
            <div class="staff-info">
                <div class="staff-name">{{ complaint.staff.get_full_name }}</div>
                <div class="staff-meta">
                    {% if complaint.staff.job_title %}{{ complaint.staff.job_title }}{% endif %}
                    {% if complaint.staff.department %} | {{ complaint.staff.department.name }}{% endif %}
                </div>
                {% if complaint.staff.report_to %}
                <div class="staff-meta" style="margin-top: 5px;">
                    <strong>{% trans "Reports to:" %}</strong> {{ complaint.staff.report_to.get_full_name }}
                </div>
                {% endif %}
            </div>
        </div>
    </div>
    {% endif %}

    <!-- Explanations -->
    {% if explanations %}
    <div class="section">
        <h2 class="section-title"><span class="icon">💬</span> {% trans "Staff Explanations" %}</h2>
        {% for exp in explanations %}
        <div class="explanation-card">
            <div class="explanation-header">
                <div>
                    <div class="explanation-author">
                        {% if exp.staff.get_full_name %}{{ exp.staff.get_full_name }}{% else %}{% trans "Unknown Staff" %}{% endif %}
                        {% if exp.metadata.is_escalation %}
                            <span style="font-size: 8pt; color: #666;">({% trans "Manager - Escalated" %})</span>
                        {% endif %}
                    </div>
                    <div class="explanation-type">
                        {% if exp.submitted_via == 'email_link' %}{% trans "Via Email Link" %}{% else %}{% trans "Direct Entry" %}{% endif %}
                        | {% trans "Submitted:" %} {{ exp.responded_at|date:"M d, Y H:i" }}
                    </div>
                </div>
                {% if exp.is_used %}
                    {% if exp.acceptance_status == 'acceptable' %}
                        <span class="acceptance-badge acceptance-acceptable">✓ {% trans "Acceptable" %}</span>
                    {% elif exp.acceptance_status == 'not_acceptable' %}
                        <span class="acceptance-badge acceptance-not_acceptable">✗ {% trans "Not Acceptable" %}</span>
                    {% else %}
                        <span class="acceptance-badge acceptance-pending">⏳ {% trans "Pending Review" %}</span>
                    {% endif %}
                {% else %}
                    <span class="acceptance-badge acceptance-pending">⏳ {% trans "Awaiting Response" %}</span>
                {% endif %}
            </div>
            
            {% if exp.is_used and exp.explanation %}
            <div class="explanation-content">
                {{ exp.explanation|linebreaks }}
            </div>
            
            {% if exp.acceptance_notes %}
            <div style="margin-top: 10px; padding: 8px; background: #f5f5f5; border-radius: 5px; font-size: 9pt;">
                <strong>{% trans "Review Notes:" %}</strong> {{ exp.acceptance_notes }}
            </div>
            {% endif %}
            
            <div class="explanation-meta">
                {% if exp.accepted_by %}
                    {% trans "Reviewed by" %} {{ exp.accepted_by.get_full_name }} {% trans "on" %} {{ exp.accepted_at|date:"M d, Y" }}
                {% endif %}
                {% if exp.attachments.count > 0 %}
                    | {% blocktrans count counter=exp.attachments.count %}{{ counter }} attachment{% plural %}{{ counter }} attachments{% endblocktrans %}
                {% endif %}
            </div>
            {% else %}
            <div class="explanation-content" style="color: #888; font-style: italic;">
                {% trans "Explanation not yet submitted." %} {% trans "Request sent on" %} {{ exp.email_sent_at|date:"M d, Y" }}
            </div>
            {% endif %}
        </div>
        {% endfor %}
    </div>
    {% endif %}

    <!-- AI Analysis -->
    {% if complaint.short_description_en or complaint.suggested_action_en or complaint.emotion %}
    <div class="section">
        <h2 class="section-title"><span class="icon">🤖</span> {% trans "AI Analysis" %}</h2>
        <div class="ai-section">
            {% if complaint.emotion %}
            <div class="ai-box">
                <div class="ai-box-label">{% trans "Emotion Analysis" %}</div>
                <div style="display: flex; align-items: center; gap: 10px;">
                    <span class="status-badge" style="background: #7b1fa2; color: white;">
                        {{ complaint.get_emotion_display }}
                    </span>
                    <span style="font-size: 9pt; color: #666;">
                        {% trans "Confidence:" %} {{ complaint.emotion_confidence|floatformat:0 }}%
                    </span>
                </div>
                <div class="emotion-indicator">
                    <span style="font-size: 8pt; color: #666;">{% trans "Intensity" %}</span>
                    <div class="emotion-bar">
                        <div class="emotion-fill" style="width: {{ complaint.emotion_intensity|floatformat:0 }}%;"></div>
                    </div>
                    <span class="emotion-value">{{ complaint.emotion_intensity|floatformat:2 }}</span>
                </div>
            </div>
            {% endif %}

            {% if complaint.short_description_en %}
            <div class="ai-box">
                <div class="ai-box-label">{% trans "AI Summary" %}</div>
                <div>{{ complaint.short_description_en }}</div>
                {% if complaint.short_description_ar %}
                <div style="margin-top: 8px; padding-top: 8px; border-top: 1px dashed #ccc; direction: rtl; text-align: right;">
                    {{ complaint.short_description_ar }}
                </div>
                {% endif %}
            </div>
            {% endif %}

            {% if complaint.suggested_action_en %}
            <div class="ai-box" style="background: linear-gradient(135deg, #e8f5e9 0%, #c8e6c9 100%); border: 1px solid #4caf50;">
                <div class="ai-box-label" style="color: #2e7d32;">💡 {% trans "Suggested Action" %}</div>
                <div style="color: #1b5e20;">{{ complaint.suggested_action_en }}</div>
                {% if complaint.suggested_action_ar %}
                <div style="margin-top: 8px; padding-top: 8px; border-top: 1px dashed #81c784; direction: rtl; text-align: right; color: #1b5e20;">
                    {{ complaint.suggested_action_ar }}
                </div>
                {% endif %}
            </div>
            {% endif %}

            {% if complaint.reasoning_en %}
            <div class="ai-box">
                <div class="ai-box-label">{% trans "AI Reasoning" %}</div>
                <div style="font-size: 9pt; color: #555;">{{ complaint.reasoning_en }}</div>
            </div>
            {% endif %}
        </div>
    </div>
    {% endif %}

    <!-- PX Actions -->
    {% if px_actions %}
    <div class="section">
        <h2 class="section-title"><span class="icon">⚡</span> {% trans "Related PX Actions" %}</h2>
        {% for action in px_actions %}
        <div class="px-action-card">
            <div class="px-action-title">{{ action.title }}</div>
            <div class="px-action-meta">
                {% trans "Status:" %} {{ action.get_status_display }} | 
                {% trans "Priority:" %} {{ action.get_priority_display }} | 
                {% trans "Created:" %} {{ action.created_at|date:"M d, Y" }}
            </div>
        </div>
        {% endfor %}
    </div>
    {% endif %}

    <!-- Resolution -->
    {% if complaint.resolution %}
    <div class="section page-break">
        <h2 class="section-title"><span class="icon">✅</span> {% trans "Resolution" %}</h2>
        <div class="resolution-card">
            <div class="resolution-header">
                <span class="resolution-icon">✓</span>
                <span class="resolution-title">{% trans "Complaint Resolved" %}</span>
            </div>
            <div class="resolution-content">
                {{ complaint.resolution|linebreaks }}
            </div>
            {% if complaint.resolution_category %}
            <div style="margin-top: 10px;">
                <span style="font-weight: 600;">{% trans "Category:" %}</span> {{ complaint.get_resolution_category_display }}
            </div>
            {% endif %}
            <div class="resolution-meta">
                <strong>{% trans "Resolved by:" %}</strong> {{ complaint.resolved_by.get_full_name|default:"N/A" }}
                <br>
                <strong>{% trans "Resolved on:" %}</strong> {{ complaint.resolved_at|date:"F d, Y at H:i"|default:"N/A" }}
            </div>
        </div>
    </div>
    {% endif %}

    <!-- Recent Timeline -->
    {% if timeline %}
    <div class="section">
        <h2 class="section-title"><span class="icon">📋</span> {% trans "Recent Activity" %}</h2>
        {% for update in timeline %}
        <div class="timeline-entry">
            <div class="timeline-time">{{ update.created_at|date:"M d, Y H:i" }}</div>
            <div class="timeline-content">{{ update.message }}</div>
            {% if update.created_by %}
            <div class="timeline-author">{% trans "by" %} {{ update.created_by.get_full_name }}</div>
            {% endif %}
        </div>
        {% endfor %}
    </div>
    {% endif %}

    <!-- Footer -->
    <div class="report-footer">
        <div class="logo">{% trans "PX360 Patient Experience Management" %}</div>
        <div>{% trans "This report was generated automatically from the PX360 system." %}</div>
        <div style="margin-top: 5px;">© {% now "Y" %} {% trans "AlHammadi Group. All rights reserved." %}</div>
        <div style="margin-top: 3px; font-size: 10px; color: #999;">Powered by <a href="https://tenhal.sa" style="color: #005696;">tenhal.sa</a></div>
    </div>
</body>
</html>
