@extends('siswa.layouts.app')

@section('title', 'Hasil Challenge')

@push('styles')
<style>
.hasil-wrapper { max-width: 680px; margin: 0 auto; padding: 0 0 40px; }

.hasil-hero {
    background: linear-gradient(135deg, #667eea, #764ba2);
    border-radius: 24px;
    padding: 36px 28px;
    text-align: center;
    color: white;
    margin-bottom: 24px;
    position: relative;
    overflow: hidden;
}

.hasil-emoji { margin-bottom: 10px; }
.hasil-emoji img { width: 64px; height: 64px; object-fit: contain; }

.hasil-title   { font-size: 22px; font-weight: 800; margin-bottom: 4px; }
.hasil-subtitle { font-size: 14px; opacity: 0.85; margin-bottom: 20px; }

.hasil-exp {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    background: rgba(255,255,255,0.2);
    border-radius: 99px;
    padding: 8px 24px;
    font-size: 20px;
    font-weight: 800;
}
.hasil-exp img { width: 22px; height: 22px; object-fit: contain; }

.stat-row { display: grid; grid-template-columns: repeat(3,1fr); gap: 14px; margin-bottom: 24px; }
.stat-box { background: white; border-radius: 16px; border: 2px solid #e5e5e5; padding: 18px 14px; text-align: center; }
.stat-num  { font-size: 28px; font-weight: 800; margin: 0; }
.stat-label { font-size: 12px; color: #94a3b8; margin: 4px 0 0; font-weight: 500; }

.custom-card { background: white; border-radius: 20px; border: 2px solid #e5e5e5; padding: 24px; margin-bottom: 16px; }

.section-title {
    font-size: 16px;
    font-weight: 700;
    color: #1e293b;
    margin-bottom: 16px;
    display: flex;
    align-items: center;
    gap: 8px;
}
.section-title img { width: 20px; height: 20px; object-fit: contain; }

.soal-review { border: 2px solid #e2e8f0; border-radius: 14px; padding: 18px; margin-bottom: 12px; }
.soal-review.benar { border-color: #22c55e; background: #f0fdf4; }
.soal-review.salah { border-color: #ef4444; background: #fef2f2; }

.review-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.review-number { font-size: 12px; font-weight: 700; color: #64748b; }

.review-status {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 12px;
    font-weight: 700;
    padding: 3px 10px;
    border-radius: 99px;
}
.review-status img { width: 13px; height: 13px; object-fit: contain; }
.review-status.benar { background: #dcfce7; color: #16a34a; }
.review-status.salah { background: #fee2e2; color: #dc2626; }

.review-pertanyaan { font-size: 14px; font-weight: 600; color: #1e293b; margin-bottom: 12px; line-height: 1.6; }

.opsi-review {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    border-radius: 10px;
    font-size: 13px;
    margin-bottom: 6px;
    background: #f8fafc;
}
.opsi-review.jawaban-benar { background: #dcfce7; font-weight: 700; color: #15803d; }
.opsi-review.salah-dipilih  { background: #fee2e2; color: #991b1b; font-weight: 700; }

.opsi-circle {
    width: 26px; height: 26px;
    border-radius: 50%;
    background: #e2e8f0;
    display: flex; align-items: center; justify-content: center;
    font-size: 11px; font-weight: 700; flex-shrink: 0;
}
.opsi-review.jawaban-benar .opsi-circle { background: #22c55e; color: white; }
.opsi-review.salah-dipilih .opsi-circle  { background: #ef4444; color: white; }

.opsi-tag {
    margin-left: auto;
    font-size: 12px;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
    white-space: nowrap;
}
.opsi-tag img { width: 13px; height: 13px; object-fit: contain; }

.btn-back {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 8px;
    background: linear-gradient(135deg, #667eea, #764ba2);
    color: white;
    border-radius: 12px;
    padding: 12px;
    font-size: 14px;
    font-weight: 600;
    text-decoration: none;
    margin-top: 20px;
}
.btn-back:hover { opacity: 0.9; color: white; }
</style>
@endpush

@section('content')

@php
    if ($persentase >= 80) {
        $emojiSrc = asset('images/icon/siswac/confetti.png');
        $emojiAlt = 'Selamat, hasil luar biasa!';
    } elseif ($persentase >= 60) {
        $emojiSrc = asset('images/icon/siswac/sip.png');
        $emojiAlt = 'Bagus, terus tingkatkan!';
    } else {
        $emojiSrc = asset('images/icon/siswac/muscle.png');
        $emojiAlt = 'Semangat, jangan menyerah!';
    }

    $pesan = $persentase >= 80
        ? 'Luar biasa! Kamu menguasai materi ini!'
        : ($persentase >= 60 ? 'Bagus! Terus tingkatkan kemampuanmu!' : 'Jangan menyerah! Terus semangat belajar!');
@endphp

<div class="hasil-wrapper">

    <div class="hasil-hero">
        <div class="hasil-emoji">
            <img src="{{ $emojiSrc }}" alt="{{ $emojiAlt }}">
        </div>
        <div class="hasil-title">{{ $challenge->judul_challenge }}</div>
        <div class="hasil-subtitle">{{ $pesan }}</div>
        <div class="hasil-exp">
            <img src="{{ asset('images/icon/siswac/star.png') }}" alt="Ikon bintang EXP">
            +{{ $peserta->exp }} EXP didapat!
        </div>
    </div>

    <div class="stat-row">
        <div class="stat-box">
            <p class="stat-num" style="color:#22c55e">{{ $benar }}</p>
            <p class="stat-label">Jawaban Benar</p>
        </div>
        <div class="stat-box">
            <p class="stat-num" style="color:#ef4444">{{ $salah }}</p>
            <p class="stat-label">Jawaban Salah</p>
        </div>
        <div class="stat-box">
            <p class="stat-num" style="color:#667eea">{{ $persentase }}%</p>
            <p class="stat-label">Skor</p>
        </div>
    </div>

    <div class="custom-card">
        <p class="section-title">
            <img src="{{ asset('images/icon/siswac/buku1.png') }}" alt="Ikon buku">
            Pembahasan Jawaban
        </p>

        @foreach($challenge->soal as $i => $soal)
        @php
            $jwbSiswa = strtoupper($jawabanSiswa[$soal->id_soal] ?? '');
            $jwbBenar = strtoupper($soal->jawaban_benar);
            $isBenar  = $jwbSiswa === $jwbBenar;
        @endphp
        <div class="soal-review {{ $isBenar ? 'benar' : 'salah' }}">
            <div class="review-header">
                <span class="review-number">Soal {{ $i + 1 }}</span>
                <span class="review-status {{ $isBenar ? 'benar' : 'salah' }}">
                    @if($isBenar)
                        <img src="{{ asset('images/icon/siswac/v.png') }}" alt="Centang benar"> Benar
                    @else
                        <img src="{{ asset('images/icon/siswac/x.png') }}" alt="Tanda salah"> Salah
                    @endif
                </span>
            </div>

            <p class="review-pertanyaan">{{ $soal->pertanyaan }}</p>

            @foreach(['A','B','C','D'] as $opsi)
            @php
                $key        = 'opsi_' . strtolower($opsi);
                $isDipilih  = $jwbSiswa === $opsi;
                $isJwbBenar = $jwbBenar === $opsi;
                $cls = $isJwbBenar ? 'jawaban-benar' : ($isDipilih ? 'salah-dipilih' : '');
            @endphp
            <div class="opsi-review {{ $cls }}">
                <span class="opsi-circle">{{ $opsi }}</span>
                <span>{{ $soal->$key }}</span>
                @if($isJwbBenar)
                    <span class="opsi-tag">
                        <img src="{{ asset('images/icon/siswac/v.png') }}" alt="Centang benar"> Jawaban benar
                    </span>
                @elseif($isDipilih)
                    <span class="opsi-tag">← Jawabanmu</span>
                @endif
            </div>
            @endforeach
        </div>
        @endforeach
    </div>

    <a href="{{ route('siswa.challenge.index') }}" class="btn-back">
        ← Kembali ke Daftar Challenge
    </a>
</div>

@endsection