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

{% block title %}{{ physician.get_full_name }} - {% trans "Physicians" %} - PX360{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{% url 'physicians:physician_list' %}">{% trans "Physicians" %}</a></li>
                    <li class="breadcrumb-item active">{{ physician.get_full_name }}</li>
                </ol>
            </nav>
            <h2 class="mb-1">
                <i class="bi bi-person-badge text-primary me-2"></i>
                {{ physician.get_full_name }}
            </h2>
            <p class="text-muted mb-0">{{ physician.specialization }}</p>
        </div>
        <div>
            {% if physician.status == 'active' %}
            <span class="badge bg-success fs-6">{% trans "Active" %}</span>
            {% else %}
            <span class="badge bg-secondary fs-6">{% trans "Inactive" %}</span>
            {% endif %}
        </div>
    </div>

    <div class="row">
        <!-- Left Column: Physician Info -->
        <div class="col-md-4">
            <!-- Basic Information -->
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">{% trans "Basic Information" %}</h5>
                </div>
                <div class="card-body">
                    <div class="mb-3">
                        <label class="text-muted small">{% trans "License Number" %}</label>
                        <p class="mb-0"><strong>{{ physician.license_number }}</strong></p>
                    </div>
                    <div class="mb-3">
                        <label class="text-muted small">{% trans "Specialization" %}</label>
                        <p class="mb-0">{{ physician.specialization }}</p>
                    </div>
                    <div class="mb-3">
                        <label class="text-muted small">{% trans "Hospital" %}</label>
                        <p class="mb-0">{{ physician.hospital.name }}</p>
                    </div>
                    {% if physician.department %}
                    <div class="mb-3">
                        <label class="text-muted small">{% trans "Department" %}</label>
                        <p class="mb-0">{{ physician.department.name }}</p>
                    </div>
                    {% endif %}
                    {% if physician.email %}
                    <div class="mb-3">
                        <label class="text-muted small">{% trans "Email" %}</label>
                        <p class="mb-0">{{ physician.email }}</p>
                    </div>
                    {% endif %}
                    {% if physician.phone %}
                    <div class="mb-3">
                        <label class="text-muted small">{% trans "Phone" %}</label>
                        <p class="mb-0">{{ physician.phone }}</p>
                    </div>
                    {% endif %}
                </div>
            </div>

            <!-- Current Month Performance -->
            {% if current_month_rating %}
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">{% trans "Current Month" %}</h5>
                </div>
                <div class="card-body text-center">
                    <h1 class="display-4 mb-2">{{ current_month_rating.average_rating|floatformat:2 }}</h1>
                    <p class="text-muted mb-3">{% trans "Average Rating" %}</p>
                    <div class="row">
                        <div class="col-6">
                            <h4>{{ current_month_rating.total_surveys }}</h4>
                            <small class="text-muted">{% trans "Surveys" %}</small>
                        </div>
                        <div class="col-6">
                            {% if current_month_rating.hospital_rank %}
                            <h4>#{{ current_month_rating.hospital_rank }}</h4>
                            <small class="text-muted">{% trans "Hospital Rank" %}</small>
                            {% else %}
                            <h4>-</h4>
                            <small class="text-muted">{% trans "No Rank" %}</small>
                            {% endif %}
                        </div>
                    </div>
                    
                    <!-- Trend -->
                    {% if trend != 'stable' %}
                    <div class="mt-3">
                        {% if trend == 'improving' %}
                        <span class="badge bg-success">
                            <i class="bi bi-arrow-up"></i> {% trans "Improving" %} {{ trend_percentage|floatformat:1 }}%
                        </span>
                        {% else %}
                        <span class="badge bg-danger">
                            <i class="bi bi-arrow-down"></i> {% trans "Declining" %} {{ trend_percentage|floatformat:1 }}%
                        </span>
                        {% endif %}
                    </div>
                    {% endif %}
                </div>
            </div>
            {% endif %}
        </div>

        <!-- Right Column: Performance Metrics -->
        <div class="col-md-8">
            <!-- Year-to-Date Performance -->
            <div class="row mb-4">
                <div class="col-md-6">
                    <div class="card border-left-primary">
                        <div class="card-body">
                            <h6 class="text-muted mb-1">{% trans "YTD Average Rating" %}</h6>
                            {% if ytd_average %}
                            <h3 class="mb-0">{{ ytd_average|floatformat:2 }}</h3>
                            {% else %}
                            <h3 class="mb-0 text-muted">-</h3>
                            {% endif %}
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="card border-left-info">
                        <div class="card-body">
                            <h6 class="text-muted mb-1">{% trans "YTD Total Surveys" %}</h6>
                            <h3 class="mb-0">{{ ytd_surveys|default:0 }}</h3>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Best & Worst Months -->
            {% if best_month or worst_month %}
            <div class="row mb-4">
                {% if best_month %}
                <div class="col-md-6">
                    <div class="card border-left-success">
                        <div class="card-body">
                            <h6 class="text-muted mb-1">{% trans "Best Month" %}</h6>
                            <h3 class="mb-0">{{ best_month.average_rating|floatformat:2 }}</h3>
                            <small class="text-muted">{{ best_month.year }}-{{ best_month.month|stringformat:"02d" }}</small>
                        </div>
                    </div>
                </div>
                {% endif %}
                {% if worst_month %}
                <div class="col-md-6">
                    <div class="card border-left-warning">
                        <div class="card-body">
                            <h6 class="text-muted mb-1">{% trans "Lowest Month" %}</h6>
                            <h3 class="mb-0">{{ worst_month.average_rating|floatformat:2 }}</h3>
                            <small class="text-muted">{{ worst_month.year }}-{{ worst_month.month|stringformat:"02d" }}</small>
                        </div>
                    </div>
                </div>
                {% endif %}
            </div>
            {% endif %}

            <!-- Ratings History -->
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">{% trans "Ratings History" %} ({% trans "Last 12 Months" %})</h5>
                </div>
                <div class="card-body">
                    {% if ratings_history %}
                    <div class="table-responsive">
                        <table class="table table-sm">
                            <thead>
                                <tr>
                                    <th>{% trans "Month" %}</th>
                                    <th>{% trans "Rating" %}</th>
                                    <th>{% trans "Surveys" %}</th>
                                    <th>{% trans "Positive" %}</th>
                                    <th>{% trans "Neutral" %}</th>
                                    <th>{% trans "Negative" %}</th>
                                    <th>{% trans "Hospital Rank" %}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for rating in ratings_history %}
                                <tr>
                                    <td>{{ rating.year }}-{{ rating.month|stringformat:"02d" }}</td>
                                    <td>
                                        <strong>{{ rating.average_rating|floatformat:2 }}</strong>
                                    </td>
                                    <td>{{ rating.total_surveys }}</td>
                                    <td>
                                        <span class="badge bg-success">{{ rating.positive_count }}</span>
                                    </td>
                                    <td>
                                        <span class="badge bg-warning">{{ rating.neutral_count }}</span>
                                    </td>
                                    <td>
                                        <span class="badge bg-danger">{{ rating.negative_count }}</span>
                                    </td>
                                    <td>
                                        {% if rating.hospital_rank %}
                                        #{{ rating.hospital_rank }}
                                        {% else %}
                                        -
                                        {% endif %}
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="text-center py-5">
                        <i class="bi bi-graph-up" style="font-size: 3rem; color: #ccc;"></i>
                        <p class="text-muted mt-3">{% trans "No rating history available" %}</p>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
