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

{% block title %}{% trans "My Badges" %} - {% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <!-- Breadcrumb -->
    <nav aria-label="breadcrumb" class="mb-4">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{% url 'appreciation:appreciation_list' %}">{% trans "Appreciation" %}</a></li>
            <li class="breadcrumb-item active" aria-current="page">{% trans "My Badges" %}</li>
        </ol>
    </nav>

    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>
            <i class="bi bi-award text-warning me-2"></i>
            {% trans "My Badges" %}
        </h2>
        <div>
            <a href="{% url 'appreciation:appreciation_list' %}" class="btn btn-primary">
                <i class="bi bi-send me-2"></i>
                {% trans "Appreciations" %}
            </a>
        </div>
    </div>

    <!-- Stats Summary -->
    <div class="row mb-4">
        <div class="col-md-4">
            <div class="card shadow-sm border-primary">
                <div class="card-body text-center">
                    <i class="bi bi-heart fa-2x text-danger mb-2"></i>
                    <h3 class="mb-0">{{ total_received }}</h3>
                    <p class="text-muted mb-0">{% trans "Total Appreciations Received" %}</p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card shadow-sm border-warning">
                <div class="card-body text-center">
                    <i class="bi bi-award fa-2x text-warning mb-2"></i>
                    <h3 class="mb-0">{{ badges|length }}</h3>
                    <p class="text-muted mb-0">{% trans "Badges Earned" %}</p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card shadow-sm border-info">
                <div class="card-body text-center">
                    <i class="bi bi-trophy fa-2x text-info mb-2"></i>
                    <h3 class="mb-0">{{ badge_progress|length }}</h3>
                    <p class="text-muted mb-0">{% trans "Available Badges" %}</p>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <!-- Earned Badges -->
        <div class="col-lg-8">
            <div class="card shadow-sm mb-4">
                <div class="card-header bg-warning text-white">
                    <h5 class="card-title mb-0">
                        <i class="bi bi-award me-2"></i>
                        {% trans "Earned Badges" %}
                    </h5>
                </div>
                <div class="card-body">
                    {% if badges %}
                        <div class="row">
                            {% for user_badge in badges %}
                                <div class="col-md-6 col-lg-4 mb-3">
                                    <div class="card h-100 border-2 border-warning">
                                        <div class="card-body text-center">
                                            <div class="badge-icon-wrapper mb-3">
                                                <i class="{{ user_badge.badge.icon }} fa-4x text-warning"></i>
                                            </div>
                                            <h5 class="card-title">{{ user_badge.badge.get_localized_name }}</h5>
                                            <p class="card-text small text-muted">
                                                {{ user_badge.badge.description_en|truncatewords:10 }}
                                            </p>
                                            <small class="text-muted">
                                                <i class="bi bi-calendar me-1"></i>
                                                {% trans "Earned on" %} {{ user_badge.earned_at|date:"F j, Y" }}
                                            </small>
                                        </div>
                                    </div>
                                </div>
                            {% endfor %}
                        </div>

                        <!-- Pagination -->
                        {% if page_obj.has_other_pages %}
                            <nav aria-label="Page navigation" 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 }}">
                                                {% trans "Previous" %}
                                            </a>
                                        </li>
                                    {% else %}
                                        <li class="page-item disabled">
                                            <span class="page-link">{% trans "Previous" %}</span>
                                        </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 }}">{{ 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 }}">
                                                {% trans "Next" %}
                                            </a>
                                        </li>
                                    {% else %}
                                        <li class="page-item disabled">
                                            <span class="page-link">{% trans "Next" %}</span>
                                        </li>
                                    {% endif %}
                                </ul>
                            </nav>
                        {% endif %}
                    {% else %}
                        <div class="text-center py-5">
                            <i class="bi bi-award fa-4x text-muted mb-3"></i>
                            <h4 class="text-muted">{% trans "No badges earned yet" %}</h4>
                            <p class="text-muted">
                                {% trans "Start receiving appreciations to earn badges!" %}
                            </p>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- Badge Progress -->
        <div class="col-lg-4">
            <div class="card shadow-sm">
                <div class="card-header bg-light">
                    <h6 class="card-title mb-0">
                        <i class="bi bi-graph-up me-2"></i>
                        {% trans "Badge Progress" %}
                    </h6>
                </div>
                <div class="card-body">
                    {% for item in badge_progress %}
                        <div class="mb-3 {% if not item.earned %}opacity-75{% endif %}">
                            <div class="d-flex align-items-center mb-1">
                                <i class="{{ item.badge.icon }} me-2 {% if item.earned %}text-warning{% else %}text-muted{% endif %}"></i>
                                <div>
                                    <strong class="{% if item.earned %}text-warning{% else %}text-muted{% endif %}">
                                        {{ item.badge.get_localized_name }}
                                    </strong>
                                    {% if item.earned %}
                                        <span class="badge bg-success ms-2">✓</span>
                                    {% endif %}
                                </div>
                            </div>
                            <div class="progress" style="height: 10px;">
                                <div 
                                    class="progress-bar {% if item.earned %}bg-warning{% else %}bg-info{% endif %}" 
                                    role="progressbar" 
                                    style="width: {{ item.progress }}%"
                                    aria-valuenow="{{ item.progress }}" 
                                    aria-valuemin="0" 
                                    aria-valuemax="100"
                                ></div>
                            </div>
                            <small class="text-muted">
                                {% if item.badge.criteria_type == 'count' %}
                                    {% trans "Requires" %} {{ item.badge.criteria_value }} {% trans "appreciations" %}
                                {% endif %}
                                ({% trans "Progress" %}: {{ item.progress }}%)
                            </small>
                        </div>
                    {% empty %}
                        <div class="text-center py-3">
                            <i class="bi bi-trophy fa-2x text-muted mb-2"></i>
                            <p class="text-muted mb-0">{% trans "No badges available" %}</p>
                        </div>
                    {% endfor %}
                </div>
            </div>

            <!-- Tips -->
            <div class="card shadow-sm mt-3">
                <div class="card-header bg-light">
                    <h6 class="card-title mb-0">
                        <i class="bi bi-lightbulb me-2"></i>
                        {% trans "How to Earn Badges" %}
                    </h6>
                </div>
                <div class="card-body">
                    <ul class="list-unstyled mb-0 small">
                        <li class="mb-2">
                            <i class="bi bi-check text-success me-2"></i>
                            {% trans "Receive appreciations from colleagues" %}
                        </li>
                        <li class="mb-2">
                            <i class="bi bi-check text-success me-2"></i>
                            {% trans "Consistently provide excellent service" %}
                        </li>
                        <li class="mb-2">
                            <i class="bi-check text-success me-2"></i>
                            {% trans "Be a team player" %}
                        </li>
                        <li class="mb-2">
                            <i class="bi-check text-success me-2"></i>
                            {% trans "Show leadership qualities" %}
                        </li>
                        <li>
                            <i class="bi-check text-success me-2"></i>
                            {% trans "Demonstrate innovation" %}
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_css %}
{{ block.super }}
<style>
.badge-icon-wrapper {
    padding: 20px;
    border-radius: 50%;
    background-color: #fff9c4;
    width: 120px;
    height: 120px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
}

.opacity-75 {
    opacity: 0.75;
}
</style>
{% endblock %}
