@extends('layouts.front')

@section('content')
@php
    $competitionSections = collect($competitionSections ?? []);
    $competitionTimeline = collect($competitionTimeline ?? []);
    $competitionCount = $competitionSections->sum(
        fn ($section) => count(data_get($section, 'cards', []))
    );
    $studentClassName = $studentsprofile->classname ?? 'Profile pending';
@endphp
<div class="jel-portal-page jel-dashboard-page">
    @if ($bannerimages->count() > 0)
        <div id="jelPortalBanner" class="carousel slide jel-portal-banner" data-bs-ride="carousel">
            <div class="carousel-indicators">
                @foreach ($bannerimages as $bannerimagesdata)
                    <button
                        type="button"
                        data-bs-target="#jelPortalBanner"
                        data-bs-slide-to="{{ $loop->index }}"
                        @class(['active' => $loop->first])
                        @if ($loop->first) aria-current="true" @endif
                    ></button>
                @endforeach
            </div>

            <div class="carousel-inner">
                @foreach ($bannerimages as $bannerimagesdata)
                    <div @class(['carousel-item', 'active' => $loop->first])>
                        <img src="{{ $bannerimagesdata->image_url }}" alt="{{ $bannerimagesdata->bannrename }}" class="d-block w-100">
                    </div>
                @endforeach
            </div>

            <button class="carousel-control-prev" type="button" data-bs-target="#jelPortalBanner" data-bs-slide="prev">
                <span class="carousel-control-prev-icon"></span>
            </button>
            <button class="carousel-control-next" type="button" data-bs-target="#jelPortalBanner" data-bs-slide="next">
                <span class="carousel-control-next-icon"></span>
            </button>
        </div>
    @endif

    <section class="jel-portal-hero">
        <div>
            <span class="jel-portal-kicker">Student Dashboard</span>
            <h1>Available scholarship exams ek jagah par.</h1>
            <p>
                Class-wise board pattern competitions ko quickly compare kijiye, prize pool samajhiye aur jis exam
                mein fit baithe uski details open kijiye.
            </p>
        </div>
        <div class="jel-portal-hero-panel">
            <article>
                <strong>{{ count($competitionSections) }}</strong>
                <span>Active Board Sections</span>
            </article>
            <article>
                <strong>{{ $competitionCount }}</strong>
                <span>Open Competitions</span>
            </article>
            <article>
                <strong>{{ $studentClassName }}</strong>
                <span>Current Profile Class</span>
            </article>
            <article>
                <strong>&#8377;{{ number_format((float) ($walletBalance ?? 0), 2) }}</strong>
                <span>Current Wallet Balance</span>
            </article>
        </div>
    </section>

    @forelse ($competitionSections as $section)
        <section class="jel-portal-board">
            <div class="jel-portal-board-head">
                <div>
                    <span class="jel-portal-kicker">{{ $section['board']->showname }}</span>
                    <h2>{{ $section['board']->showname }} Scholarship Exams</h2>
                </div>
                <span class="jel-portal-badge">{{ count($section['cards']) }} Active</span>
            </div>

            <div class="jel-portal-grid">
                @foreach ($section['cards'] as $card)
                    <article class="jel-portal-card">
                        <div class="jel-portal-card-top">
                            <span class="jel-portal-pill">{{ $card['assignment']->groupclassname }}</span>
                            <span class="jel-portal-price">&#8377;{{ number_format((float) $card['contest']->joinamt, 2) }}</span>
                        </div>

                        <h3>{{ $card['contest']->contesttitile }}</h3>
                        <p class="jel-portal-copy">
                            Join fee ke sath seats, dates aur prize structure check karke exam details open kijiye.
                        </p>

                        <div class="jel-portal-progress">
                            <div class="jel-portal-progress-bar">
                                <div class="jel-portal-progress-fill" style="width: {{ $card['joined_percent'] }}%"></div>
                            </div>
                            <div class="jel-portal-progress-meta">
                                <span>{{ round($card['joined_percent']) }}% joined</span>
                                <span>{{ $card['remaining_seat'] }} seats left</span>
                            </div>
                        </div>

                        <div class="jel-portal-meta-grid">
                            <div>
                                <span>Start Date</span>
                                <strong>{{ optional($card['assignment']->startdate)->format('d M Y') }}</strong>
                            </div>
                            <div>
                                <span>Last Date</span>
                                <strong>{{ optional($card['assignment']->enddate)->format('d M Y') }}</strong>
                            </div>
                            <div>
                                <span>Exam Date</span>
                                <strong>{{ optional($card['assignment']->examdate)->format('d M Y') }}</strong>
                            </div>
                            <div>
                                <span>Exam Time</span>
                                <strong>
                                    {{ $card['assignment']->examendtime ? date('h:i A', strtotime((string) $card['assignment']->examendtime)) : '-' }}
                                </strong>
                            </div>
                        </div>

                        <div class="jel-portal-stat-row">
                            <div>
                                <small>1st Prize</small>
                                <strong>&#8377;{{ number_format((float) $card['first_prize'], 2) }}</strong>
                            </div>
                            <div>
                                <small>Winning %</small>
                                <strong>{{ $card['contest']->winpertange }}%</strong>
                            </div>
                            <div>
                                <small>Last Rank</small>
                                <strong>{{ $card['last_rank'] ?? '-' }}</strong>
                            </div>
                        </div>

                        <a class="jel-portal-button" href="{{ $card['detail_url'] }}">View Exam Details</a>
                    </article>
                @endforeach
            </div>
        </section>
    @empty
        <section class="jel-portal-empty">
            <h2>No active scholarship exams available right now.</h2>
            <p>
                Abhi aapki class ke liye koi active competition open nahi hai. Profile, wallet aur previous competition
                timeline yahin se track kar sakte hain.
            </p>
            <div class="jel-empty-actions">
                <a class="jel-portal-button" href="{{ route('editprofile') }}">Update Profile</a>
                <a class="jel-portal-button jel-portal-button-muted" href="{{ route('scholershipresult') }}">Check Results</a>
            </div>
        </section>
    @endforelse

    @if ($competitionTimeline->isNotEmpty())
        <section class="jel-portal-board">
            <div class="jel-portal-board-head">
                <div>
                    <span class="jel-portal-kicker">Competition Timeline</span>
                    <h2>Recent and upcoming competitions for your class</h2>
                </div>
                <span class="jel-portal-badge">{{ $competitionTimeline->count() }} Records</span>
            </div>

            <div class="jel-portal-grid">
                @foreach ($competitionTimeline as $timelineCard)
                    <article class="jel-portal-card">
                        <div class="jel-portal-card-top">
                            <span class="jel-portal-pill">{{ $timelineCard['assignment']->groupclassname }}</span>
                            <span class="jel-status-badge jel-status-{{ $timelineCard['status_tone'] }}">
                                {{ $timelineCard['status_label'] }}
                            </span>
                        </div>

                        <h3>{{ $timelineCard['contest']->contesttitile }}</h3>
                        <p class="jel-portal-copy">{{ $timelineCard['status_copy'] }}</p>

                        <div class="jel-portal-meta-grid">
                            <div>
                                <span>Board</span>
                                <strong>{{ $timelineCard['assignment']->board?->showname ?? 'Board not linked' }}</strong>
                            </div>
                            <div>
                                <span>Join Amount</span>
                                <strong>&#8377;{{ number_format((float) $timelineCard['contest']->joinamt, 2) }}</strong>
                            </div>
                            <div>
                                <span>Exam Date</span>
                                <strong>{{ optional($timelineCard['assignment']->examdate)->format('d M Y') ?: '-' }}</strong>
                            </div>
                            <div>
                                <span>Result Date</span>
                                <strong>{{ optional($timelineCard['assignment']->resultdate)->format('d M Y') ?: '-' }}</strong>
                            </div>
                        </div>

                        <div class="jel-portal-stat-row">
                            <div>
                                <small>1st Prize</small>
                                <strong>&#8377;{{ number_format((float) $timelineCard['first_prize'], 2) }}</strong>
                            </div>
                            <div>
                                <small>Winning %</small>
                                <strong>{{ $timelineCard['contest']->winpertange }}%</strong>
                            </div>
                            <div>
                                <small>Last Rank</small>
                                <strong>{{ $timelineCard['last_rank'] ?? '-' }}</strong>
                            </div>
                        </div>

                        <a class="jel-portal-button" href="{{ $timelineCard['detail_url'] }}">Open Competition Details</a>
                    </article>
                @endforeach
            </div>
        </section>
    @endif
</div>
@endsection
