<?php

namespace App\Http\Requests\Admin\Manage;

use App\Enums\ClassMethod;
use App\Models\Course;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class CourseClassRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()->can($this->isMethod('post') ? 'create-course-classes' : 'update-course-classes');
    }

    public function rules(): array
    {
        $departmentId = Course::find($this->input('course_id'))?->department_id;

        return [
            'course_id' => ['required', 'integer', Rule::exists('courses', 'id')],
            'lecturer_id' => [
                'required',
                'integer',
                Rule::exists('lecturer_department', 'lecturer_id')->where('department_id', $departmentId),
            ],
            'academic_term_id' => ['required', 'integer', Rule::exists('academic_terms', 'id')],
            'method' => ['nullable', 'string', Rule::in(ClassMethod::values())],
        ];
    }
}
