{% extends 'base.html' %}
{% load auth_extras %}
{% load help_tags %}
{% block content %}
    <div class="py-4">
        <div class="container">
            <div class="row mb-4">
                <div class="col-12">
                    <div class="p-4 bg-dark rounded-3 border">
                        <div class="d-flex justify-content-between align-items-center">
                            <h1 class="mb-0">PENRA Courses</h1>
                            {% if not request.user.is_authenticated %}
                        {% comment %} <div>
                            <a class="btn btn-outline-primary me-2" href="{% url 'login' %}">Log in</a>
                            <a class="btn btn-outline-secondary" href="{% url 'password_reset' %}">Reset password</a>
                        </div> {% endcomment %}
                            {% else %}
                                <a class="btn btn-outline-light" href="{% url 'profile' %}">Your profile</a>
                            {% endif %}
                        </div>
                        <p class="text-muted mt-2">Exams, courses and resources for trainees and trainers.</p>
                    </div>
                </div>
            </div>

            <div class="row g-4">
                <div class="col-lg-8">
                    {% if request.user.is_authenticated %}
                        {% if request.user.userprofile.peninsula_trainee %}
                            <div class="card mb-3">
                                <div class="card-body">
                                    <h5 class="card-title">Profile</h5>
                                    <p class="mb-1"><strong>Name:</strong> {{ request.user.first_name }} {{ request.user.last_name
                                        }}</p>
                                    <p class="mb-1"><strong>Email:</strong> {{ request.user.email }}</p>
                                    <p class="mb-1"><strong>Grade:</strong> {{ request.user.userprofile.grade }}</p>
                                    <p class="mb-0"><strong>Supervisor:</strong> {{ request.user.userprofile.supervisor }}</p>
                                    <div role="alert" class="alert alert-warning mt-3">
                                        If any of this information is incorrect, please update it via your <a
                                            href="{% url 'profile' %}">profile page</a>
                                    </div>
                                </div>
                            </div>
                        {% endif %}

                        {% if request.user.supervisor %}
                            <div class="card mb-3 border border-info bg-dark bg-opacity-25">
                                <div class="card-body">
                                    <h5 class="card-title text-info"><i class="bi bi-person-workspace me-2"></i>Supervisor Dashboard</h5>
                                    <p class="card-text text-muted mb-3">Welcome, {{ request.user.supervisor.name }}. You can track, review, and leave feedback on your trainees' attempts here.</p>
                                    <a class="btn btn-outline-info" href="{% url 'generic:supervisor_overview' request.user.supervisor.pk %}">Go to Dashboard</a>
                                </div>
                            </div>
                        {% endif %}

                        <div class="card mb-3">
                            <div class="card-body">
                                <h5 class="card-title">Exams / Courses</h5>
                                <p class="card-text">View your courses and exam scores.</p>
                                <a class="btn btn-outline-primary" href="{% url 'user_scores' %}">View my courses</a>
                            </div>
                        </div>
                        {% if request.user.is_authenticated %}
                            <div class="card mb-3">
                                <div class="card-body">
                                    <h5 class="card-title">We need your help!</h5>
                                    <p class="card-text small text-muted">Help us grow the question banks and atlas.</p>
                                    <div class="row g-3">
                                        <div class="col-md-6">
                                            <div class="border rounded p-3 h-100 bg-dark-subtle">
                                                <div class="d-flex align-items-center justify-content-between mb-2">
                                                    <h6 class="mb-0">FRCR</h6>
                                                    <span class="badge text-bg-secondary">Exams</span>
                                                </div>
                                                <div class="d-grid gap-2">
                                                    <a class="btn btn-sm btn-outline-primary"
                                                       href="{% url 'anatomy:question_create' %}">Add Anatomy question</a>
                                                    <a class="btn btn-sm btn-outline-primary"
                                                       href="{% url 'sbas:question_create' %}">Add SBA question</a>
                                                    <a class="btn btn-sm btn-outline-secondary"
                                                       href="{% url 'sbas:question_review_start' %}">Review SBA question</a>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="col-md-6">
                                            <div class="border rounded p-3 h-100 bg-dark-subtle">
                                                <div class="d-flex align-items-center justify-content-between mb-2">
                                                    <h6 class="mb-0">Atlas</h6>
                                                    <span class="badge text-bg-secondary">Cases</span>
                                                </div>
                                                <div class="d-grid gap-2">
                                                    <a class="btn btn-sm btn-outline-primary"
                                                       href="{% url 'atlas:case_create' %}">Add case to Atlas</a>
                                                    <a class="btn btn-sm btn-outline-primary"
                                                       href="{% url 'atlas:exam_create' %}">Create collection</a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        {% endif %}
                    {% else %}
                        <div class="card mb-3">
                            <div class="card-body">
                                <h5 class="card-title">Peninsula Trainees</h5>
                                <p class="card-text">Please log in to access your exams and courses.</p>
                                <a class="btn btn-outline-primary me-2" href="{% url 'login' %}">Log in</a>
                                <a class="btn btn-outline-secondary" href="{% url 'password_reset' %}">Reset password</a>
                            </div>
                        </div>
                        <div class="alert alert-info" role="alert">
                            <h5 class="alert-heading mb-1">Not a Peninsula trainee?</h5>
                            <p class="mb-0">If you are an external user, please <a href="{% url 'cid_selector' %}">log in via
                                CID</a>.</p>
                            {% help "Help" %}
                                You should have received your candidate ID and login details by email or directly from your course
                                organiser. If you have not, please contact them for assistance.
                            {% endhelp %}
                        </div>
                    {% endif %}

                    {% if collections %}
                        <div class="card mb-3">
                            <div class="card-body">
                                <h5 class="card-title">Exam collections</h5>
                                <div class="list-group list-group-flush">
                                    {% for collection in collections %}
                                        <a class="list-group-item list-group-item-action"
                                           href="{% url 'generic:examcollection_detail' collection.pk %}">{{ collection.name }}</a>
                                    {% endfor %}
                                </div>
                            </div>
                        </div>
                    {% endif %}

                    {% if rcr_assessor %}
                        <div class="alert alert-info" role="alert">
                            <h5 class="alert-heading mb-1">RCR platform migration</h5>
                            <p class="mb-0"><a href="{% url 'rcr:radiology_index_view' %}">Open RCR migration tools</a></p>
                        </div>
                    {% endif %}
                </div>

                <div class="col-lg-4">
                    <div class="card mb-3">
                        <div class="card-body">
                            <h6 class="card-title">Quick links</h6>
                            <ul class="list-unstyled mb-0">
                                <li><a href="http://www.penracourses.org.uk/rts" class="link-primary">RTS platform</a></li>
                                {% if request.user.is_authenticated %}
                                    <li><a href="{% url 'user_marking' %}" class="link-primary">Marking overview</a></li>
                                {% endif %}
                                {% if not request.user.is_authenticated %}<li><a href="{% url 'cid_selector' %}"
                                                                                 class="link-primary">CID login</a></li>{% endif %}
                                {% if request.user.supervisor %}<li><a
                                    href='{% url "generic:supervisor_overview" request.user.supervisor.pk %}'
                                    class="link-primary">Supervisor overview</a></li>{% endif %}
                                {% if request.user|has_group:"cid_user_manager" %}
                                    <li>Manage <a href="{% url 'trainees' %}">trainees</a> · <a
                                        href="{% url 'accounts_list' %}">users</a> · <a
                                            href="{% url 'generic:manage_cids' %}">candidates</a></li>
                                    <li><a href="{% url 'media_cleanup' %}" class="link-primary">Unused media cleanup</a></li>
                                {% endif %}
                                {% if request.user.is_superuser %}
                                    <li><a href="{% url 'django_psutil_dash:dashboard' %}" class="link-primary">psutil</a></li>
                                {% endif %}
                            </ul>
                        </div>
                    </div>

                    <div class="card text-muted small">
                        <div class="card-body">
                            <strong>Need help?</strong>
                            <p class="mb-0">See <a href="{% url 'about' %}">About</a> or contact your admin.</p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row mt-4">
                <div class="col-12 text-center">
                    {% if request.user.is_authenticated %}
                        {% if request.user|has_group:"cid_user_manager" %}<a class="btn btn-sm btn-outline-secondary me-2"
                                                                             href="{% url 'admin:index' %}">Admin</a>{% endif %}
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
{% endblock %}