@extends('layouts.front')

@section('content')
@php
    $resultCollection = collect($resultCards);
    $topPrize = (float) $resultCollection->pluck('first_prize')->filter()->max();
    $totalJoinAmount = (float) $resultCollection->map(fn ($card) => (float) ($card['contest']->joinamt ?? 0))->sum();
    $averageWinningPercentage = $resultCollection->isNotEmpty()
        ? round($resultCollection->map(fn ($card) => (float) ($card['contest']->winpertange ?? 0))->avg(), 1)
        : 0;
@endphp
<div class="jel-portal-page jel-dashboard-page">
    <section class="jel-portal-hero">
        <div>
            <span class="jel-portal-kicker">Scholarship Results</span>
            <h1>Your completed scholarship exam results.</h1>
            <p>Completed competitions ke result-enabled cards yahan dikhte hain. Details open karke answer sheet, result aur ranking flow dekh sakte hain.</p>
        </div>
        <div class="jel-portal-hero-panel">
            <article>
                <strong>{{ $resultCollection->count() }}</strong>
                <span>Completed Result Cards</span>
            </article>
            <article>
                <strong>{{ $resultCollection->filter(fn ($card) => ! empty($card['last_rank']))->count() }}</strong>
                <span>Reward Ladders Ready</span>
            </article>
            <article>
                <strong>&#8377;{{ number_format($topPrize, 2) }}</strong>
                <span>Top First Prize</span>
            </article>
            <article>
                <strong>{{ $averageWinningPercentage }}%</strong>
                <span>Average Winning %</span>
            </article>
        </div>
    </section>

    <section class="jel-history-intro">
        <div class="jel-result-summary-grid jel-history-summary-grid">
            <article>
                <span>Joined Value</span>
                <strong>&#8377;{{ number_format($totalJoinAmount, 2) }}</strong>
            </article>
            <article>
                <span>Published Result Cards</span>
                <strong>{{ $resultCollection->count() }}</strong>
            </article>
            <article>
                <span>Top First Prize</span>
                <strong>&#8377;{{ number_format($topPrize, 2) }}</strong>
            </article>
            <article>
                <span>Average Reward %</span>
                <strong>{{ $averageWinningPercentage }}%</strong>
            </article>
        </div>

        <div class="jel-inline-actions">
            <a class="jel-portal-button jel-portal-button-muted" href="{{ route('competitionlist') }}">Competition Dashboard</a>
            <a class="jel-portal-button jel-portal-button-secondary" href="{{ route('scoringresult') }}">Scoring Table</a>
        </div>
    </section>

    @if (! empty($resultCards))
        <section class="jel-portal-board">
            <div class="jel-portal-board-head">
                <div>
                    <span class="jel-portal-kicker">My Results</span>
                    <h2>Scholarship Exam Cards</h2>
                </div>
            </div>

            <div class="jel-portal-grid">
                @foreach ($resultCards 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">Competition result aur reward ladder dekhne ke liye details page open kijiye.</p>

                        <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 Result Details</a>
                    </article>
                @endforeach
            </div>
        </section>
    @else
        <section class="jel-portal-empty">
            <h2>No scholarship results available yet.</h2>
            <p>Competition result publish hone ke baad cards yahan show honge.</p>
            <div class="jel-empty-actions">
                <a class="jel-portal-button jel-portal-button-muted" href="{{ route('competitionlist') }}">Back to Dashboard</a>
                <a class="jel-portal-button jel-portal-button-secondary" href="{{ route('gamehistory') }}">View Activity</a>
            </div>
        </section>
    @endif
</div>
@endsection
