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

{% block title %}{% trans "Review Doctor Ratings" %} - PX360{% endblock %}

{% block extra_css %}
<style>
    .rating-row {
        transition: all 0.2s ease;
        border-left: 3px solid transparent;
    }
    .rating-row:hover {
        background: #f8fafc;
        border-left-color: #007bbd;
        transform: translateX(2px);
    }
    .table-card {
        transition: box-shadow 0.2s ease;
    }
    .table-card:hover {
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    }
    .summary-card {
        transition: all 0.2s ease;
    }
    .summary-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
    }
</style>
{% endblock %}

{% block content %}
<div class="p-8">
  <!-- Header -->
  <header class="mb-8">
    <div class="flex items-center justify-between">
      <div>
        <h1 class="text-2xl font-bold text-navy flex items-center gap-3">
          <div class="w-10 h-10 bg-blue/10 rounded-xl flex items-center justify-center">
            <i data-lucide="eye" class="w-5 h-5 text-blue"></i>
          </div>
          {% trans "Review Import" %}
        </h1>
        <p class="text-slate mt-2 text-sm">{% trans "Review and confirm doctor ratings before importing" %}</p>
      </div>
      <a href="{% url 'physicians:doctor_rating_import' %}" 
         class="px-4 py-2.5 border border-slate-200 text-slate rounded-xl font-semibold hover:bg-slate-50 hover:border-slate-300 transition flex items-center gap-2">
        <i data-lucide="arrow-left" class="w-4 h-4"></i>
        {% trans "Back to Upload" %}
      </a>
    </div>
  </header>

  <!-- Summary Cards -->
  <section class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4 mb-8">
    <div class="bg-white rounded-2xl p-6 border border-slate-100 shadow-sm summary-card">
      <div class="flex items-center gap-4">
        <div class="w-12 h-12 bg-navy/10 rounded-xl flex items-center justify-center">
          <i data-lucide="database" class="w-6 h-6 text-navy"></i>
        </div>
        <div>
          <p class="text-xs font-bold uppercase tracking-wider text-slate mb-1">{% trans "Total Records" %}</p>
          <p class="text-2xl font-bold text-navy">{{ total_count }}</p>
        </div>
      </div>
    </div>
    
    <div class="bg-white rounded-2xl p-6 border border-green-100 shadow-sm summary-card border-l-4 border-l-green-500">
      <div class="flex items-center gap-4">
        <div class="w-12 h-12 bg-green/10 rounded-xl flex items-center justify-center">
          <i data-lucide="check-circle" class="w-6 h-6 text-green"></i>
        </div>
        <div>
          <p class="text-xs font-bold uppercase tracking-wider text-slate mb-1">{% trans "Matched Doctors" %}</p>
          <p class="text-2xl font-bold text-green">{{ matched_count }}</p>
          <p class="text-xs text-green-600 font-medium">{{ matched_count|default:0|add:0 }} / {{ total_count }}</p>
        </div>
      </div>
    </div>
    
    <div class="bg-white rounded-2xl p-6 border border-yellow-100 shadow-sm summary-card border-l-4 border-l-yellow-500">
      <div class="flex items-center gap-4">
        <div class="w-12 h-12 bg-yellow/10 rounded-xl flex items-center justify-center">
          <i data-lucide="help-circle" class="w-6 h-6 text-yellow-600"></i>
        </div>
        <div>
          <p class="text-xs font-bold uppercase tracking-wider text-slate mb-1">{% trans "Unmatched" %}</p>
          <p class="text-2xl font-bold text-yellow-600">{{ unmatched_count }}</p>
          <p class="text-xs text-yellow-700 font-medium">{% trans "Will need manual matching" %}</p>
        </div>
      </div>
    </div>
    
    <div class="bg-white rounded-2xl p-6 border border-slate-100 shadow-sm summary-card">
      <div class="flex items-center gap-4">
        <div class="w-12 h-12 bg-blue/10 rounded-xl flex items-center justify-center">
          <i data-lucide="building-2" class="w-6 h-6 text-blue"></i>
        </div>
        <div>
          <p class="text-xs font-bold uppercase tracking-wider text-slate mb-1">{% trans "Hospital" %}</p>
          <p class="text-lg font-bold text-navy truncate">{{ hospital.name }}</p>
        </div>
      </div>
    </div>
  </section>

  <!-- Errors Alert -->
  {% if errors %}
  <div class="bg-yellow-50 border border-yellow-200 rounded-2xl p-6 mb-6">
    <div class="flex items-start gap-3">
      <div class="w-8 h-8 bg-yellow-100 rounded-lg flex items-center justify-center flex-shrink-0">
        <i data-lucide="alert-triangle" class="w-4 h-4 text-yellow-600"></i>
      </div>
      <div class="flex-1">
        <h3 class="font-bold text-yellow-800 mb-2">
          {% trans "Parsing Errors" %} <span class="text-yellow-600">({{ errors|length }})</span>
        </h3>
        <ul class="text-sm text-yellow-700 space-y-1">
          {% for error in errors|slice:":5" %}
          <li class="flex items-start gap-2">
            <i data-lucide="x" class="w-4 h-4 text-yellow-600 flex-shrink-0 mt-0.5"></i>
            {{ error }}
          </li>
          {% endfor %}
          {% if errors|length > 5 %}
          <li class="text-yellow-600 italic">... and {{ errors|length|add:"-5" }} more errors</li>
          {% endif %}
        </ul>
      </div>
    </div>
  </div>
  {% endif %}

  <!-- Unmatched Doctors Warning -->
  {% if unmatched_count > 0 %}
  <div class="bg-blue-50 border border-blue-200 rounded-2xl p-6 mb-6">
    <div class="flex items-start gap-3">
      <div class="w-8 h-8 bg-blue-100 rounded-lg flex items-center justify-center flex-shrink-0">
        <i data-lucide="info" class="w-4 h-4 text-blue"></i>
      </div>
      <div class="flex-1">
        <h3 class="font-bold text-blue-900 mb-2">{% trans "Unmatched Doctors" %}</h3>
        <p class="text-sm text-blue-700 mb-3">{% trans "Some doctors could not be automatically matched to staff records. You can:" %}</p>
        <ul class="text-sm text-blue-700 space-y-1">
          <li class="flex items-start gap-2">
            <i data-lucide="check-circle" class="w-4 h-4 text-blue flex-shrink-0 mt-0.5"></i>
            <span>{% trans "Import anyway - ratings will be stored and can be matched later" %}</span>
          </li>
          <li class="flex items-start gap-2">
            <i data-lucide="check-circle" class="w-4 h-4 text-blue flex-shrink-0 mt-0.5"></i>
            <span>{% trans "Update staff records with correct employee_id to enable matching" %}</span>
          </li>
        </ul>
      </div>
    </div>
  </div>
  {% endif %}

  <!-- Ratings Table -->
  <section class="bg-white rounded-2xl shadow-sm border border-slate-100 table-card mb-6">
    <div class="px-6 py-4 border-b border-slate-200 flex items-center justify-between">
      <h2 class="font-bold text-navy">{% trans "Doctor Ratings Preview" %}</h2>
      <span class="inline-flex px-3 py-1 rounded-full text-xs font-bold uppercase tracking-wider bg-slate-100 text-slate-700">
        {% trans "Showing" %} {{ page_obj.start_index }} - {{ page_obj.end_index }} {% trans "of" %} {{ total_count }}
      </span>
    </div>
    <div class="overflow-x-auto">
      <table class="w-full">
        <thead>
          <tr class="bg-slate-50 border-b border-slate-200">
            <th class="p-4 text-left font-bold text-navy text-xs uppercase tracking-wider w-16">#</th>
            <th class="p-4 text-left font-bold text-navy text-xs uppercase tracking-wider">{% trans "Doctor" %}</th>
            <th class="p-4 text-left font-bold text-navy text-xs uppercase tracking-wider">{% trans "Patient" %}</th>
            <th class="p-4 text-left font-bold text-navy text-xs uppercase tracking-wider">{% trans "Rating" %}</th>
            <th class="p-4 text-left font-bold text-navy text-xs uppercase tracking-wider">{% trans "Date" %}</th>
            <th class="p-4 text-left font-bold text-navy text-xs uppercase tracking-wider">{% trans "Department" %}</th>
            <th class="p-4 text-center font-bold text-navy text-xs uppercase tracking-wider">{% trans "Match Status" %}</th>
          </tr>
        </thead>
        <tbody class="divide-y divide-slate-100">
          {% for rating in page_obj %}
          <tr class="rating-row">
            <td class="p-4 text-sm font-medium text-slate">{{ rating.row_num }}</td>
            <td class="p-4">
              <div class="flex items-center gap-3">
                <div class="w-8 h-8 rounded-lg bg-blue/10 flex items-center justify-center">
                  <i data-lucide="user" class="w-4 h-4 text-blue"></i>
                </div>
                <div>
                  <p class="font-bold text-navy text-sm">{{ rating.doctor_name|default:rating.doctor_name_raw }}</p>
                  {% if rating.doctor_id %}
                  <p class="text-xs text-slate">ID: {{ rating.doctor_id }}</p>
                  {% endif %}
                </div>
              </div>
            </td>
            <td class="p-4">
              <div class="text-sm text-slate">{{ rating.patient_name }}</div>
              <div class="text-xs text-slate">{{ rating.uhid }}</div>
            </td>
            <td class="p-4">
              {% if rating.rating >= 4 %}
              <div class="flex items-center gap-1 bg-green-100 text-green-700 px-3 py-1.5 rounded-lg w-fit">
                <i data-lucide="star" class="w-4 h-4 fill-green-600 text-green-600"></i>
                <span class="font-bold text-sm">{{ rating.rating }}</span>
              </div>
              {% elif rating.rating >= 3 %}
              <div class="flex items-center gap-1 bg-yellow-100 text-yellow-700 px-3 py-1.5 rounded-lg w-fit">
                <i data-lucide="star" class="w-4 h-4 fill-yellow-500 text-yellow-500"></i>
                <span class="font-bold text-sm">{{ rating.rating }}</span>
              </div>
              {% else %}
              <div class="flex items-center gap-1 bg-red-100 text-red-700 px-3 py-1.5 rounded-lg w-fit">
                <i data-lucide="star" class="w-4 h-4 fill-red-500 text-red-500"></i>
                <span class="font-bold text-sm">{{ rating.rating }}</span>
              </div>
              {% endif %}
            </td>
            <td class="p-4">
              {% if rating.rating_date %}
              <div class="text-sm text-slate">{{ rating.rating_date }}</div>
              {% else %}
              <div class="text-sm text-slate">-</div>
              {% endif %}
            </td>
            <td class="p-4">
              <div class="text-sm text-slate">{{ rating.department|default:"-" }}</div>
            </td>
            <td class="p-4 text-center">
              {% if rating.staff_matched %}
              <span class="inline-flex items-center gap-1 px-3 py-1 rounded-full text-xs font-bold uppercase tracking-wider bg-green-100 text-green-700">
                <i data-lucide="check-circle" class="w-3 h-3"></i>
                {% trans "Matched" %}
              </span>
              {% else %}
              <span class="inline-flex items-center gap-1 px-3 py-1 rounded-full text-xs font-bold uppercase tracking-wider bg-yellow-100 text-yellow-700">
                <i data-lucide="help-circle" class="w-3 h-3"></i>
                {% trans "Unmatched" %}
              </span>
              {% endif %}
            </td>
          </tr>
          {% empty %}
          <tr>
            <td colspan="7" class="p-12 text-center">
              <div class="flex flex-col items-center justify-center">
                <div class="w-20 h-20 bg-slate-100 rounded-full flex items-center justify-center mb-4">
                  <i data-lucide="inbox" class="w-10 h-10 text-slate-300"></i>
                </div>
                <p class="text-slate font-medium">{% trans "No ratings to display" %}</p>
              </div>
            </td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
  </section>

  <!-- Pagination -->
  {% if page_obj.has_other_pages %}
  <div class="bg-slate-50 px-8 py-4 flex items-center justify-between border border-slate-200 rounded-2xl mb-6">
    <div class="flex items-center gap-4">
      <span class="text-xs text-slate font-medium">
        {% trans "Showing" %} <span class="font-bold text-navy">{{ page_obj.start_index }}-{{ page_obj.end_index }}</span> {% trans "of" %} <span class="font-bold text-navy">{{ total_count }}</span> {% trans "entries" %}
      </span>
    </div>
    <div class="flex gap-2">
      {% if page_obj.has_previous %}
      <a href="?page={{ page_obj.previous_page_number }}" 
         class="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 transition">
        <i data-lucide="chevron-left" class="w-4 h-4 text-slate"></i>
      </a>
      {% else %}
      <span class="w-8 h-8 flex items-center justify-center rounded-lg border bg-slate-100 text-slate-300 cursor-not-allowed">
        <i data-lucide="chevron-left" class="w-4 h-4"></i>
      </span>
      {% endif %}
      
      {% for num in page_obj.paginator.page_range %}
        {% if num == page_obj.number %}
        <span class="w-8 h-8 flex items-center justify-center rounded-lg bg-navy text-white text-xs font-bold">{{ num }}</span>
        {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
        <a href="?page={{ num }}" 
           class="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 text-xs font-bold text-slate transition">
          {{ num }}
        </a>
        {% elif num == 1 or num == page_obj.paginator.num_pages %}
        <a href="?page={{ num }}" 
           class="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 text-xs font-bold text-slate transition">
          {{ num }}
        </a>
        {% elif num == page_obj.number|add:'-3' or num == page_obj.number|add:'3' %}
        <span class="w-8 h-8 flex items-center justify-center text-xs text-slate">...</span>
        {% endif %}
      {% endfor %}
      
      {% if page_obj.has_next %}
      <a href="?page={{ page_obj.next_page_number }}" 
         class="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 transition">
        <i data-lucide="chevron-right" class="w-4 h-4 text-slate"></i>
      </a>
      {% else %}
      <span class="w-8 h-8 flex items-center justify-center rounded-lg border bg-slate-100 text-slate-300 cursor-not-allowed">
        <i data-lucide="chevron-right" class="w-4 h-4"></i>
      </span>
      {% endif %}
    </div>
  </div>
  {% endif %}

  <!-- Action Buttons -->
  <form method="post">
    {% csrf_token %}
    <div class="bg-white rounded-2xl p-6 border border-slate-100 shadow-sm">
      <div class="flex items-center justify-between">
        <div>
          <h3 class="font-bold text-navy mb-1">{% trans "Ready to Import?" %}</h3>
          <p class="text-slate text-sm">{% trans "This will queue a background job to import all ratings. You'll be able to track progress." %}</p>
        </div>
        <div class="flex gap-3">
          <button type="submit" name="action" value="cancel" 
                  class="px-6 py-2.5 border border-slate-200 text-slate rounded-xl font-semibold hover:bg-slate-50 hover:border-slate-300 transition flex items-center gap-2">
            <i data-lucide="x-circle" class="w-4 h-4"></i>
            {% trans "Cancel" %}
          </button>
          <button type="submit" name="action" value="import" 
                  class="px-6 py-2.5 bg-navy text-white rounded-xl font-semibold shadow-md hover:bg-blue transition flex items-center gap-2">
            <i data-lucide="check-circle" class="w-4 h-4"></i>
            {% trans "Import" %} {{ total_count }} {% trans "Ratings" %}
          </button>
        </div>
      </div>
    </div>
  </form>
</div>
{% endblock %}

{% block extra_js %}
<script>
  lucide.createIcons();
</script>
{% endblock %}