<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Course;
use App\Models\Subject;
use App\Models\Level;

class CourseController extends Controller
{
    public function index()
    {
        $courses = Course::all();
        return view('admin.courses.index', compact('courses'));
    }	
	
	public function create()
    {
		$subjects = \App\Models\Subject::all();
		$levels = \App\Models\Level::all();

		return view('admin.courses.create', compact('subjects', 'levels'));
    }
	
    public function store(Request $request)
    {
		$request->validate([
			'subject_id'   => 'required|exists:subjects,id',
			'level_id'     => 'required|exists:levels,id',
			'lead_teacher' => 'required|string|max:255',
			'description'  => 'nullable|string',
		]);

		Course::create([
			'subject_id'   => $request->input('subject_id'),
			'level_id'     => $request->input('level_id'),
			'lead_teacher' => $request->input('lead_teacher'),
			'description'  => $request->input('description'),
		]);

        return redirect()
            ->route('admin.courses.index')
            ->with('success', 'Course added successfully!');
    }

	public function show(Course $course)
	{
		// Eager load related modules/sections/chapters so they’re available
		$course->load([
			'modules.lessonSections.chapters'
		]);

		return view('admin.courses.show', compact('course'));
	}	
}
