{% load i18n %}
<section class="bg-white rounded-2xl p-6 shadow-sm border border-slate-100">
    <div class="flex justify-between items-center mb-6">
        <h3 class="text-xl font-bold text-navy">{% trans "Involved Departments" %}</h3>
        {% if can_edit %}
        <a href="{% url 'complaints:involved_department_add' complaint_pk=complaint.pk %}" 
           class="px-4 py-2 bg-gradient-to-r from-navy to-blue text-white rounded-xl font-semibold hover:opacity-90 transition flex items-center gap-2">
            <i data-lucide="plus" class="w-4 h-4"></i> {% trans "Add Department" %}
        </a>
        {% endif %}
    </div>
    
    {% if can_edit and ai_department_suggested %}
    <div class="bg-blue-50 border border-blue-200 rounded-xl p-5 mb-4">
        <div class="flex items-center justify-between">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 bg-blue-100 rounded-lg flex items-center justify-center">
                    <i data-lucide="sparkles" class="w-5 h-5 text-blue-600"></i>
                </div>
                <div>
                    <div class="flex items-center gap-2">
                        <h4 class="font-bold text-navy">{{ complaint.department.name }}</h4>
                        <span class="px-2 py-0.5 bg-blue-100 text-blue-700 rounded-lg text-xs font-semibold">{% trans "AI Suggestion" %}</span>
                    </div>
                    <p class="text-sm text-slate">{% trans "AI suggested this department based on the complaint analysis." %}</p>
                </div>
            </div>
            <form method="post" action="{% url 'complaints:confirm_ai_department_suggestion' complaint_pk=complaint.pk %}" class="inline">
                {% csrf_token %}
                <button type="submit" class="px-4 py-2 bg-gradient-to-r from-navy to-blue text-white rounded-xl font-semibold hover:opacity-90 transition flex items-center gap-2">
                    <i data-lucide="check" class="w-4 h-4"></i> {% trans "Confirm" %}
                </button>
            </form>
        </div>
    </div>
    {% endif %}
    
    {% if complaint.involved_departments.exists %}
    <div class="space-y-4">
        {% for dept in complaint.involved_departments.all %}
        <div class="bg-white border border-slate-200 rounded-xl p-5 hover:shadow-md transition {% if dept.is_primary %}border-l-4 border-l-navy{% endif %}">
            <div class="flex justify-between items-start">
                <div class="flex-1">
                    <div class="flex items-center gap-3 mb-2">
                        <h4 class="font-bold text-navy">{{ dept.department.name }}</h4>
                        {% if dept.is_primary %}
                        <span class="px-2 py-1 bg-navy text-white rounded-lg text-xs font-bold">{% trans "PRIMARY" %}</span>
                        {% endif %}
                        <span class="px-2 py-1 bg-light text-navy rounded-lg text-xs font-semibold">{{ dept.get_role_display }}</span>
                    </div>
                    
                    {% if dept.assigned_to %}
                    <div class="flex items-center gap-2 text-sm text-slate mb-2">
                        <i data-lucide="user-check" class="w-4 h-4 text-blue"></i>
                        <span>{% trans "Assigned to:" %} <strong>{{ dept.assigned_to.get_full_name }}</strong></span>
                        {% if dept.assigned_at %}
                        <span class="text-slate">({{ dept.assigned_at|date:"M d, Y" }})</span>
                        {% endif %}
                    </div>
                    {% endif %}
                    
                    {% if dept.notes %}
                    <div class="bg-slate-50 rounded-lg p-3 mt-2">
                        <p class="text-sm text-slate">{{ dept.notes }}</p>
                    </div>
                    {% endif %}
                    
                    {% if dept.response_submitted %}
                    <div class="bg-green-50 border border-green-200 rounded-lg p-3 mt-3">
                        <div class="flex items-center gap-2 mb-1">
                            <i data-lucide="check-circle" class="w-4 h-4 text-green-500"></i>
                            <span class="text-sm font-semibold text-green-700">{% trans "Response Submitted" %}</span>
                            <span class="text-xs text-slate">{{ dept.response_submitted_at|date:"M d, Y H:i" }}</span>
                        </div>
                        {% if dept.response_notes %}
                        <p class="text-sm text-slate-700 mt-1">{{ dept.response_notes }}</p>
                        {% endif %}
                    </div>
                    {% endif %}
                </div>
                
                {% if can_edit %}
                <div class="flex items-center gap-2 ml-4">
                    {% if not dept.response_submitted %}
                    <form method="post" action="{% url 'complaints:involved_department_response' pk=dept.pk %}" class="inline">
                        {% csrf_token %}
                        <button type="submit" class="p-2 text-slate hover:text-blue transition" title="{% trans 'Submit Response' %}">
                            <i data-lucide="message-square" class="w-5 h-5"></i>
                        </button>
                    </form>
                    {% endif %}
                    <a href="{% url 'complaints:involved_department_edit' pk=dept.pk %}" 
                       class="p-2 text-slate hover:text-navy transition" title="{% trans 'Edit' %}">
                        <i data-lucide="edit-2" class="w-5 h-5"></i>
                    </a>
                    <form method="post" action="{% url 'complaints:involved_department_remove' pk=dept.pk %}" 
                          class="inline" onsubmit="return confirm('{% trans "Are you sure you want to remove this department?" %}')">
                        {% csrf_token %}
                        <button type="submit" class="p-2 text-slate hover:text-red-500 transition" title="{% trans 'Remove' %}">
                            <i data-lucide="trash-2" class="w-5 h-5"></i>
                        </button>
                    </form>
                </div>
                {% endif %}
            </div>
        </div>
        {% endfor %}
    </div>
    {% else %}
    <div class="text-center py-12">
        <i data-lucide="building-2" class="w-16 h-16 mx-auto text-slate-300 mb-4"></i>
        <p class="text-slate mb-4">{% trans "No departments involved yet" %}</p>
        {% if can_edit %}
        <a href="{% url 'complaints:involved_department_add' complaint_pk=complaint.pk %}" 
           class="px-4 py-2 bg-gradient-to-r from-navy to-blue text-white rounded-xl font-semibold hover:opacity-90 transition inline-flex items-center gap-2">
            <i data-lucide="plus" class="w-4 h-4"></i> {% trans "Add First Department" %}
        </a>
        {% endif %}
    </div>
    {% endif %}
</section>
