<x-app-layout>
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8 space-y-10">

        {{-- Greeting --}}
        <div>
            <h2 class="text-2xl font-semibold text-gray-800">
                Welcome back, {{ auth()->user()->name }}!
            </h2>
        </div>
		
        {{-- Continue Learning (enrolled & in-progress) --}}
        <div class="bg-white rounded-xl shadow p-6">
            <h3 class="text-lg font-medium text-gray-900 border-b pb-3 mb-4">Continue Learning</h3>

            @if($inProgressCourses->isNotEmpty())
                <div class="grid sm:grid-cols-2 lg:grid-cols-3 gap-6">
                    @foreach($inProgressCourses as $course)
                        @php
                            // Find the most recent lesson viewed in this course
                            $lastLesson = $lastLessonsByCourse
                                ->firstWhere('chapter.lessonSection.module.course.id', $course->id);
                        @endphp

                        <div class="border rounded-lg p-4 bg-gray-50 hover:bg-gray-100 transition">
                            <h4 class="text-indigo-600 font-semibold text-lg">{{ $course->name }}</h4>
                            <p class="text-gray-600 text-sm">
                                {{ $course->subject->name }} ({{ $course->level->name }})
                            </p>

                            @if($lastLesson)
                                <p class="text-gray-700 text-sm mt-2">Last lesson: {{ $lastLesson->title }}</p>
                                <a href="{{ route('student.course.lesson', [
                                    'course' => $course->slug,
                                    'lesson' => $lastLesson->slug
                                ]) }}"
                                   class="inline-block mt-3 px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 text-sm">
                                    ▶ Resume Lesson
                                </a>
                            @else
                                @php
                                    $firstLesson = $course->modules
                                        ->flatMap(fn($m) => $m->lessonSections)
                                        ->flatMap(fn($s) => $s->chapters)
                                        ->flatMap(fn($c) => $c->lessons)
                                        ->sortBy('order')
                                        ->first();
                                @endphp

                                @if($firstLesson)
                                    <a href="{{ route('student.course.lesson', [
                                        'course' => $course->slug,
                                        'lesson' => $firstLesson->slug
                                    ]) }}"
                                       class="inline-block mt-3 px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 text-sm">
                                        ▶ Go to Course
                                    </a>
                                @else
                                    <span class="text-gray-500 text-sm mt-3 inline-block">No lessons yet</span>
                                @endif
                            @endif
                        </div>
                    @endforeach
                </div>
            @else
                <p class="text-gray-600">You haven’t started any courses yet. Pick one below!</p>
            @endif
        </div>

        {{-- Enrolled but not yet started --}}
        <div class="bg-white rounded-xl shadow p-6">
            <h3 class="text-lg font-medium text-gray-900 border-b pb-3 mb-4">Courses You’ve Enrolled In (Not Started)</h3>

            @if($notStartedCourses->isNotEmpty())
                <div class="grid sm:grid-cols-2 lg:grid-cols-3 gap-6">
                    @foreach($notStartedCourses as $course)
                        <div class="border rounded-lg p-4 bg-white">
                            <h4 class="font-semibold text-gray-900">{{ $course->name }}</h4>
                            <p class="text-gray-600 text-sm">
                                {{ $course->subject->name }} ({{ $course->level->name }})
                            </p>

                            @php
                                $firstLesson = $course->modules
                                    ->flatMap(fn($m) => $m->lessonSections)
                                    ->flatMap(fn($s) => $s->chapters)
                                    ->flatMap(fn($c) => $c->lessons)
                                    ->sortBy('order')
                                    ->first();
                            @endphp

                            @if($firstLesson)
                                <a href="{{ route('student.course.lesson', [
                                    'course' => $course->slug,
                                    'lesson' => $firstLesson->slug
                                ]) }}"
                                   class="inline-block mt-3 px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 text-sm">
                                    ▶ Start Course
                                </a>
                            @else
                                <span class="text-gray-500 text-sm">No lessons yet</span>
                            @endif
                        </div>
                    @endforeach
                </div>
            @else
                <p class="text-gray-600">No enrolled courses awaiting a start right now.</p>
            @endif
        </div>

        {{-- Available to enrol (not enrolled) --}}
        <div class="bg-white rounded-xl shadow p-6">
            <h3 class="text-lg font-medium text-gray-900 border-b pb-3 mb-4">Available Courses</h3>

            @if($availableCourses->isNotEmpty())
                <div class="grid sm:grid-cols-2 lg:grid-cols-3 gap-6">
                    @foreach($availableCourses as $course)
                        <div class="border rounded-lg p-4 bg-white">
                            <h4 class="font-semibold text-gray-900">{{ $course->name }}</h4>
                            <p class="text-gray-600 text-sm">
                                {{ $course->subject->name }} ({{ $course->level->name }})
                            </p>
                            {{-- If you have a course details page, update this route accordingly --}}
							<a href="#"
							   class="inline-block mt-3 px-4 py-2 bg-gray-900 text-white rounded-lg opacity-70 cursor-not-allowed text-sm">
								Enrollment coming soon
							</a>
                        </div>
                    @endforeach
                </div>
            @else
                <p class="text-gray-600">You’re enrolled in all available courses. 🎉</p>
            @endif
        </div>

    </div>
</x-app-layout>