<x-student-layout>
    <x-slot name="header">
        <div>
            <a href="{{ route('student.self-practice.index') }}" class="inline-flex items-center gap-1 text-sm text-brand-green hover:underline mb-2">
                <svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/></svg>
                Back to Self Practice
            </a>
            <span class="admin-section-label">Self Practice</span>
            <h2 class="admin-page-title">{{ $subject->name }}</h2>
            <p class="admin-page-subtitle">Click a chapter to view topics and start practice</p>
        </div>
    </x-slot>

    <div class="admin-page">
        <div class="admin-card">
            <div class="admin-card-top"></div>
            <div class="admin-card-header">
                <div>
                    <h3 class="font-bold text-slate-900">Index — {{ $subject->name }}</h3>
                    <p class="text-xs text-slate-500 mt-0.5">{{ $chapters->count() }} chapter(s) — click a chapter to view topics</p>
                </div>
            </div>

            <div class="book-index-header-note">
                અનુક્રમણિકા / Index — chapter number with topic names (reference book style)
            </div>

            @if ($chapters->isEmpty())
                <div class="p-8 text-center text-sm text-slate-400">No chapters available for this subject yet.</div>
            @else
                <div class="book-index" x-data="{ openChapter: null }">
                    @foreach ($chapters as $chapter)
                        @php
                            $chapterNo = $chapter->sort_order > 0 ? $chapter->sort_order : $loop->iteration;
                            $topics = $chapter->topics;
                            $hasContent = (bool) $chapter->content;
                            $questionCount = $chapter->content?->questions?->count() ?? 0;
                            $displayTopics = $topics->isNotEmpty()
                                ? $topics
                                : ($hasContent ? collect([(object) ['id' => 0, 'name' => $chapter->content->title, 'sort_order' => 1]]) : collect());
                            $chapterKey = 'chapter-'.$chapter->id;
                            $canPractice = $questionCount > 0;
                        @endphp

                        <article class="book-index-chapter">
                            <button
                                type="button"
                                class="book-index-chapter-head"
                                @click="openChapter = openChapter === '{{ $chapterKey }}' ? null : '{{ $chapterKey }}'"
                                :aria-expanded="openChapter === '{{ $chapterKey }}'"
                            >
                                <span class="book-index-chapter-no">{{ $chapterNo }}</span>
                                <div class="book-index-chapter-title">
                                    <h4 class="book-index-chapter-name">{{ $chapter->name }}</h4>
                                    <p class="book-index-chapter-meta">
                                        @if ($displayTopics->isNotEmpty())
                                            {{ $displayTopics->count() }} {{ Str::plural('topic', $displayTopics->count()) }}
                                        @else
                                            No topics yet
                                        @endif
                                        @if ($canPractice)
                                            <span class="text-brand-green"> &middot; {{ $questionCount }} question{{ $questionCount !== 1 ? 's' : '' }}</span>
                                        @else
                                            <span class="text-slate-400"> &middot; No questions</span>
                                        @endif
                                    </p>
                                </div>
                                <span
                                    class="book-index-chapter-chevron"
                                    :class="{ 'book-index-chapter-chevron--open': openChapter === '{{ $chapterKey }}' }"
                                    aria-hidden="true"
                                >
                                    <svg class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/>
                                    </svg>
                                </span>
                            </button>

                            <div
                                x-show="openChapter === '{{ $chapterKey }}'"
                                x-cloak
                                class="book-index-chapter-body"
                            >
                                @if ($displayTopics->isNotEmpty())
                                    <div class="book-index-topics">
                                        @foreach ($displayTopics as $topic)
                                            @php
                                                $topicNo = $chapterNo.'.'.($topic->sort_order > 0 ? $topic->sort_order : $loop->iteration);
                                                $practiceUrl = route('student.self-practice.chapter', [$subject, $chapter]);
                                            @endphp
                                            @if ($canPractice)
                                                <a href="{{ $practiceUrl }}" class="book-index-topic book-index-topic--link group">
                                                    <span class="book-index-topic-no">{{ $topicNo }}</span>
                                                    <span class="book-index-topic-name group-hover:text-brand-green">{{ $topic->name }}</span>
                                                    <span class="book-index-topic-dots" aria-hidden="true"></span>
                                                    <span class="book-index-topic-page">{{ str_pad($loop->iteration, 2, '0', STR_PAD_LEFT) }}</span>
                                                    <span class="book-index-topic-arrow" aria-hidden="true">
                                                        <svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/></svg>
                                                    </span>
                                                </a>
                                            @else
                                                <div class="book-index-topic opacity-60">
                                                    <span class="book-index-topic-no">{{ $topicNo }}</span>
                                                    <span class="book-index-topic-name">{{ $topic->name }}</span>
                                                    <span class="book-index-topic-dots" aria-hidden="true"></span>
                                                    <span class="book-index-topic-page">{{ str_pad($loop->iteration, 2, '0', STR_PAD_LEFT) }}</span>
                                                </div>
                                            @endif
                                        @endforeach
                                    </div>
                                @else
                                    <p class="book-index-empty">Topics will appear here after admin uploads chapter material.</p>
                                @endif
                            </div>
                        </article>
                    @endforeach
                </div>
            @endif
        </div>
    </div>
</x-student-layout>
