<?php

namespace App\Http\Requests\Admin\Manage;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ClassEnrollmentRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()->can('create-course-class-enrollments');
    }

    public function rules(): array
    {
        $courseClass = $this->route('course_class');
        $departmentId = $courseClass->course->department_id;

        return [
            'student_ids' => ['required', 'array', 'min:1'],
            'student_ids.*' => [
                'integer',
                Rule::exists('students', 'id')->where('department_id', $departmentId),
                Rule::unique('class_enrollments', 'student_id')->where('course_class_id', $courseClass->id),
            ],
        ];
    }
}
