{% extends "layouts/base.html" %}
{% load i18n %}

{% block title %}{% trans "Survey Comments" %} - PX360{% endblock %}

{% block extra_css %}
<style>
.page-header-gradient {
    background: linear-gradient(135deg, #005696 0%, #0069a8 50%, #007bbd 100%);
    color: white;
    padding: 1.5rem 2rem;
    border-radius: 1rem;
    margin-bottom: 1.5rem;
    box-shadow: 0 10px 15px -3px rgba(0, 86, 150, 0.2);
}

.section-card {
    background: white;
    border-radius: 1rem;
    border: 2px solid #e2e8f0;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    transition: all 0.3s ease;
}
.section-card:hover {
    border-color: #005696;
    box-shadow: 0 10px 25px -5px rgba(0, 86, 150, 0.15);
}

.section-header {
    padding: 1rem 1.5rem;
    border-bottom: 2px solid #e2e8f0;
    background: linear-gradient(to right, #f8fafc, #f1f5f9);
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.section-icon {
    width: 40px;
    height: 40px;
    border-radius: 0.75rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

.stat-card {
    background: white;
    border-radius: 1rem;
    border: 2px solid #e2e8f0;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    padding: 1.5rem;
    transition: all 0.3s ease;
}
.stat-card:hover {
    border-color: #005696;
    box-shadow: 0 10px 25px -5px rgba(0, 86, 150, 0.15);
}
</style>
{% endblock %}

{% block content %}
<!-- Page Header -->
<div class="page-header-gradient">
    <div class="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">
        <div>
            <h1 class="text-2xl font-bold flex items-center gap-3">
                <div class="section-icon bg-white/20">
                    <i data-lucide="message-square-quote" class="w-6 h-6 text-white"></i>
                </div>
                {% trans "Survey Comments" %}
            </h1>
            <p class="text-white/80 mt-2">{% trans "View all patient comments with AI-powered sentiment analysis" %}</p>
        </div>
    </div>
</div>

<!-- Statistics Cards -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-6">
    <div class="stat-card">
        <div class="flex justify-between items-start mb-4">
            <div class="section-icon bg-blue-100">
                <i data-lucide="message-square-quote" class="w-5 h-5 text-blue-600"></i>
            </div>
            <span class="text-blue-600 text-xs font-bold bg-blue-50 px-2 py-1 rounded-lg">
                {% trans "Total" %}
            </span>
        </div>
        <div class="text-3xl font-bold text-navy mb-1">{{ stats.total }}</div>
        <div class="text-slate text-sm font-medium">{% trans "Comments" %}</div>
    </div>

    <div class="stat-card">
        <div class="flex justify-between items-start mb-4">
            <div class="section-icon bg-green-100">
                <i data-lucide="smile" class="w-5 h-5 text-green-600"></i>
            </div>
            <span class="text-green-600 text-xs font-bold bg-green-50 px-2 py-1 rounded-lg">
                {% trans "Positive" %}
            </span>
        </div>
        <div class="text-3xl font-bold text-green-600 mb-1">{{ stats.positive }}</div>
        <div class="text-slate text-sm font-medium">{% trans "Sentiment" %}</div>
    </div>

    <div class="stat-card">
        <div class="flex justify-between items-start mb-4">
            <div class="section-icon bg-red-100">
                <i data-lucide="frown" class="w-5 h-5 text-red-600"></i>
            </div>
            <span class="text-red-600 text-xs font-bold bg-red-50 px-2 py-1 rounded-lg">
                {% trans "Negative" %}
            </span>
        </div>
        <div class="text-3xl font-bold text-red-600 mb-1">{{ stats.negative }}</div>
        <div class="text-slate text-sm font-medium">{% trans "Sentiment" %}</div>
    </div>

    <div class="stat-card">
        <div class="flex justify-between items-start mb-4">
            <div class="section-icon bg-purple-100">
                <i data-lucide="bot" class="w-5 h-5 text-purple-600"></i>
            </div>
            <span class="text-purple-600 text-xs font-bold bg-purple-50 px-2 py-1 rounded-lg">
                {% trans "AI" %}
            </span>
        </div>
        <div class="text-3xl font-bold text-purple-600 mb-1">{{ stats.analyzed }}</div>
        <div class="text-slate text-sm font-medium">{% trans "Analyzed" %}</div>
    </div>
</div>

<!-- Charts -->
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-6">
    <div class="section-card">
        <div class="section-header">
            <div class="section-icon bg-blue-100">
                <i data-lucide="pie-chart" class="w-5 h-5 text-blue-600"></i>
            </div>
            <h3 class="font-bold text-navy">{% trans "Patient Type Distribution" %}</h3>
        </div>
        <div class="p-6">
            <div id="patientTypeDistributionChart" style="min-height: 300px;"></div>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <div class="section-icon bg-purple-100">
                <i data-lucide="bar-chart-3" class="w-5 h-5 text-purple-600"></i>
            </div>
            <h3 class="font-bold text-navy">{% trans "Sentiment by Patient Type" %}</h3>
        </div>
        <div class="p-6">
            <div id="sentimentByPatientTypeChart" style="min-height: 300px;"></div>
        </div>
    </div>
</div>

<!-- Filters -->
<div class="section-card mb-6">
    <div class="section-header">
        <div class="section-icon bg-amber-100">
            <i data-lucide="search" class="w-5 h-5 text-amber-600"></i>
        </div>
        <h3 class="font-bold text-navy">{% trans "Search & Filter" %}</h3>
    </div>
    <div class="p-6">
        <form method="get" class="space-y-4">
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-7 gap-4">
                <!-- Search -->
                <div class="xl:col-span-2">
                    <label for="search" class="block text-sm font-bold text-slate mb-2">{% trans "Search" %}</label>
                    <div class="relative">
                        <i data-lucide="search" class="absolute left-3 top-1/2 -translate-y-1/2 w-5 h-5 text-slate-400"></i>
                        <input type="text" class="w-full pl-10 pr-4 py-2.5 border-2 border-slate-200 rounded-xl text-slate-800 focus:ring-2 focus:ring-navy focus:border-transparent transition" 
                               id="search" name="search" 
                               placeholder="{% trans 'MRN, name, or comment...' %}"
                               value="{{ filters.search }}">
                    </div>
                </div>

                <!-- Sentiment -->
                <div>
                    <label for="sentiment" class="block text-sm font-bold text-slate mb-2">{% trans "Sentiment" %}</label>
                    <select class="w-full px-4 py-2.5 border-2 border-slate-200 rounded-xl text-slate-800 focus:ring-2 focus:ring-navy focus:border-transparent transition" 
                            id="sentiment" name="sentiment">
                        <option value="">{% trans "All" %}</option>
                        <option value="positive" {% if filters.sentiment == 'positive' %}selected{% endif %}>{% trans "Positive" %}</option>
                        <option value="negative" {% if filters.sentiment == 'negative' %}selected{% endif %}>{% trans "Negative" %}</option>
                        <option value="neutral" {% if filters.sentiment == 'neutral' %}selected{% endif %}>{% trans "Neutral" %}</option>
                    </select>
                </div>

                <!-- Category -->
                <div>
                    <label for="comment_category" class="block text-sm font-bold text-slate mb-2">{% trans "Category" %}</label>
                    <select class="w-full px-4 py-2.5 border-2 border-slate-200 rounded-xl text-slate-800 focus:ring-2 focus:ring-navy focus:border-transparent transition" 
                            id="comment_category" name="comment_category">
                        <option value="">{% trans "All" %}</option>
                        <option value="complaint" {% if filters.comment_category == 'complaint' %}selected{% endif %}>{% trans "Complaint" %}</option>
                        <option value="negative" {% if filters.comment_category == 'negative' %}selected{% endif %}>{% trans "Negative" %}</option>
                        <option value="positive" {% if filters.comment_category == 'positive' %}selected{% endif %}>{% trans "Positive" %}</option>
                        <option value="suggestion" {% if filters.comment_category == 'suggestion' %}selected{% endif %}>{% trans "Suggestion" %}</option>
                        <option value="appreciation" {% if filters.comment_category == 'appreciation' %}selected{% endif %}>{% trans "Appreciation" %}</option>
                    </select>
                </div>

                <!-- Survey Type -->
                <div>
                    <label for="survey_type" class="block text-sm font-bold text-slate mb-2">{% trans "Survey Type" %}</label>
                    <select class="w-full px-4 py-2.5 border-2 border-slate-200 rounded-xl text-slate-800 focus:ring-2 focus:ring-navy focus:border-transparent transition" 
                            id="survey_type" name="survey_type">
                        <option value="">{% trans "All" %}</option>
                        <option value="stage" {% if filters.survey_type == 'stage' %}selected{% endif %}>{% trans "Journey Stage" %}</option>
                        <option value="complaint_resolution" {% if filters.survey_type == 'complaint_resolution' %}selected{% endif %}>{% trans "Complaint Resolution" %}</option>
                        <option value="general" {% if filters.survey_type == 'general' %}selected{% endif %}>{% trans "General" %}</option>
                        <option value="nps" {% if filters.survey_type == 'nps' %}selected{% endif %}>{% trans "NPS" %}</option>
                    </select>
                </div>

                <!-- Patient Type -->
                <div>
                    <label for="patient_type" class="block text-sm font-bold text-slate mb-2">{% trans "Patient Type" %}</label>
                    <select class="w-full px-4 py-2.5 border-2 border-slate-200 rounded-xl text-slate-800 focus:ring-2 focus:ring-navy focus:border-transparent transition" 
                            id="patient_type" name="patient_type">
                        <option value="">{% trans "All" %}</option>
                        <option value="outpatient" {% if filters.patient_type == 'outpatient' %}selected{% endif %}>{% trans "Outpatient" %}</option>
                        <option value="inpatient" {% if filters.patient_type == 'inpatient' %}selected{% endif %}>{% trans "Inpatient" %}</option>
                        <option value="emergency" {% if filters.patient_type == 'emergency' %}selected{% endif %}>{% trans "Emergency" %}</option>
                    </select>
                </div>

                <!-- Date Range -->
                <div class="xl:col-span-2">
                    <label class="block text-sm font-bold text-slate mb-2">{% trans "Date Range" %}</label>
                    <div class="flex gap-2">
                        <input type="date" class="flex-1 px-3 py-2.5 border-2 border-slate-200 rounded-xl text-slate-800 focus:ring-2 focus:ring-navy focus:border-transparent transition text-sm" 
                               id="date_from" name="date_from" value="{{ filters.date_from }}">
                        <input type="date" class="flex-1 px-3 py-2.5 border-2 border-slate-200 rounded-xl text-slate-800 focus:ring-2 focus:ring-navy focus:border-transparent transition text-sm" 
                               id="date_to" name="date_to" value="{{ filters.date_to }}">
                    </div>
                </div>
            </div>

            <div class="flex justify-between items-center pt-4 border-t border-slate-100">
                <button type="submit" class="bg-navy text-white px-6 py-2.5 rounded-xl font-semibold hover:bg-blue transition flex items-center gap-2">
                    <i data-lucide="search" class="w-4 h-4"></i> {% trans "Search" %}
                </button>
                <a href="{% url 'surveys:survey_comments_list' %}" class="text-slate hover:text-navy font-medium flex items-center gap-1">
                    <i data-lucide="x-circle" class="w-4 h-4"></i> {% trans "Clear Filters" %}
                </a>
            </div>
        </form>
    </div>
</div>

<!-- Comments List -->
<div class="section-card">
    <div class="section-header">
        <div class="section-icon bg-green-100">
            <i data-lucide="message-square" class="w-5 h-5 text-green-600"></i>
        </div>
        <h3 class="font-bold text-navy">{% trans "Comments List" %}</h3>
    </div>
    
    {% if page_obj.object_list %}
    <div class="overflow-x-auto">
        <table class="w-full">
            <thead class="bg-slate-50 border-b border-slate-100">
                <tr class="text-xs font-bold text-slate uppercase tracking-wider">
                    <th class="px-6 py-4 text-left">{% trans "Patient" %}</th>
                    <th class="px-6 py-4 text-left">{% trans "Type" %}</th>
                    <th class="px-6 py-4 text-left">{% trans "Comment" %}</th>
                    <th class="px-6 py-4 text-left">{% trans "Sentiment" %}</th>
                    <th class="px-6 py-4 text-left">{% trans "Survey" %}</th>
                    <th class="px-6 py-4 text-left">{% trans "Date" %}</th>
                    <th class="px-6 py-4 text-left">{% trans "Actions" %}</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-slate-100">
                {% for survey in page_obj.object_list %}
                <tr class="hover:bg-slate-50 transition">
                    <td class="px-6 py-4">
                        <div class="font-semibold text-navy">{{ survey.patient.get_full_name }}</div>
                        <div class="text-sm text-slate">{{ survey.patient.mrn }}</div>
                        {% if survey.survey_template.hospital %}
                        <div class="text-xs text-slate mt-1">{{ survey.survey_template.hospital.name }}</div>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4">
                        {% if survey.patient_type and survey.patient_type.code %}
                        <span class="inline-flex items-center gap-1 px-3 py-1 rounded-full text-xs font-bold {{ survey.patient_type.bg_class }} {{ survey.patient_type.text_class }}">
                            <i data-lucide="{{ survey.patient_type.icon }}" class="w-3 h-3"></i>
                            {{ survey.patient_type.label }}
                        </span>
                        {% else %}
                        <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-slate-100 text-slate-600">N/A</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4">
                        <div class="max-w-md overflow-hidden text-ellipsis whitespace-nowrap text-slate-700" title="{{ survey.comment }}">
                            {{ survey.comment }}
                        </div>
                        {% if survey.comment_analyzed %}
                        {% if survey.comment_analysis.comment_category %}
                        <div class="text-xs mt-1 flex items-center gap-1">
                            <span class="inline-flex items-center px-1.5 py-0.5 rounded text-[10px] font-bold uppercase
                                {% if survey.comment_analysis.comment_category == 'complaint' %}bg-red-100 text-red-700
                                {% elif survey.comment_analysis.comment_category == 'negative' %}bg-orange-100 text-orange-700
                                {% elif survey.comment_analysis.comment_category == 'positive' %}bg-green-100 text-green-700
                                {% elif survey.comment_analysis.comment_category == 'appreciation' %}bg-emerald-100 text-emerald-700
                                {% elif survey.comment_analysis.comment_category == 'suggestion' %}bg-blue-100 text-blue-700
                                {% endif %}">
                                {{ survey.comment_analysis.comment_category }}
                            </span>
                        </div>
                        {% endif %}
                        {% if survey.comment_analysis.emotion %}
                        <div class="text-xs text-slate-500 mt-1 flex items-center gap-1">
                            <i data-lucide="heart-pulse" class="w-3 h-3"></i> {{ survey.comment_analysis.emotion|title }}
                        </div>
                        {% endif %}
                        {% else %}
                        <div class="text-xs text-amber-500 mt-1 flex items-center gap-1">
                            <i data-lucide="hourglass" class="w-3 h-3"></i> {% trans "Analyzing..." %}
                        </div>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4">
                        {% if survey.comment_analyzed and survey.comment_analysis.sentiment %}
                        <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold {% if survey.comment_analysis.sentiment == 'positive' %}bg-green-100 text-green-700{% elif survey.comment_analysis.sentiment == 'negative' %}bg-red-100 text-red-700{% else %}bg-slate-100 text-slate-700{% endif %}">
                            {{ survey.comment_analysis.sentiment|title }}
                        </span>
                        {% else %}
                        <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-slate-100 text-slate-600">{% trans "Pending" %}</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4">
                        <div class="font-semibold text-navy text-sm">{{ survey.survey_template.name }}</div>
                        <div class="text-xs text-slate">{{ survey.survey_template.get_survey_type_display }}</div>
                        {% if survey.total_score %}
                        <div class="text-xs {% if survey.is_negative %}text-red-600{% else %}text-green-600{% endif %} mt-1">
                            <strong>Score: {{ survey.total_score|floatformat:1 }}/5.0</strong>
                        </div>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4">
                        <div class="text-sm text-slate-800">{{ survey.completed_at|date:"M d, Y" }}</div>
                        <div class="text-xs text-slate">{{ survey.completed_at|time:"H:i" }}</div>
                    </td>
                    <td class="px-6 py-4">
                        <a href="{% url 'surveys:instance_detail' survey.id %}" 
                           class="inline-flex items-center gap-1 px-3 py-2 text-navy bg-light rounded-lg hover:bg-slate-200 transition font-medium text-sm">
                            <i data-lucide="eye" class="w-4 h-4"></i> {% trans "View" %}
                        </a>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <div class="px-6 py-4 border-t border-slate-100">
        <div class="flex items-center justify-between">
            <div class="text-sm text-slate">
                {% trans "Showing" %} <span class="font-medium text-navy">{{ page_obj.start_index }}</span> 
                {% trans "to" %} <span class="font-medium text-navy">{{ page_obj.end_index }}</span> 
                {% trans "of" %} <span class="font-medium text-navy">{{ page_obj.paginator.count }}</span> 
                {% trans "results" %}
            </div>
            <nav class="flex gap-2">
                {% if page_obj.has_previous %}
                <a href="?page=1{% if filters.search %}&search={{ filters.search }}{% endif %}{% if filters.sentiment %}&sentiment={{ filters.sentiment }}{% endif %}{% if filters.survey_type %}&survey_type={{ filters.survey_type }}{% endif %}{% if filters.hospital %}&hospital={{ filters.hospital }}{% endif %}{% if filters.date_from %}&date_from={{ filters.date_from }}{% endif %}{% if filters.date_to %}&date_to={{ filters.date_to }}{% endif %}" 
                   class="px-3 py-2 text-slate hover:text-navy hover:bg-light rounded-lg transition">
                    <i data-lucide="chevrons-left" class="w-5 h-5"></i>
                </a>
                <a href="?page={{ page_obj.previous_page_number }}{% if filters.search %}&search={{ filters.search }}{% endif %}{% if filters.sentiment %}&sentiment={{ filters.sentiment }}{% endif %}{% if filters.survey_type %}&survey_type={{ filters.survey_type }}{% endif %}{% if filters.hospital %}&hospital={{ filters.hospital }}{% endif %}{% if filters.date_from %}&date_from={{ filters.date_from }}{% endif %}{% if filters.date_to %}&date_to={{ filters.date_to }}{% endif %}" 
                   class="px-3 py-2 text-slate hover:text-navy hover:bg-light rounded-lg transition">
                    <i data-lucide="chevron-left" class="w-5 h-5"></i>
                </a>
                {% endif %}

                <div class="px-4 py-2 bg-light text-navy rounded-lg font-medium">
                    <span class="text-slate">{% trans "Page" %}</span> {{ page_obj.number }} <span class="text-slate">{% trans "of" %}</span> {{ page_obj.paginator.num_pages }}
                </div>

                {% if page_obj.has_next %}
                <a href="?page={{ page_obj.next_page_number }}{% if filters.search %}&search={{ filters.search }}{% endif %}{% if filters.sentiment %}&sentiment={{ filters.sentiment }}{% endif %}{% if filters.survey_type %}&survey_type={{ filters.survey_type }}{% endif %}{% if filters.hospital %}&hospital={{ filters.hospital }}{% endif %}{% if filters.date_from %}&date_from={{ filters.date_from }}{% endif %}{% if filters.date_to %}&date_to={{ filters.date_to }}{% endif %}" 
                   class="px-3 py-2 text-slate hover:text-navy hover:bg-light rounded-lg transition">
                    <i data-lucide="chevron-right" class="w-5 h-5"></i>
                </a>
                <a href="?page={{ page_obj.paginator.num_pages }}{% if filters.search %}&search={{ filters.search }}{% endif %}{% if filters.sentiment %}&sentiment={{ filters.sentiment }}{% endif %}{% if filters.survey_type %}&survey_type={{ filters.survey_type }}{% endif %}{% if filters.hospital %}&hospital={{ filters.hospital }}{% endif %}{% if filters.date_from %}&date_from={{ filters.date_from }}{% endif %}{% if filters.date_to %}&date_to={{ filters.date_to }}{% endif %}" 
                   class="px-3 py-2 text-slate hover:text-navy hover:bg-light rounded-lg transition">
                    <i data-lucide="chevrons-right" class="w-5 h-5"></i>
                </a>
                {% endif %}
            </nav>
        </div>
    </div>
    {% endif %}

    {% else %}
    <!-- Empty State -->
    <div class="text-center py-16">
        <div class="inline-flex items-center justify-center w-20 h-20 bg-slate-100 rounded-full mb-4">
            <i data-lucide="message-square-quote" class="w-10 h-10 text-slate-400"></i>
        </div>
        <h3 class="text-xl font-bold text-navy mb-2">{% trans "No Comments Found" %}</h3>
        <p class="text-slate mb-4">{% trans "No survey comments match your current filters." %}</p>
        {% if filters.search or filters.sentiment or filters.survey_type or filters.hospital or filters.date_from or filters.date_to %}
        <a href="{% url 'surveys:survey_comments_list' %}" class="inline-flex items-center gap-2 text-navy hover:text-blue font-medium">
            <i data-lucide="x-circle" class="w-4 h-4"></i> {% trans "Clear Filters" %}
        </a>
        {% endif %}
    </div>
    {% endif %}
</div>

<!-- Chart Scripts -->
<script>
document.addEventListener('DOMContentLoaded', function() {
    lucide.createIcons();
    
    // Patient Type Distribution Chart (Donut Chart)
    const patientTypeDistributionData = {{ patient_type_distribution_json|safe }};
    
    if (patientTypeDistributionData && patientTypeDistributionData.length > 0) {
        const patientTypeLabels = patientTypeDistributionData.map(item => item.label);
        const patientTypeCounts = patientTypeDistributionData.map(item => item.count);
        const patientTypeColors = ['#3b82f6', '#f59e0b', '#ef4444', '#6b7280'];
        
        const patientTypeChartOptions = {
            series: patientTypeCounts,
            labels: patientTypeLabels,
            colors: patientTypeColors,
            chart: {
                type: 'donut',
                height: 300,
                fontFamily: 'Inter, sans-serif'
            },
            plotOptions: {
                pie: {
                    donut: {
                        size: '65%',
                        labels: {
                            show: true,
                            total: {
                                show: true,
                                showAlways: true,
                                label: 'Total',
                                formatter: function (w) {
                                    return w.globals.seriesTotals.reduce((a, b) => a + b, 0)
                                }
                            }
                        }
                    }
                }
            },
            dataLabels: {
                enabled: true,
                formatter: function(val, opts) {
                    return patientTypeDistributionData[opts.seriesIndex].percentage + '%'
                }
            },
            tooltip: {
                y: {
                    formatter: function(value, { series, seriesIndex, dataPointIndex, w }) {
                        return value + ' comments (' + patientTypeDistributionData[seriesIndex].percentage + '%)'
                    }
                }
            },
            legend: {
                position: 'bottom',
                fontSize: '13px',
                fontFamily: 'Inter, sans-serif'
            }
        };
        
        const patientTypeChart = new ApexCharts(document.querySelector("#patientTypeDistributionChart"), patientTypeChartOptions);
        patientTypeChart.render();
    }
    
    // Sentiment by Patient Type Chart (Stacked Bar Chart)
    const sentimentByPatientTypeData = {{ sentiment_by_patient_type_json|safe }};
    
    if (sentimentByPatientTypeData && sentimentByPatientTypeData.types && sentimentByPatientTypeData.types.length > 0) {
        const sentimentChartOptions = {
            series: [
                { name: 'Positive', data: sentimentByPatientTypeData.positive },
                { name: 'Negative', data: sentimentByPatientTypeData.negative },
                { name: 'Neutral', data: sentimentByPatientTypeData.neutral }
            ],
            chart: {
                type: 'bar',
                height: 300,
                stacked: true,
                fontFamily: 'Inter, sans-serif',
                toolbar: { show: false }
            },
            plotOptions: {
                bar: {
                    horizontal: false,
                    borderRadius: 4,
                    dataLabels: { total: { enabled: true, style: { fontSize: '13px', fontWeight: 900 } } }
                }
            },
            xaxis: {
                categories: sentimentByPatientTypeData.types,
                labels: { style: { fontSize: '12px', fontFamily: 'Inter, sans-serif' } }
            },
            yaxis: {
                title: { text: 'Number of Comments' },
                labels: { style: { fontSize: '12px', fontFamily: 'Inter, sans-serif' } }
            },
            legend: {
                position: 'bottom',
                fontSize: '13px',
                fontFamily: 'Inter, sans-serif'
            },
            fill: { opacity: 1 },
            colors: ['#10b981', '#ef4444', '#6b7280'],
            dataLabels: { enabled: false },
            tooltip: {
                y: {
                    formatter: function (value, { series, seriesIndex, dataPointIndex, w }) {
                        return value + ' comments'
                    }
                }
            }
        };
        
        const sentimentChart = new ApexCharts(document.querySelector("#sentimentByPatientTypeChart"), sentimentChartOptions);
        sentimentChart.render();
    }
});
</script>

{% endblock %}
