<?php

namespace App\Http\Controllers\Teacher;

use App\Http\Controllers\Concerns\RestrictsTeacherLessons;
use App\Http\Controllers\Controller;

class LessonController extends Controller
{
    use RestrictsTeacherLessons;

    public function index()
    {
        $lessons = $this->teacherLessons()
            ->withCount('questions')
            ->when(request()->q, function ($query) {
                $query->where('title', 'like', '%' . request()->q . '%');
            })
            ->latest()
            ->paginate(10);

        $lessons->appends(['q' => request()->q]);

        return inertia('Teacher/Lessons/Index', [
            'lessons' => $lessons,
        ]);
    }
}
