{% extends "base.html" %}
{% load crispy_forms_tags %}

{% block content %}
<div class="container py-4">
    <h2>{{ study.name }}</h2>
    <p class="text-muted">Participant ID: <code>{{ participant.pseudo_id }}</code></p>

    {% if study.description %}
    <div class="card card-body bg-dark border-secondary mb-4">
        {{ study.description|linebreaks }}
    </div>
    {% endif %}

    {% if assigned_packet and start_url and participant.consented %}
    <!-- Participant has been assigned and completed intake -->
    <div class="card card-body bg-dark border-secondary mb-3">
        <h5>Ready to Start</h5>
        <p class="mb-1">You have been assigned to: <strong>{{ participant.assigned_arm.name }}</strong></p>
        <p class="mb-1 text-muted">Collection: {{ assigned_packet.name }}</p>

        <!-- Passcode reminder block -->
        <div class="alert alert-info mt-3">
            <strong>Your Access Credentials</strong><br>
            Keep note of these to reaccess your results later:<br>
            CID: <code>{{ participant.cid_user.cid }}</code> &nbsp;|&nbsp;
            Passcode: <code>{{ participant.cid_user.passcode }}</code>
            {% if participant.email %}
            <br><small class="text-muted">Results can also be accessed via email: {{ participant.email }}</small>
            {% endif %}
        </div>

        <a class="btn btn-primary" href="{{ start_url }}">Start</a>
    </div>

    {% elif participant.assigned_arm %}
    <!-- Assigned but no start_url — CID not yet assigned -->
    <div class="card card-body bg-dark border-secondary mb-3">
        <h5>Assignment Complete</h5>
        <p class="text-muted">Refreshing to load your credentials...</p>
    </div>

    {% else %}
    <!-- Not yet assigned - show intake form -->
    <div class="card card-body bg-dark border-secondary mb-3">
        <h5>Participant Intake</h5>
        <p class="text-muted">
            Complete this form to be assigned to a study packet.
            {% if study.collect_demographics %}
            We collect anonymous demographic data to support analysis.
            {% endif %}
        </p>
        <form method="post">
            {% csrf_token %}
            {{ form|crispy }}
            <button class="btn btn-primary mt-2" type="submit">Submit and Get Assignment</button>
        </form>
    </div>
    {% endif %}
</div>
{% endblock %}
