{% extends 'layouts/base.html' %}
{% load i18n %}
{% load static %}

{% block title %}{% trans "Call Records" %} - 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;
    }
</style>
{% endblock %}

{% block content %}
<!-- Page Header -->
<div class="page-header-gradient">
    <div class="flex items-center justify-between">
        <div class="flex items-center gap-3">
            <div class="section-icon bg-white/20">
                <i data-lucide="phone-call" class="w-6 h-6"></i>
            </div>
            <div>
                <h1 class="text-2xl font-bold mb-1">{% trans "Call Records" %}</h1>
                <p class="opacity-90 mb-0">{% trans "Manage and analyze imported call center recordings" %}</p>
            </div>
        </div>
        <div class="flex items-center gap-3">
            <a href="{% url 'callcenter:export_call_records_template' %}" 
               class="px-4 py-2 border border-white/30 text-white rounded-xl font-semibold hover:bg-white/20 transition flex items-center gap-2">
                <i data-lucide="download" class="w-4 h-4"></i>
                {% trans "Download Template" %}
            </a>
            <a href="{% url 'callcenter:import_call_records' %}" 
               class="px-4 py-2 bg-white text-blue-600 rounded-xl font-semibold hover:bg-blue-50 transition flex items-center gap-2">
                <i data-lucide="upload" class="w-4 h-4"></i>
                {% trans "Import CSV" %}
            </a>
        </div>
    </div>
</div>

<!-- Stats Cards -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-5 gap-6 mb-6">
    <!-- Total Calls -->
    <div class="bg-white rounded-2xl p-6 shadow-sm border border-slate-100">
        <div class="flex items-center justify-between mb-4">
            <div class="w-12 h-12 bg-blue-50 rounded-xl flex items-center justify-center">
                <i data-lucide="phone" class="w-6 h-6 text-blue"></i>
            </div>
            <span class="text-xs font-bold text-slate uppercase">{% trans "All Calls" %}</span>
        </div>
        <p class="text-3xl font-bold text-navy">{{ stats.total_calls }}</p>
        <p class="text-xs text-slate mt-1">{% trans "Total records" %}</p>
    </div>

    <!-- Inbound Calls -->
    <div class="bg-white rounded-2xl p-6 shadow-sm border border-slate-100">
        <div class="flex items-center justify-between mb-4">
            <div class="w-12 h-12 bg-green-50 rounded-xl flex items-center justify-center">
                <i data-lucide="phone-incoming" class="w-6 h-6 text-green-600"></i>
            </div>
            <span class="text-xs font-bold text-slate uppercase">{% trans "Inbound" %}</span>
        </div>
        <p class="text-3xl font-bold text-navy">{{ stats.inbound_calls }}</p>
        <p class="text-xs text-slate mt-1">{% trans "Incoming calls" %}</p>
    </div>

    <!-- Outbound Calls -->
    <div class="bg-white rounded-2xl p-6 shadow-sm border border-slate-100">
        <div class="flex items-center justify-between mb-4">
            <div class="w-12 h-12 bg-orange-50 rounded-xl flex items-center justify-center">
                <i data-lucide="phone-outgoing" class="w-6 h-6 text-orange-600"></i>
            </div>
            <span class="text-xs font-bold text-slate uppercase">{% trans "Outbound" %}</span>
        </div>
        <p class="text-3xl font-bold text-navy">{{ stats.outbound_calls }}</p>
        <p class="text-xs text-slate mt-1">{% trans "Outgoing calls" %}</p>
    </div>

    <!-- Total Duration -->
    <div class="bg-white rounded-2xl p-6 shadow-sm border border-slate-100">
        <div class="flex items-center justify-between mb-4">
            <div class="w-12 h-12 bg-purple-50 rounded-xl flex items-center justify-center">
                <i data-lucide="clock" class="w-6 h-6 text-purple-600"></i>
            </div>
            <span class="text-xs font-bold text-slate uppercase">{% trans "Duration" %}</span>
        </div>
        <p class="text-3xl font-bold text-navy">{{ stats.total_duration_formatted }}</p>
        <p class="text-xs text-slate mt-1">{% trans "Avg:" %} {{ stats.avg_duration_formatted }}</p>
    </div>

    <!-- Evaluated Calls -->
    <div class="bg-white rounded-2xl p-6 shadow-sm border border-slate-100">
        <div class="flex items-center justify-between mb-4">
            <div class="w-12 h-12 bg-indigo-50 rounded-xl flex items-center justify-center">
                <i data-lucide="circle-check" class="w-6 h-6 text-indigo-600"></i>
            </div>
            <span class="text-xs font-bold text-slate uppercase">{% trans "Evaluated" %}</span>
        </div>
        <p class="text-3xl font-bold text-navy">{{ stats.evaluated_calls }}</p>
        <p class="text-xs text-slate mt-1">{% trans "Not evaluated:" %} {{ stats.not_evaluated_calls }}</p>
    </div>
</div>

<!-- Filters -->
<div class="section-card mb-6">
    <div class="section-header">
        <div class="section-icon bg-blue-100">
            <i data-lucide="filter" class="w-5 h-5 text-blue-600"></i>
        </div>
        <h3 class="font-bold text-navy">{% trans "Filters" %}</h3>
    </div>
    <div class="p-5">
        <form method="get" class="flex flex-wrap gap-4">
            <div class="flex-1 min-w-48">
                <label class="block text-xs font-bold text-slate uppercase mb-1.5">{% trans "Search" %}</label>
                <input type="text" name="search" value="{{ filters.search }}" 
                       placeholder="{% trans 'Name, department, extension...' %}"
                       class="w-full px-4 py-2.5 border border-slate-200 rounded-xl focus:outline-none focus:ring-2 focus:ring-navy/20 text-sm">
            </div>

            <div class="w-48">
                <label class="block text-xs font-bold text-slate uppercase mb-1.5">{% trans "Call Type" %}</label>
                <select name="call_type" class="w-full px-4 py-2.5 border border-slate-200 rounded-xl focus:outline-none focus:ring-2 focus:ring-navy/20 text-sm bg-white">
                    <option value="">{% trans "All Calls" %}</option>
                    <option value="inbound" {% if filters.call_type == 'inbound' %}selected{% endif %}>{% trans "Inbound" %}</option>
                    <option value="outbound" {% if filters.call_type == 'outbound' %}selected{% endif %}>{% trans "Outbound" %}</option>
                </select>
            </div>

            <div class="w-48">
                <label class="block text-xs font-bold text-slate uppercase mb-1.5">{% trans "Evaluated" %}</label>
                <select name="evaluated" class="w-full px-4 py-2.5 border border-slate-200 rounded-xl focus:outline-none focus:ring-2 focus:ring-navy/20 text-sm bg-white">
                    <option value="">{% trans "All" %}</option>
                    <option value="true" {% if filters.evaluated == 'true' %}selected{% endif %}>{% trans "Yes" %}</option>
                    <option value="false" {% if filters.evaluated == 'false' %}selected{% endif %}>{% trans "No" %}</option>
                </select>
            </div>

            <div class="w-40">
                <label class="block text-xs font-bold text-slate uppercase mb-1.5">{% trans "From Date" %}</label>
                <input type="date" name="date_from" value="{{ filters.date_from }}"
                       class="w-full px-4 py-2.5 border border-slate-200 rounded-xl focus:outline-none focus:ring-2 focus:ring-navy/20 text-sm">
            </div>

            <div class="w-40">
                <label class="block text-xs font-bold text-slate uppercase mb-1.5">{% trans "To Date" %}</label>
                <input type="date" name="date_to" value="{{ filters.date_to }}"
                       class="w-full px-4 py-2.5 border border-slate-200 rounded-xl focus:outline-none focus:ring-2 focus:ring-navy/20 text-sm">
            </div>

            <div class="flex items-end">
                <button type="submit" class="px-6 py-2.5 bg-navy text-white rounded-xl font-semibold hover:bg-blue transition flex items-center gap-2">
                    <i data-lucide="search" class="w-4 h-4"></i>
                    {% trans "Filter" %}
                </button>
                <a href="{% url 'callcenter:call_records_list' %}" 
                   class="px-4 py-2.5 border border-slate-200 text-slate rounded-xl font-semibold hover:bg-light transition ml-2">
                    <i data-lucide="x" class="w-4 h-4"></i>
                </a>
            </div>
        </form>
    </div>
</div>

<!-- Call Records Table -->
<div class="section-card">
    <div class="section-header">
        <div class="section-icon bg-blue-100">
            <i data-lucide="list" class="w-5 h-5 text-blue-600"></i>
        </div>
        <h3 class="font-bold text-navy">{% trans "Call Records" %}</h3>
    </div>
    <div class="overflow-x-auto">
        <table class="w-full">
            <thead class="bg-slate-50 border-b border-slate-200">
                <tr>
                    <th class="text-left text-xs font-bold text-slate uppercase tracking-wider py-4 px-6">{% trans "Date/Time" %}</th>
                    <th class="text-left text-xs font-bold text-slate uppercase tracking-wider py-4 px-6">{% trans "Caller" %}</th>
                    <th class="text-left text-xs font-bold text-slate uppercase tracking-wider py-4 px-6">{% trans "Department" %}</th>
                    <th class="text-left text-xs font-bold text-slate uppercase tracking-wider py-4 px-6">{% trans "Type" %}</th>
                    <th class="text-left text-xs font-bold text-slate uppercase tracking-wider py-4 px-6">{% trans "Duration" %}</th>
                    <th class="text-left text-xs font-bold text-slate uppercase tracking-wider py-4 px-6">{% trans "Evaluated" %}</th>
                    <th class="text-left text-xs font-bold text-slate uppercase tracking-wider py-4 px-6">{% trans "File" %}</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-slate-100">
                {% for record in call_records %}
                <tr class="hover:bg-light/30 transition">
                    <td class="py-4 px-6">
                        <p class="text-sm font-semibold text-navy">{{ record.call_start|date:"M d, Y" }}</p>
                        <p class="text-xs text-slate">{{ record.call_start|date:"h:i A" }}</p>
                    </td>
                    <td class="py-4 px-6">
                        <p class="text-sm font-semibold text-navy">{{ record.caller_full_name|default:"-" }}</p>
                        {% if record.inbound_name %}
                        <p class="text-xs text-slate">{{ record.inbound_name }}</p>
                        {% endif %}
                    </td>
                    <td class="py-4 px-6">
                        <p class="text-sm font-semibold text-navy">{{ record.department|default:"-" }}</p>
                        {% if record.extension %}
                        <p class="text-xs text-slate">Ext: {{ record.extension }}</p>
                        {% endif %}
                    </td>
                    <td class="py-4 px-6">
                        {% if record.is_inbound %}
                        <span class="inline-flex items-center px-2.5 py-1 rounded-lg text-xs font-bold bg-green-100 text-green-800">
                            <i data-lucide="phone-incoming" class="w-3 h-3 mr-1"></i>
                            {% trans "Inbound" %}
                        </span>
                        {% elif record.is_outbound %}
                        <span class="inline-flex items-center px-2.5 py-1 rounded-lg text-xs font-bold bg-orange-100 text-orange-800">
                            <i data-lucide="phone-outgoing" class="w-3 h-3 mr-1"></i>
                            {% trans "Outbound" %}
                        </span>
                        {% else %}
                        <span class="text-xs text-slate">-</span>
                        {% endif %}
                    </td>
                    <td class="py-4 px-6">
                        <p class="text-sm font-mono text-navy">{{ record.call_length|default:"-" }}</p>
                    </td>
                    <td class="py-4 px-6">
                        {% if record.evaluated %}
                        <span class="inline-flex items-center px-2.5 py-1 rounded-lg text-xs font-bold bg-green-100 text-green-800">
                            <i data-lucide="check" class="w-3 h-3 mr-1"></i>
                            {% trans "Yes" %}
                        </span>
                        {% else %}
                        <span class="inline-flex items-center px-2.5 py-1 rounded-lg text-xs font-bold bg-slate-100 text-slate-600">
                            <i data-lucide="x" class="w-3 h-3 mr-1"></i>
                            {% trans "No" %}
                        </span>
                        {% endif %}
                    </td>
                    <td class="py-4 px-6">
                        <p class="text-xs text-slate truncate max-w-48" title="{{ record.file_name }}">{{ record.file_name|default:"-" }}</p>
                    </td>
                </tr>
                {% empty %}
                <tr>
                    <td colspan="7" class="py-12 text-center">
                        <div class="inline-flex items-center justify-center w-16 h-16 bg-slate-100 rounded-full mb-4">
                            <i data-lucide="phone-off" class="w-8 h-8 text-slate-400"></i>
                        </div>
                        <p class="text-slate font-medium">{% trans "No call records found" %}</p>
                        <p class="text-xs text-slate mt-1">{% trans "Import a CSV file to get started" %}</p>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <div class="border-t border-slate-200 px-6 py-4">
        <div class="flex items-center justify-between">
            <p class="text-sm text-slate">
                {% trans "Showing" %} {{ page_obj.start_index }}-{% if page_obj.has_next %}{{ page_obj.end_index }}{% else %}{{ page_obj.count }}{% endif %} {% trans "of" %} {{ page_obj.count }}
            </p>
            <div class="flex items-center gap-2">
                {% if page_obj.has_previous %}
                <a href="?page={{ page_obj.previous_page_number }}{% if filters %}{% for key, value in filters.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}{% endif %}"
                   class="px-3 py-2 border border-slate-200 rounded-lg text-sm font-semibold hover:bg-light transition">
                    {% trans "Previous" %}
                </a>
                {% endif %}

                <span class="px-4 py-2 bg-navy text-white rounded-lg text-sm font-semibold">
                    {{ page_obj.number }}
                </span>

                {% if page_obj.has_next %}
                <a href="?page={{ page_obj.next_page_number }}{% if filters %}{% for key, value in filters.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}{% endif %}"
                   class="px-3 py-2 border border-slate-200 rounded-lg text-sm font-semibold hover:bg-light transition">
                    {% trans "Next" %}
                </a>
                {% endif %}
            </div>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}
