@extends('layouts.front')

@section('content')
@php
    $currentRankEntry = collect($leaderboarduserrank)->first(function ($entry) {
        return (int) ($entry->createdby ?? 0) === (int) auth()->id();
    });
    $passedSubjects = collect($resultPage['subject_rows'] ?? [])
        ->filter(fn ($subjectRow) => (float) ($subjectRow['percentage'] ?? 0) >= 40)
        ->count();
@endphp

<div class="jel-portal-page">
    @include('front.partials.flash-messages')

    <section class="jel-result-layout">
        <div class="jel-history-intro">
            <span class="jel-portal-kicker">Exam Result</span>
            <h1>Subject-wise performance and scholarship ranking.</h1>
            <p>Yahan aapke marks, percentage, pass-fail status aur scholarship leaderboard ek saath show hote hain. Result publish hote hi ye page updated data dikhata hai.</p>
        </div>

        @if ($useranswerdetails)
            <section class="jel-table-card">
                <div class="jel-history-card-head">
                    <div>
                        <span class="jel-portal-kicker">Overall Summary</span>
                        <h2>Your Exam Snapshot</h2>
                    </div>
                    <span class="jel-history-count">{{ count($resultPage['subject_rows']) }} Subjects</span>
                </div>

                <div class="jel-result-summary-grid">
                    <article>
                        <span>Total Marks</span>
                        <strong>{{ $useranswerdetails->totalmarks }}</strong>
                    </article>
                    <article>
                        <span>Total Percentage</span>
                        <strong>{{ $useranswerdetails->totalpercentage }}%</strong>
                    </article>
                    <article>
                        <span>Result</span>
                        <strong class="{{ (int) $useranswerdetails->status === 1 ? 'jel-pass-text' : 'jel-fail-text' }}">
                            {{ (int) $useranswerdetails->status === 1 ? 'Pass' : 'Fail' }}
                        </strong>
                    </article>
                    <article>
                        <span>Your Scholarship Rank</span>
                        <strong>{{ $currentRankEntry?->display_rank ?? '-' }}</strong>
                    </article>
                    <article>
                        <span>Qualified Subjects</span>
                        <strong>{{ $passedSubjects }} / {{ count($resultPage['subject_rows']) }}</strong>
                    </article>
                    <article>
                        <span>Qualified Students</span>
                        <strong>{{ count($leaderboarduserrank) }}</strong>
                    </article>
                </div>

                <div class="jel-inline-actions">
                    <a class="jel-portal-button jel-portal-button-muted" href="{{ route('examsanswersheet', ['id' => $useranswerdetails->competitionassignid]) }}">Open Answer Sheet</a>
                    <a class="jel-portal-button jel-portal-button-secondary" href="{{ route('contestdetails', ['id' => $useranswerdetails->competitionid, 'assignment' => $useranswerdetails->competitionassignid]) }}">Back to Competition</a>
                </div>
            </section>

            <section class="jel-table-card">
                <div class="jel-history-card-head">
                    <div>
                        <span class="jel-portal-kicker">Subject Analysis</span>
                        <h2>Subject-wise Result</h2>
                    </div>
                </div>

                <div class="jel-table-wrap">
                    <table class="jel-data-table jel-data-table-stack">
                        <thead>
                            <tr>
                                <th>Subject</th>
                                <th>Marks</th>
                                <th>Percentage</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($resultPage['subject_rows'] as $subjectRow)
                                <tr>
                                    <td data-label="Subject">{{ $subjectRow['label'] }}</td>
                                    <td data-label="Marks">{{ $subjectRow['marks'] }}</td>
                                    <td data-label="Percentage">{{ $subjectRow['percentage'] }}%</td>
                                    <td data-label="Status">
                                        <span class="jel-status-badge {{ $subjectRow['percentage'] >= 40 ? 'jel-status-success' : 'jel-status-danger' }}">
                                            {{ $subjectRow['percentage'] >= 40 ? 'Pass' : 'Fail' }}
                                        </span>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </section>
        @else
            <section class="jel-portal-empty">
                <h2>Result not available yet.</h2>
                <p>Jaise hi exam evaluation complete hogi, aapka subject-wise result yahan show ho jayega.</p>
            </section>
        @endif

        <section class="jel-table-card">
            <div class="jel-history-card-head">
                <div>
                    <span class="jel-portal-kicker">Scholarship Rank</span>
                    <h2>Qualified Leaderboard</h2>
                </div>
                <span class="jel-history-count">{{ count($leaderboarduserrank) }} Qualified</span>
            </div>

            @if ($leadreboard->count() > 0)
                <div class="jel-leaderboard-list">
                    <div class="jel-leaderboard-head">
                        <span>Name</span>
                        <span>Mobile</span>
                        <span>Rank</span>
                    </div>

                    @foreach ($leadreboard as $leadreboarddata)
                        <article class="jel-leaderboard-row">
                            <strong>{{ $leadreboarddata->user?->name ?? 'Student' }}</strong>
                            <span>{{ $leadreboarddata->masked_mobile }}</span>
                            <span>#{{ $leadreboarddata->display_rank }}</span>
                        </article>
                    @endforeach
                </div>

                @if ($leadreboard->hasPages())
                    <div class="jel-simple-pagination">
                        @if ($leadreboard->onFirstPage())
                            <span class="is-disabled">Previous</span>
                        @else
                            <a href="{{ $leadreboard->previousPageUrl() }}">Previous</a>
                        @endif

                        <span>Page {{ $leadreboard->currentPage() }} of {{ $leadreboard->lastPage() }}</span>

                        @if ($leadreboard->hasMorePages())
                            <a href="{{ $leadreboard->nextPageUrl() }}">Next</a>
                        @else
                            <span class="is-disabled">Next</span>
                        @endif
                    </div>
                @endif
            @else
                <div class="jel-empty-state">
                    <h3>No qualified students found yet.</h3>
                    <p>Leaderboard result publish hone ke baad yahan show hoga.</p>
                </div>
            @endif
        </section>
    </section>
</div>
@endsection
