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

{% block title %}{% trans "Physician Leaderboard" %} - PX360{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-1">
                <i class="bi bi-trophy text-warning me-2"></i>
                {% trans "Physician Leaderboard" %}
            </h2>
            <p class="text-muted mb-0">{% trans "Top-rated physicians for" %} {{ year }}-{{ month|stringformat:"02d" }}</p>
        </div>
        <div>
            <a href="{% url 'physicians:physician_list' %}" class="btn btn-outline-primary">
                <i class="bi bi-arrow-left me-2"></i>{% trans "Back to Physicians" %}
            </a>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card border-left-primary">
                <div class="card-body">
                    <h6 class="text-muted mb-1">{% trans "Total Physicians" %}</h6>
                    <h3 class="mb-0">{{ stats.total_physicians|default:0 }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-success">
                <div class="card-body">
                    <h6 class="text-muted mb-1">{% trans "Average Rating" %}</h6>
                    <h3 class="mb-0">{{ stats.average_rating|floatformat:2|default:"-" }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-info">
                <div class="card-body">
                    <h6 class="text-muted mb-1">{% trans "Total Surveys" %}</h6>
                    <h3 class="mb-0">{{ stats.total_surveys|default:0 }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-warning">
                <div class="card-body">
                    <h6 class="text-muted mb-1">{% trans "Excellent (4.5+)" %}</h6>
                    <h3 class="mb-0">{{ distribution.excellent|default:0 }}</h3>
                </div>
            </div>
        </div>
    </div>

    <!-- Filters -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-2">
                    <label class="form-label">{% trans "Year" %}</label>
                    <input type="number" name="year" class="form-control" 
                           value="{{ year }}" min="2020" max="2030">
                </div>
                <div class="col-md-2">
                    <label class="form-label">{% trans "Month" %}</label>
                    <select name="month" class="form-select">
                        {% for m in "123456789012"|make_list %}
                        <option value="{{ forloop.counter }}" {% if month == forloop.counter %}selected{% endif %}>
                            {{ forloop.counter|stringformat:"02d" }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">{% trans "Hospital" %}</label>
                    <select name="hospital" class="form-select">
                        <option value="">{% trans "All Hospitals" %}</option>
                        {% for hospital in hospitals %}
                        <option value="{{ hospital.id }}" {% if filters.hospital == hospital.id|stringformat:"s" %}selected{% endif %}>
                            {{ hospital.name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">{% trans "Department" %}</label>
                    <select name="department" class="form-select">
                        <option value="">{% trans "All Departments" %}</option>
                        {% for department in departments %}
                        <option value="{{ department.id }}" {% if filters.department == department.id|stringformat:"s" %}selected{% endif %}>
                            {{ department.name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">{% trans "Limit" %}</label>
                    <select name="limit" class="form-select">
                        <option value="10" {% if filters.limit == "10" %}selected{% endif %}>10</option>
                        <option value="20" {% if filters.limit == "20" or not filters.limit %}selected{% endif %}>20</option>
                        <option value="50" {% if filters.limit == "50" %}selected{% endif %}>50</option>
                        <option value="100" {% if filters.limit == "100" %}selected{% endif %}>100</option>
                    </select>
                </div>
                <div class="col-12">
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-search me-2"></i>{% trans "Filter" %}
                    </button>
                    <a href="{% url 'physicians:leaderboard' %}" class="btn btn-outline-secondary">
                        <i class="bi bi-x-circle me-2"></i>{% trans "Clear" %}
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Leaderboard -->
    <div class="card">
        <div class="card-header">
            <h5 class="mb-0">{% trans "Top Performers" %}</h5>
        </div>
        <div class="card-body p-0">
            {% if leaderboard %}
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th style="width: 80px;">{% trans "Rank" %}</th>
                            <th>{% trans "Physician" %}</th>
                            <th>{% trans "Specialization" %}</th>
                            <th>{% trans "Department" %}</th>
                            <th>{% trans "Rating" %}</th>
                            <th>{% trans "Surveys" %}</th>
                            <th>{% trans "Sentiment" %}</th>
                            <th>{% trans "Trend" %}</th>
                            <th>{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for entry in leaderboard %}
                        <tr onclick="window.location='{% url 'physicians:physician_detail' entry.physician.id %}'" style="cursor: pointer;">
                            <td>
                                {% if entry.rank <= 3 %}
                                <h3 class="mb-0">
                                    {% if entry.rank == 1 %}
                                    <i class="bi bi-trophy-fill text-warning"></i>
                                    {% elif entry.rank == 2 %}
                                    <i class="bi bi-trophy-fill text-secondary"></i>
                                    {% elif entry.rank == 3 %}
                                    <i class="bi bi-trophy-fill" style="color: #cd7f32;"></i>
                                    {% endif %}
                                </h3>
                                {% else %}
                                <strong class="text-muted">#{{ entry.rank }}</strong>
                                {% endif %}
                            </td>
                            <td>
                                <strong>{{ entry.physician.get_full_name }}</strong><br>
                                <small class="text-muted">{{ entry.physician.license_number }}</small>
                            </td>
                            <td>{{ entry.physician.specialization }}</td>
                            <td>
                                {% if entry.physician.department %}
                                {{ entry.physician.department.name }}
                                {% else %}
                                <span class="text-muted">-</span>
                                {% endif %}
                            </td>
                            <td>
                                <h4 class="mb-0 text-success">{{ entry.rating.average_rating|floatformat:2 }}</h4>
                            </td>
                            <td>
                                <span class="badge bg-light text-dark">{{ entry.rating.total_surveys }}</span>
                            </td>
                            <td>
                                <div class="d-flex gap-1">
                                    <span class="badge bg-success" title="{% trans 'Positive' %}">
                                        {{ entry.rating.positive_count }}
                                    </span>
                                    <span class="badge bg-warning" title="{% trans 'Neutral' %}">
                                        {{ entry.rating.neutral_count }}
                                    </span>
                                    <span class="badge bg-danger" title="{% trans 'Negative' %}">
                                        {{ entry.rating.negative_count }}
                                    </span>
                                </div>
                            </td>
                            <td>
                                {% if entry.trend == 'up' %}
                                <span class="text-success ">
                                    <i class="bi bi-arrow-up text-success"></i> {% trans "Up" %}
{#                                </span>#}
                                {% elif entry.trend == 'down' %}
                                <span class="text-danger">
                                    <i class="bi bi-arrow-down text-danger"></i> {% trans "Down" %}
                                </span>
                                {% else %}
                                <span class="text-primary">
                                    <i class="bi bi-dash text-secondary"></i> {% trans "Stable" %}
                                </span>
                                {% endif %}
                            </td>
                            <td onclick="event.stopPropagation();">
                                <a href="{% url 'physicians:physician_detail' entry.physician.id %}" 
                                   class="btn btn-sm btn-outline-primary">
                                    <i class="bi bi-eye"></i>
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% else %}
            <div class="text-center py-5">
                <i class="bi bi-trophy" style="font-size: 3rem; color: #ccc;"></i>
                <p class="text-muted mt-3">{% trans "No ratings available for this period" %}</p>
            </div>
            {% endif %}
        </div>
    </div>

    <!-- Performance Distribution -->
    <div class="card mt-4">
        <div class="card-header">
            <h5 class="mb-0">{% trans "Performance Distribution" %}</h5>
        </div>
        <div class="card-body">
            <div class="row text-center">
                <div class="col-md-3">
                    <div class="p-3 border rounded">
                        <h2 class="text-success mb-2">{{ distribution.excellent|default:0 }}</h2>
                        <p class="text-muted mb-0">{% trans "Excellent" %}<br><small>(4.5+)</small></p>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="p-3 border rounded">
                        <h2 class="text-primary mb-2">{{ distribution.good|default:0 }}</h2>
                        <p class="text-muted mb-0">{% trans "Good" %}<br><small>(3.5-4.5)</small></p>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="p-3 border rounded">
                        <h2 class="text-warning mb-2">{{ distribution.average|default:0 }}</h2>
                        <p class="text-muted mb-0">{% trans "Average" %}<br><small>(2.5-3.5)</small></p>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="p-3 border rounded">
                        <h2 class="text-danger mb-2">{{ distribution.poor|default:0 }}</h2>
                        <p class="text-muted mb-0">{% trans "Poor" %}<br><small>(&lt;2.5)</small></p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
