{% extends 'school/base.html.twig' %}

{% set status_labels = {
    SOUMISE: 'Soumise',
    A_TESTER: 'À tester',
    A_AFFECTER: 'À affecter',
    VALIDEE: 'Validée',
    REJETEE: 'Rejetée',
    ANNULEE: 'Annulée'
} %}
{% set status_tone = inscription.statut == 'VALIDEE' ? 'success' : (inscription.statut == 'REJETEE' or inscription.statut == 'ANNULEE' ? 'danger' : (inscription.statut == 'A_TESTER' ? 'warning' : 'info')) %}
{% set type_label = inscription.typeCandidat == 'ANCIEN' ? 'Ancien' : 'Nouveau' %}
{% set type_tone = inscription.typeCandidat == 'ANCIEN' ? 'warning' : 'info' %}
{% set display_matricule = inscription.displayMatricule ?: '-' %}
{% set timeline = [
    {
        key: 'SOUMISE',
        label: 'Demande enregistrée',
        meta: inscription.dateSoumission ? inscription.dateSoumission|date('d/m/Y H:i') : 'Enregistrée',
        done: true
    },
    {
        key: 'A_TESTER',
        label: 'À tester',
        meta: inscription.statut == 'A_TESTER' ? 'Statut actuel' : 'Orientation pédagogique',
        done: inscription.statut in ['A_TESTER', 'A_AFFECTER', 'VALIDEE']
    },
    {
        key: 'A_AFFECTER',
        label: 'Affectée',
        meta: inscription.classeFinale ? inscription.classeFinale.nom : 'Affectation à finaliser',
        done: inscription.statut in ['A_AFFECTER', 'VALIDEE']
    },
    {
        key: 'VALIDEE',
        label: 'Validée',
        meta: inscription.dateValidation ? inscription.dateValidation|date('d/m/Y H:i') : 'Validation administrative',
        done: inscription.statut == 'VALIDEE'
    },
    {
        key: 'REJETEE',
        label: 'Rejetée',
        meta: inscription.dateRejet ? inscription.dateRejet|date('d/m/Y H:i') : 'Non rejetée',
        done: inscription.statut == 'REJETEE'
    }
] %}

{% block title %}{{ inscription.numeroDossier }} | SALSABIL Madrassas{% endblock %}

{% block content %}
    <style>
        .registration-program-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(260px,1fr));gap:1rem}
        .registration-program-card{border:1px solid rgba(15,92,69,.12);border-radius:16px;padding:1rem;background:linear-gradient(180deg,#fff,#fbfcfd);box-shadow:0 16px 40px rgba(20,30,44,.08)}
        .registration-program-card.is-muted{opacity:.68}
        .registration-program-card-head{display:flex;align-items:flex-start;justify-content:space-between;gap:1rem;margin-bottom:1rem}
        .registration-program-card-head span{display:block;font-size:.72rem;font-weight:800;letter-spacing:.04em;text-transform:uppercase;color:#64748b}
        .registration-program-card-head strong{display:block;color:#102033}
        .registration-program-badges{display:flex;flex-wrap:wrap;gap:.35rem;justify-content:flex-end}
        .registration-program-details{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:.75rem}
        .registration-program-details div{border-radius:12px;background:#f8fafc;padding:.7rem}
        .registration-program-details span{display:block;font-size:.72rem;font-weight:800;color:#64748b;text-transform:uppercase}
        .registration-program-details strong{display:block;margin-top:.15rem;color:#1f2937}
        .registration-program-note{margin:.9rem 0 0;padding:.75rem;border-radius:12px;background:#fff7ed;color:#7c2d12}
        @media (max-width:640px){.registration-program-details{grid-template-columns:1fr}.registration-program-card-head{flex-direction:column}}
    </style>
    <div class="admin-page-stack">
        <section class="admin-page-header">
            <div>
                <p class="admin-eyebrow">DOSSIER {{ inscription.numeroDossier }}</p>
                <h1 class="admin-page-title">{{ inscription.nomComplet }}</h1>
                <p>Suivi de la demande, affectation, validation et fiche PDF.</p>
            </div>
            <a class="admin-btn admin-btn-light" href="{{ path('school_inscription_index') }}">Retour</a>
        </section>

        <section class="school-summary-grid">
            <article class="school-summary-card"><span>Statut</span><strong>{% include 'components/admin/status_badge.html.twig' with {label: status_labels[inscription.statut]|default(inscription.statut), tone: status_tone} only %}</strong></article>
            <article class="school-summary-card"><span>Type</span><strong>{% include 'components/admin/status_badge.html.twig' with {label: type_label, tone: type_tone} only %}</strong></article>
            <article class="school-summary-card"><span>Matricule</span><strong>{{ display_matricule }}</strong></article>
            <article class="school-summary-card"><span>Année</span><strong>{{ inscription.anneeScolaire ? inscription.anneeScolaire.libelle : 'Non liée' }}</strong></article>
        </section>

        <section class="admin-card">
            <div class="admin-card-head"><div><h2>Timeline du parcours</h2><p>Progression du dossier depuis la préinscription jusqu’à la décision administrative.</p></div></div>
            <div class="registration-timeline">
                {% for step in timeline %}
                    {% set item_class = step.key == inscription.statut ? 'is-current' : (step.done ? 'is-done' : 'is-pending') %}
                    <div class="registration-timeline-item {{ item_class }}">
                        <span class="registration-timeline-marker"></span>
                        <div>
                            <strong>{{ step.label }}</strong>
                            <small>{{ step.meta }}</small>
                        </div>
                    </div>
                {% endfor %}
            </div>
            <div class="admin-mini-list mt-4">
                <div class="admin-mini-item"><div><strong>Numéro de dossier</strong><small>{{ inscription.numeroDossier }}</small></div></div>
                <div class="admin-mini-item"><div><strong>École</strong><small>{{ inscription.ecole ? inscription.ecole.nom : '-' }}</small></div></div>
                <div class="admin-mini-item"><div><strong>Date de soumission</strong><small>{{ inscription.dateSoumission ? inscription.dateSoumission|date('d/m/Y H:i') : '-' }}</small></div></div>
                <div class="admin-mini-item"><div><strong>Date de validation</strong><small>{{ inscription.dateValidation ? inscription.dateValidation|date('d/m/Y H:i') : '-' }}</small></div></div>
                <div class="admin-mini-item"><div><strong>Date de rejet</strong><small>{{ inscription.dateRejet ? inscription.dateRejet|date('d/m/Y H:i') : '-' }}</small></div></div>
                <div class="admin-mini-item"><div><strong>Niveau / classe souhaités</strong><small>{{ inscription.niveauSouhaite ? inscription.niveauSouhaite.nom : '-' }}{% if inscription.classeSouhaitee %} · {{ inscription.classeSouhaitee.nom }}{% endif %}</small></div></div>
                <div class="admin-mini-item"><div><strong>Étudiant lié</strong><small>{{ inscription.etudiant ? inscription.etudiant.nomComplet ~ ' · ' ~ (inscription.etudiant.matricule ?: 'matricule à finaliser') : 'Aucun étudiant rattaché' }}</small></div></div>
                <div class="admin-mini-item"><div><strong>Fiche PDF</strong><small>{% if inscription.fichePdfPath %}<a href="{{ asset(inscription.fichePdfPath) }}" target="_blank" rel="noopener">Ouvrir la fiche</a>{% else %}Non générée{% endif %}</small></div></div>
                <div class="admin-mini-item"><div><strong>WhatsApp</strong><small>{{ inscription.whatsappNotificationSentAt ? 'Dernier envoi le ' ~ inscription.whatsappNotificationSentAt|date('d/m/Y H:i') : (inscription.whatsappLastError ? 'Erreur : ' ~ inscription.whatsappLastError : 'Aucun envoi enregistré') }}</small></div></div>
            </div>
        </section>

        <section class="admin-grid admin-grid-school">
            <div class="admin-card">
                <div class="admin-card-head"><div><h2>Informations candidat</h2><p>Données transmises depuis le formulaire public.</p></div></div>
                <div class="admin-mini-list">
                    <div class="admin-mini-item"><div><strong>Type de dossier</strong><small>{% include 'components/admin/status_badge.html.twig' with {label: type_label, tone: type_tone} only %}</small></div></div>
                    <div class="admin-mini-item"><div><strong>Matricule</strong><small>{{ display_matricule }}</small></div></div>
                    <div class="admin-mini-item"><div><strong>Nom complet</strong><small>{{ inscription.nomComplet }}</small></div></div>
                    <div class="admin-mini-item"><div><strong>Sexe</strong><small>{{ inscription.sexe ?: '-' }}</small></div></div>
                    <div class="admin-mini-item"><div><strong>Naissance</strong><small>{{ inscription.dateNaissance ? inscription.dateNaissance|date('d/m/Y') : '-' }}{% if inscription.lieuNaissance %} à {{ inscription.lieuNaissance }}{% endif %}</small></div></div>
                    <div class="admin-mini-item"><div><strong>Enfants</strong><small>{{ inscription.hasEnfants ? 'Oui' : 'Non' }}</small></div></div>
                    <div class="admin-mini-item"><div><strong>Contacts</strong><small>{{ inscription.telephonePrincipal ?: '-' }}{% if inscription.telephoneWhatsapp %} · WhatsApp {{ inscription.telephoneWhatsapp }}{% endif %}</small></div></div>
                    <div class="admin-mini-item"><div><strong>Email</strong><small>{{ inscription.email ?: '-' }}</small></div></div>
                    <div class="admin-mini-item"><div><strong>Observation</strong><small>{{ inscription.observationCandidat ?: '-' }}</small></div></div>
                </div>
            </div>

            <div class="admin-card">
                <div class="admin-card-head"><div><h2>Parcours</h2><p>Souhait candidat et décision administrative.</p></div></div>
                <div class="admin-mini-list">
                    <div class="admin-mini-item"><div><strong>Section souhaitée</strong><small>{{ inscription.sectionSouhaitee ? inscription.sectionSouhaitee.nom : '-' }}</small></div></div>
                    <div class="admin-mini-item"><div><strong>Niveau souhaité</strong><small>{{ inscription.niveauSouhaite ? inscription.niveauSouhaite.nom : '-' }}</small></div></div>
                    <div class="admin-mini-item"><div><strong>Classe souhaitée</strong><small>{{ inscription.classeSouhaitee ? inscription.classeSouhaitee.nom : '-' }}</small></div></div>
                    <div class="admin-mini-item"><div><strong>Section finale</strong><small>{{ inscription.sectionFinale ? inscription.sectionFinale.nom : '-' }}</small></div></div>
                    <div class="admin-mini-item"><div><strong>Niveau final</strong><small>{{ inscription.niveauFinal ? inscription.niveauFinal.nom : '-' }}</small></div></div>
                    <div class="admin-mini-item"><div><strong>Classe finale</strong><small>{{ inscription.classeFinale ? inscription.classeFinale.nom : 'Non affecté' }}</small></div></div>
                    <div class="admin-mini-item"><div><strong>Disponibilités</strong><small>{{ inscription.joursDisponibles ?: '-' }}{% if inscription.plageHoraireSouhaitee %} · {{ inscription.plageHoraireSouhaitee }}{% endif %}</small></div></div>
                    <div class="admin-mini-item"><div><strong>WhatsApp</strong><small>{{ inscription.whatsappNotificationSentAt ? 'Dernier envoi le ' ~ inscription.whatsappNotificationSentAt|date('d/m/Y H:i') : (inscription.whatsappLastError ?: 'Aucun envoi enregistré') }}</small></div></div>
                </div>
            </div>
        </section>

        <section class="admin-card">
            <div class="admin-card-head">
                <div>
                    <h2>Parcours demandés</h2>
                    <p>Programmes, niveaux et classes souhaités ou finalisés pour ce dossier.</p>
                </div>
                {% if inscription.canBeAssigned %}
                    <a class="admin-btn admin-btn-primary" href="{{ path('school_inscription_affecter', {id: inscription.id}) }}">
                        <span class="material-symbols-outlined">add</span>
                        Modifier / ajouter un parcours
                    </a>
                {% endif %}
            </div>
            <div class="registration-program-grid">
                {% if inscription.parcours|length > 0 %}
                    {% for parcours in inscription.parcours %}
                        <article class="registration-program-card {{ parcours.actif ? '' : 'is-muted' }}">
                            <div class="registration-program-card-head">
                                <div>
                                    <span>Parcours {{ loop.index }}</span>
                                    <strong>{{ parcours.sectionSouhaitee ? parcours.sectionSouhaitee.nom : (parcours.sectionFinale ? parcours.sectionFinale.nom : 'Programme non précisé') }}</strong>
                                </div>
                                <div class="registration-program-badges">
                                    {% if parcours.principal %}{% include 'components/admin/status_badge.html.twig' with {label: 'Principal', tone: 'success'} only %}{% endif %}
                                    {% if not parcours.actif %}{% include 'components/admin/status_badge.html.twig' with {label: 'Inactif', tone: 'neutral'} only %}{% endif %}
                                </div>
                            </div>
                            <div class="registration-program-details">
                                <div><span>Niveau demandé</span><strong>{{ parcours.niveauSouhaite ? parcours.niveauSouhaite.nom : '-' }}</strong></div>
                                <div><span>Classe demandée</span><strong>{{ parcours.classeSouhaitee ? parcours.classeSouhaitee.nom : '-' }}</strong></div>
                                <div><span>Langue</span><strong>{{ parcours.langueSouhaitee ?: '-' }}</strong></div>
                                <div><span>Mode</span><strong>{{ parcours.modeSouhaite ?: '-' }}</strong></div>
                                <div><span>Section finale</span><strong>{{ parcours.sectionFinale ? parcours.sectionFinale.nom : '-' }}</strong></div>
                                <div><span>Niveau final</span><strong>{{ parcours.niveauFinal ? parcours.niveauFinal.nom : '-' }}</strong></div>
                                <div><span>Classe finale</span><strong>{{ parcours.classeFinale ? parcours.classeFinale.nom : 'Non affecté' }}</strong></div>
                                <div><span>Tarif proposé</span><strong>{{ parcours.tarifPropose ? parcours.tarifPropose|number_format(0, ',', ' ') ~ ' FCFA' : '-' }}</strong></div>
                            </div>
                            {% if parcours.observation %}
                                <p class="registration-program-note">{{ parcours.observation }}</p>
                            {% endif %}
                        </article>
                    {% endfor %}
                {% else %}
                    <article class="registration-program-card">
                        <div class="registration-program-card-head">
                            <div>
                                <span>Parcours historique</span>
                                <strong>{{ inscription.sectionSouhaitee ? inscription.sectionSouhaitee.nom : (inscription.sectionFinale ? inscription.sectionFinale.nom : 'Programme non précisé') }}</strong>
                            </div>
                            {% include 'components/admin/status_badge.html.twig' with {label: 'Principal', tone: 'success'} only %}
                        </div>
                        <div class="registration-program-details">
                            <div><span>Niveau demandé</span><strong>{{ inscription.niveauSouhaite ? inscription.niveauSouhaite.nom : '-' }}</strong></div>
                            <div><span>Classe demandée</span><strong>{{ inscription.classeSouhaitee ? inscription.classeSouhaitee.nom : '-' }}</strong></div>
                            <div><span>Langue</span><strong>{{ inscription.langueSouhaitee ?: '-' }}</strong></div>
                            <div><span>Mode</span><strong>{{ inscription.modeSouhaite ?: '-' }}</strong></div>
                            <div><span>Section finale</span><strong>{{ inscription.sectionFinale ? inscription.sectionFinale.nom : '-' }}</strong></div>
                            <div><span>Niveau final</span><strong>{{ inscription.niveauFinal ? inscription.niveauFinal.nom : '-' }}</strong></div>
                            <div><span>Classe finale</span><strong>{{ inscription.classeFinale ? inscription.classeFinale.nom : 'Non affecté' }}</strong></div>
                            <div><span>Tarif proposé</span><strong>-</strong></div>
                        </div>
                    </article>
                {% endif %}
            </div>
        </section>

        <section class="admin-card registration-action-panel">
            <div class="admin-card-head">
                <div>
                    <h2>Actions administratives</h2>
                    <p>Pilotez le traitement du dossier, les documents et les opérations sensibles.</p>
                </div>
            </div>

            <div class="registration-action-groups">
                <div class="registration-action-group">
                    <div class="registration-action-group-head">
                        <span class="material-symbols-outlined">account_tree</span>
                        <div>
                            <strong>Workflow</strong>
                            <small>Orientation, affectation et décision finale.</small>
                        </div>
                    </div>
                    <div class="registration-action-grid">
                        {% if inscription.canBeMarkedForTest %}
                            <form method="post" action="{{ path('school_inscription_marquer_a_tester', {id: inscription.id}) }}">
                                <input type="hidden" name="_token" value="{{ csrf_token('mark_test_' ~ inscription.id) }}">
                                <button class="registration-action-card" type="submit">
                                    <span class="material-symbols-outlined">rule</span>
                                    <strong>Marquer à tester</strong>
                                    <small>Signaler que le niveau doit être évalué avant affectation.</small>
                                </button>
                            </form>
                        {% endif %}
                        {% if inscription.canBeAssigned %}
                            <a class="registration-action-card" href="{{ path('school_inscription_affecter', {id: inscription.id}) }}">
                                <span class="material-symbols-outlined">assignment_ind</span>
                                <strong>Affecter</strong>
                                <small>Choisir la section, le niveau et la classe finale.</small>
                            </a>
                        {% endif %}
                        {% if inscription.canBeValidated %}
                            <a class="registration-action-card registration-action-card-success" href="{{ path('school_inscription_valider', {id: inscription.id}) }}">
                                <span class="material-symbols-outlined">verified</span>
                                <strong>Valider</strong>
                                <small>Créer ou rattacher l’étudiant et confirmer l’inscription.</small>
                            </a>
                        {% endif %}
                        {% if inscription.canBeRejected %}
                            <a class="registration-action-card registration-action-card-warning" href="{{ path('school_inscription_rejeter', {id: inscription.id}) }}">
                                <span class="material-symbols-outlined">block</span>
                                <strong>Rejeter</strong>
                                <small>Refuser le dossier avec un motif transmis à l’administration.</small>
                            </a>
                        {% endif %}
                        {% if not inscription.canBeMarkedForTest and not inscription.canBeAssigned and not inscription.canBeValidated and not inscription.canBeRejected %}
                            <div class="registration-action-card registration-action-card-muted" aria-disabled="true">
                                <span class="material-symbols-outlined">lock</span>
                                <strong>Dossier verrouillé</strong>
                                <small>Les actions de workflow ne sont plus disponibles pour ce statut.</small>
                            </div>
                        {% endif %}
                    </div>
                </div>

                <div class="registration-action-group">
                    <div class="registration-action-group-head">
                        <span class="material-symbols-outlined">description</span>
                        <div>
                            <strong>Documents</strong>
                            <small>Fiche PDF et règlement intérieur.</small>
                        </div>
                    </div>
                    <div class="registration-action-grid registration-action-grid-docs">
                        <form method="post" action="{{ path('school_inscription_fiche_pdf', {id: inscription.id}) }}">
                            <input type="hidden" name="_token" value="{{ csrf_token('pdf_inscription_' ~ inscription.id) }}">
                            <button class="registration-action-card" type="submit">
                                <span class="material-symbols-outlined">picture_as_pdf</span>
                                <strong>Générer fiche PDF</strong>
                                <small>Créer une fiche propre avec le statut actuel du dossier.</small>
                            </button>
                        </form>
                        {% if inscription.fichePdfPath %}
                            <a class="registration-action-card" href="{{ asset(inscription.fichePdfPath) }}" target="_blank" rel="noopener">
                                <span class="material-symbols-outlined">open_in_new</span>
                                <strong>Ouvrir le PDF</strong>
                                <small>Consulter la dernière fiche générée dans un nouvel onglet.</small>
                            </a>
                        {% else %}
                            <div class="registration-action-card registration-action-card-muted" aria-disabled="true">
                                <span class="material-symbols-outlined">draft</span>
                                <strong>PDF non généré</strong>
                                <small>Générez d’abord la fiche pour pouvoir l’ouvrir.</small>
                            </div>
                        {% endif %}
                    </div>
                </div>

                <div class="registration-action-group">
                    <div class="registration-action-group-head">
                        <span class="material-symbols-outlined">chat</span>
                        <div>
                            <strong>WhatsApp</strong>
                            <small>Relancer la notification adaptée au statut actuel.</small>
                        </div>
                    </div>
                    <div class="registration-action-grid registration-action-grid-docs">
                        <form method="post" action="{{ path('school_inscription_renvoyer_whatsapp', {id: inscription.id}) }}">
                            <input type="hidden" name="_token" value="{{ csrf_token('resend_whatsapp_' ~ inscription.id) }}">
                            <button class="registration-action-card" type="submit">
                                <span class="material-symbols-outlined">send</span>
                                <strong>Renvoyer WhatsApp</strong>
                                <small>{{ inscription.whatsappNotificationSentAt ? 'Dernier envoi le ' ~ inscription.whatsappNotificationSentAt|date('d/m/Y H:i') : 'Aucun envoi confirmé pour ce dossier.' }}</small>
                            </button>
                        </form>
                        {% if inscription.whatsappLastError %}
                            <div class="registration-action-card registration-action-card-warning" aria-disabled="true">
                                <span class="material-symbols-outlined">error</span>
                                <strong>Dernière erreur</strong>
                                <small>{{ inscription.whatsappLastError }}</small>
                            </div>
                        {% endif %}
                    </div>
                </div>

                <div class="registration-action-group registration-action-group-danger">
                    <div class="registration-action-group-head">
                        <span class="material-symbols-outlined">warning</span>
                        <div>
                            <strong>Danger zone</strong>
                            <small>Action définitive sur le dossier d’inscription.</small>
                        </div>
                    </div>
                    <div class="registration-action-grid registration-action-grid-danger">
                        {% if inscription.canBeDeleted %}
                            <form method="post" action="{{ path('school_inscription_delete', {id: inscription.id}) }}" onsubmit="return confirm('Supprimer définitivement ce dossier d’inscription ? Cette action ne supprimera pas l’étudiant déjà validé.');">
                                <input type="hidden" name="_token" value="{{ csrf_token('delete_inscription_' ~ inscription.id) }}">
                                <button class="registration-action-card registration-action-card-danger" type="submit">
                                    <span class="material-symbols-outlined">delete</span>
                                    <strong>Supprimer</strong>
                                    <small>Retirer définitivement ce dossier. L’étudiant validé éventuel est conservé.</small>
                                </button>
                            </form>
                        {% else %}
                            <div class="registration-action-card registration-action-card-muted" aria-disabled="true">
                                <span class="material-symbols-outlined">lock</span>
                                <strong>Suppression bloquée</strong>
                                <small>Un dossier validé reste consultable pour conserver la trace administrative.</small>
                            </div>
                        {% endif %}
                    </div>
                </div>
            </div>
        </section>
    </div>
{% endblock %}
