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

{% block title %}Call Center - 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 %}
<div class="container-fluid">
    <!-- Page Header -->
    <div class="page-header-gradient">
        <div class="flex items-center gap-3">
            <div class="section-icon bg-white/20">
                <i data-lucide="phone" class="w-6 h-6"></i>
            </div>
            <div>
                <h2 class="text-2xl font-bold mb-1">{% trans "Call Center" %}</h2>
                <p class="opacity-90 mb-0">{% trans "Monitor call center interactions and satisfaction" %}</p>
            </div>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-4">
            <div class="card border-left-primary">
                <div class="card-body">
                    <h6 class="text-muted mb-1">{% trans "Total Calls" %}</h6>
                    <h3 class="mb-0">{{ stats.total }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card border-left-success">
                <div class="card-body">
                    <h6 class="text-muted mb-1">{% trans "Avg Satisfaction" %}</h6>
                    <h3 class="mb-0">{{ stats.avg_satisfaction|floatformat:1 }}/5.0</h3>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card border-left-danger">
                <div class="card-body">
                    <h6 class="text-muted mb-1">{% trans "Low Ratings" %}</h6>
                    <h3 class="mb-0 text-danger">{{ stats.low_rating }}</h3>
                </div>
            </div>
        </div>
    </div>

    <!-- Interactions 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 "Interactions" %}</h3>
        </div>
        <div class="p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>{% trans "Caller" %}</th>
                            <th>{% trans "Subject" %}</th>
                            <th>{% trans "Type" %}</th>
                            <th>{% trans "Agent" %}</th>
                            <th>{% trans "Rating" %}</th>
                            <th>{% trans "Duration" %}</th>
                            <th>{% trans "Date" %}</th>
                            <th>{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for interaction in interactions %}
                        <tr onclick="window.location='{% url 'callcenter:interaction_detail' interaction.id %}'" style="cursor: pointer;">
                            <td>
                                {% if interaction.patient %}
                                <strong>{{ interaction.patient.get_full_name }}</strong><br>
                                <small class="text-muted">MRN: {{ interaction.patient.mrn }}</small>
                                {% else %}
                                <strong>{{ interaction.caller_name }}</strong><br>
                                <small class="text-muted">{{ interaction.caller_phone }}</small>
                                {% endif %}
                            </td>
                            <td>{{ interaction.subject|truncatewords:8 }}</td>
                            <td><span class="badge bg-info">{{ interaction.get_call_type_display }}</span></td>
                            <td>
                                {% if interaction.agent %}
                                <small>{{ interaction.agent.get_full_name }}</small>
                                {% else %}
                                <span class="text-muted">-</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if interaction.satisfaction_rating %}
                                <span class="badge {% if interaction.is_low_rating %}bg-danger{% else %}bg-success{% endif %}">
                                    {{ interaction.satisfaction_rating }}/5
                                </span>
                                {% else %}
                                <span class="text-muted">-</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if interaction.call_duration_seconds %}
                                <small>{{ interaction.call_duration_seconds|floatformat:0 }}s</small>
                                {% else %}
                                <span class="text-muted">-</span>
                                {% endif %}
                            </td>
                            <td><small>{{ interaction.call_started_at|date:"M d, Y H:i" }}</small></td>
                            <td onclick="event.stopPropagation();">
                                <a href="{% url 'callcenter:interaction_detail' interaction.id %}" 
                                   class="btn btn-sm btn-outline-primary">
                                    <i data-lucide="eye" class="w-4 h-4"></i>
                                </a>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="8" class="text-center py-5">
                                <i data-lucide="phone" class="w-12 h-12 mx-auto mb-3 text-gray-300"></i>
                                <p class="text-muted mt-3">{% trans "No interactions found" %}</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <nav aria-label="Interactions pagination" class="mt-4">
        <ul class="pagination justify-content-center">
            {% if page_obj.has_previous %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <i data-lucide="chevron-left" class="w-4 h-4"></i>
                </a>
            </li>
            {% endif %}
            
            {% for num in page_obj.paginator.page_range %}
                {% if page_obj.number == num %}
                <li class="page-item active"><span class="page-link">{{ num }}</span></li>
                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                <li class="page-item">
                    <a class="page-link" href="?page={{ num }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                        {{ num }}
                    </a>
                </li>
                {% endif %}
            {% endfor %}
            
            {% if page_obj.has_next %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.next_page_number }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <i data-lucide="chevron-right" class="w-4 h-4"></i>
                </a>
            </li>
            {% endif %}
        </ul>
    </nav>
    {% endif %}
</div>
{% endblock %}
