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

{% block title %}{% trans "Department Response" %} - {{ observation.tracking_code }} - PX360{% endblock %}

{% block content %}
<div class="max-w-3xl mx-auto px-4 py-6">
    <div class="mb-6">
        <a href="{% url 'observations:observation_detail' pk=observation.pk %}" class="inline-flex items-center gap-2 px-4 py-2 border border-slate-200 rounded-xl text-slate-600 hover:bg-slate-50 transition text-sm font-semibold">
            <i data-lucide="arrow-left" class="w-4 h-4"></i> {% trans "Back to Observation" %}
        </a>
    </div>

    <div class="bg-navy rounded-2xl p-6 text-white mb-6">
        <h1 class="text-2xl font-bold mb-2">{% trans "Submit Department Response" %}</h1>
        <p class="text-white/80 text-sm">{{ observation.tracking_code }} - {% if observation.title %}{{ observation.title }}{% else %}{{ observation.description|truncatewords:10 }}{% endif %}</p>
    </div>

    <div class="bg-white rounded-2xl border border-slate-200 shadow-sm p-6 mb-6">
        <h3 class="font-bold text-navy mb-3">{% trans "Original Observation" %}</h3>
        <p class="text-slate-700 text-sm leading-relaxed">{{ observation.description|linebreaks }}</p>
        {% if observation.category %}
        <div class="mt-3">
            <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase bg-slate-100 text-slate-600">{{ observation.category.get_localized_name }}</span>
        </div>
        {% endif %}
    </div>

    {% if observation.department_response_en or observation.department_response_ar %}
    <div class="bg-green-50 border border-green-200 rounded-2xl p-6 mb-6">
        <h3 class="font-bold text-green-800 mb-3 flex items-center gap-2">
            <i data-lucide="check-circle" class="w-5 h-5"></i> {% trans "Existing Department Response" %}
        </h3>
        {% if observation.department_response_en %}
        <p class="text-sm text-slate-700 mb-2">{{ observation.department_response_en|linebreaks }}</p>
        {% endif %}
        {% if observation.department_response_ar %}
        <p class="text-sm text-slate-700" dir="rtl">{{ observation.department_response_ar|linebreaks }}</p>
        {% endif %}
        <p class="text-xs text-green-600 mt-2">{% trans "Submitted by" %} {{ observation.department_responded_by.get_full_name|default:"-" }} {% trans "on" %} {{ observation.department_responded_at|date:"Y-m-d H:i"|default:"-" }}</p>
    </div>
    {% endif %}

    {% if observation.short_description_en or observation.short_description_ar or observation.ai_brief_en or observation.ai_brief_ar %}
    <div class="bg-light/50 border border-slate-200 rounded-2xl p-6 mb-6">
        <div class="flex items-center justify-between mb-3">
            <h3 class="font-bold text-navy flex items-center gap-2">
                <i data-lucide="sparkles" class="w-4 h-4"></i> {% trans "AI Summary" %}
            </h3>
            <span class="px-2 py-0.5 bg-light rounded-md text-[10px] font-bold text-navy uppercase">
                {% trans "Editable" %}
            </span>
        </div>
        {% if observation.short_description_en or observation.ai_brief_en %}
        <div class="mb-3">
            <p class="text-xs font-bold text-slate uppercase tracking-wider mb-1">English</p>
            <div id="deptAiSummaryEn" class="text-sm text-slate-700 leading-relaxed bg-white border-2 border-slate-200 rounded-xl p-4 focus:outline-none focus:border-navy focus:ring-2 focus:ring-navy/20" contenteditable="true">{% if observation.short_description_en %}{{ observation.short_description_en }}{% elif observation.ai_brief_en %}{{ observation.ai_brief_en }}{% endif %}</div>
        </div>
        {% endif %}
        {% if observation.short_description_ar or observation.ai_brief_ar %}
        <div>
            <p class="text-xs font-bold text-slate uppercase tracking-wider mb-1">العربية</p>
            <div id="deptAiSummaryAr" class="text-sm text-slate-700 leading-relaxed bg-white border-2 border-slate-200 rounded-xl p-4 focus:outline-none focus:border-navy focus:ring-2 focus:ring-navy/20" contenteditable="true" dir="rtl">{% if observation.short_description_ar %}{{ observation.short_description_ar }}{% elif observation.ai_brief_ar %}{{ observation.ai_brief_ar }}{% endif %}</div>
        </div>
        {% endif %}
        <p class="text-xs text-slate mt-2">{% trans "This AI-generated summary provides context. You can use it as a reference when writing your response." %}</p>
    </div>
    {% endif %}

    <form method="post">
        {% csrf_token %}
        <div class="bg-white rounded-2xl border border-slate-200 shadow-sm p-6">
            <h3 class="font-bold text-navy mb-4 flex items-center gap-2">
                <i data-lucide="message-square" class="w-5 h-5"></i> {% trans "Your Response" %}
            </h3>

            <div class="mb-4">
                <label class="block text-sm font-semibold text-navy mb-2">{% trans "Response (English)" %}</label>
                <textarea name="response_en" rows="6"
                    class="w-full px-4 py-3 border-2 border-slate-200 rounded-xl focus:outline-none focus:border-navy focus:ring-2 focus:ring-navy/20 resize-none text-sm"
                    placeholder="{% trans 'Enter your response in English...' %}">{{ observation.department_response_en|default:"" }}</textarea>
            </div>

            <div class="mb-4">
                <label class="block text-sm font-semibold text-navy mb-2">{% trans "Response (Arabic)" %}</label>
                <textarea name="response_ar" rows="6" dir="rtl"
                    class="w-full px-4 py-3 border-2 border-slate-200 rounded-xl focus:outline-none focus:border-navy focus:ring-2 focus:ring-navy/20 resize-none text-sm"
                    placeholder="{% trans 'أدخل ردك باللغة العربية...' %}">{{ observation.department_response_ar|default:"" }}</textarea>
            </div>

            <p class="text-xs text-slate mb-4">
                <i data-lucide="info" class="w-3 h-3 inline mr-1"></i>
                {% trans "At least one language is required. This response will be recorded as the department's official response." %}
            </p>

            <div class="flex gap-3">
                <button type="submit" class="flex-1 px-6 py-3 bg-navy text-white rounded-xl font-semibold hover:bg-blue transition flex items-center justify-center gap-2">
                    <i data-lucide="send" class="w-4 h-4"></i> {% trans "Submit Response" %}
                </button>
                <a href="{% url 'observations:observation_detail' pk=observation.pk %}" class="px-6 py-3 border border-slate-200 rounded-xl font-semibold text-slate hover:bg-slate-50 transition">
                    {% trans "Cancel" %}
                </a>
            </div>
        </div>
    </form>
</div>
{% endblock %}
