<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Course;
use App\Models\LessonSection;

class LessonSectionController extends Controller
{
	
	    public function show(Course $course, LessonSection $section)
    {
        // Optional safety: ensure this section actually belongs to this course
        // (assuming LessonSection -> module -> course)
        if ($section->module && $section->module->course_id !== $course->id) {
            abort(404);
        }

        // Eager load chapters for this section
        $section->load(['chapters' => function ($q) {
            $q->orderBy('order');
        }]);

        return view('admin.sections.show', [
            'course'  => $course,
            'section' => $section,
        ]);
    }
	
    public function index(Course $course)
    {
        $sections = $course->lessonSections()->orderBy('order')->get();

        return view('admin.lesson_sections.index', compact('course', 'sections'));
    }

	public function store(Request $request, Course $course)
	{
		$request->validate([
			'module_id' => 'required|exists:modules,id',
			'title'     => 'required|string|max:255',
			'year'      => 'nullable|integer',
		]);

		$module = \App\Models\Module::findOrFail($request->module_id);

		// Safety check: ensure module belongs to this course
		if ($module->course_id !== $course->id) {
			abort(404);
		}

		LessonSection::create([
			'module_id' => $module->id,
			'title'     => $request->title,
			'year'      => $request->year,
			'order'     => $module->lessonSections()->count() + 1,
		]);

		return back()->with('success', 'Lesson section added.');
	}

    public function update(Request $request, Course $course, LessonSection $lessonSection)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'year'  => 'nullable|integer',
        ]);

        $lessonSection->update([
            'title' => $request->title,
            'year'  => $request->year,
        ]);

        return back()->with('success', 'Lesson section updated.');
    }

	public function destroy(Course $course, LessonSection $section)
	{
		// Ensure the section belongs to the course via the module
		if ($section->module->course_id !== $course->id) {
			abort(404);
		}

		// Delete all chapters and their lessons/videos
		foreach ($section->chapters as $chapter) {
			// delete lessons inside the chapter
			foreach ($chapter->lessons as $lesson) {
				$lesson->videos()->delete();
			}

			$chapter->lessons()->delete();
			$chapter->delete();
		}

		// Delete the section itself
		$section->delete();

		return redirect()
			->route('admin.courses.show', $course->slug)
			->with('success', 'Lesson section deleted.');
	}

    public function reorder(Request $request, Course $course)
    {
        $ids = json_decode($request->order, true);

        foreach ($ids as $i => $id) {
            LessonSection::where('id', $id)->update(['order' => $i + 1]);
        }

        return back()->with('success', 'Lesson section order updated.');
    }
}
