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

{% block title %}{% trans page_title %}{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <!-- Page Header -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <a href="{% url 'accounts:provisional-user-list' %}" class="text-muted text-decoration-none mb-2 d-inline-block">
                        <i class="bi bi-arrow-left me-1"></i>
                        {% trans "Back to Provisional Users" %}
                    </a>
                    <h1 class="h3 mb-2">
                        <i class="bi bi-graph-up me-2"></i>
                        {% trans "Onboarding Progress" %}
                    </h1>
                    <p class="text-muted mb-0">{{ user.email }}</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Progress Overview -->
    <div class="row mb-4">
        <div class="col-md-6">
            <div class="card mb-3">
                <div class="card-body">
                    <h6 class="text-muted mb-3">{% trans "Overall Progress" %}</h6>
                    <div class="mb-3">
                        <div class="d-flex justify-content-between mb-2">
                            <span class="text-muted">{% trans "Required Items" %}</span>
                            <span class="fw-bold">{{ acknowledged_count }} / {{ total_items }}</span>
                        </div>
                        <div class="progress" style="height: 20px;">
                            <div class="progress-bar bg-success" role="progressbar" 
                                 style="width: {{ progress_percentage }}%;" 
                                 aria-valuenow="{{ progress_percentage }}" 
                                 aria-valuemin="0" 
                                 aria-valuemax="100">
                                {{ progress_percentage }}%
                            </div>
                        </div>
                    </div>
                    {% if progress_percentage == 100 %}
                        <div class="alert alert-success mb-0">
                            <i class="bi bi-check-circle me-2"></i>
                            {% trans "All required acknowledgements completed!" %}
                        </div>
                    {% else %}
                        <div class="alert alert-info mb-0">
                            <i class="bi bi-info-circle me-2"></i>
                            <span class="text-muted">{{ remaining_count }} {% trans "items remaining" %}</span>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="card mb-3">
                <div class="card-body">
                    <h6 class="text-muted mb-3">{% trans "User Details" %}</h6>
                    <div class="mb-2">
                        <strong>{% trans "Name:" %}</strong> {{ user.get_full_name }}
                    </div>
                    <div class="mb-2">
                        <strong>{% trans "Email:" %}</strong> {{ user.email }}
                    </div>
                    <div class="mb-2">
                        <strong>{% trans "Hospital:" %}</strong> {{ user.hospital.name|default:"-" }}
                    </div>
                    <div class="mb-2">
                        <strong>{% trans "Department:" %}</strong> {{ user.department.name|default:"-" }}
                    </div>
                    <div class="mb-2">
                        <strong>{% trans "Roles:" %}</strong>
                        {% for group in user.groups.all %}
                            <span class="badge bg-secondary me-1">{{ group.name }}</span>
                        {% empty %}
                            <span class="text-muted">-</span>
                        {% endfor %}
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Checklist Items -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-white py-3">
                    <h5 class="mb-0">
                        <i class="bi bi-list-check me-2"></i>
                        {% trans "Acknowledgement Checklist" %}
                    </h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th width="50">{% trans "Status" %}</th>
                                    <th>{% trans "Item" %}</th>
                                    <th>{% trans "Role" %}</th>
                                    <th>{% trans "Required" %}</th>
                                    <th>{% trans "Acknowledged At" %}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for checklist_item in checklist_items %}
                                <tr>
                                    <td class="text-center">
                                        {% if checklist_item.acknowledged_at %}
                                            <i class="bi bi-check-circle text-success fa-lg"></i>
                                        {% else %}
                                            <i class="bi bi-circle text-muted fa-lg"></i>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <strong>{{ checklist_item.item.text_en }}</strong>
                                        {% if checklist_item.item.description_en %}
                                            <p class="small text-muted mb-0 mt-1">{{ checklist_item.item.description_en }}</p>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <span class="badge bg-info text-dark">
                                            {% if checklist_item.item.role %}{{ checklist_item.item.role }}{% else %}All Roles{% endif %}
                                        </span>
                                    </td>
                                    <td class="text-center">
                                        {% if checklist_item.item.is_required %}
                                            <span class="badge bg-danger">{% trans "Yes" %}</span>
                                        {% else %}
                                            <span class="badge bg-secondary">{% trans "No" %}</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if checklist_item.acknowledged_at %}
                                            <small>{{ checklist_item.acknowledged_at|date:"M d, Y H:i" }}</small>
                                        {% else %}
                                            <span class="text-muted">-</span>
                                        {% endif %}
                                    </td>
                                </tr>
                                {% empty %}
                                <tr>
                                    <td colspan="5" class="text-center py-4">
                                        <p class="text-muted mb-0">
                                            {% trans "No checklist items found for this user's role" %}
                                        </p>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Activity Log -->
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-white py-3">
                    <h5 class="mb-0">
                        <i class="bi bi-clock-history me-2"></i>
                        {% trans "Activity Log" %}
                    </h5>
                </div>
                <div class="card-body">
                    <div class="timeline">
                        {% for log in logs %}
                        <div class="timeline-item mb-3">
                            <div class="d-flex">
                                <div class="flex-shrink-0">
                                    <div class="timeline-marker bg-primary">
                                        <i class="bi bi-circle"></i>
                                    </div>
                                </div>
                                <div class="flex-grow-1 ms-3">
                                    <div class="card">
                                        <div class="card-body">
                                            <div class="d-flex justify-content-between align-items-start mb-2">
                                                <h6 class="mb-0">{{ log.get_event_type_display }}</h6>
                                                <small class="text-muted">{{ log.created_at|date:"M d, Y H:i" }}</small>
                                            </div>
                                            <p class="mb-0 text-muted">{{ log.description }}</p>
                                            {% if log.ip_address %}
                                                <small class="text-muted">
                                                    <i class="bi bi-geo-alt me-1"></i>
                                                    {{ log.ip_address }}
                                                </small>
                                            {% endif %}
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        {% empty %}
                        <div class="text-center py-4">
                            <i class="bi bi-clock-history fa-2x text-muted mb-2"></i>
                            <p class="text-muted mb-0">
                                {% trans "No activity recorded yet" %}
                            </p>
                        </div>
                        {% endfor %}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
.timeline {
    position: relative;
    padding-left: 20px;
}

.timeline-item {
    position: relative;
}

.timeline-marker {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 0.875rem;
}
</style>

{% comment %}
Template filter to get event type icon
{% endcomment %}

<script>
// Any interactive functionality can be added here
</script>
{% endblock %}
