<?php

namespace App\Http\Requests\Admin\Master;

use App\Enums\DegreeLevel;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    public function rules(): array
    {
        return [
            'code' => [
                'required',
                'string',
                'max:10',
                Rule::unique('departments')->ignore($this->route('department')),
            ],
            'name' => ['required', 'string', 'max:100'],
            'degree_level' => ['nullable', 'string', Rule::enum(DegreeLevel::class)],
            'lecturer_id' => [
                'nullable',
                Rule::requiredIf($this->route('department') !== null),
                'integer',
                Rule::exists('lecturer_department', 'lecturer_id')
                    ->where('department_id', $this->route('department')?->id ?? 0),
            ],
            'leadership_started_at' => ['nullable', 'date', 'required_with:lecturer_id'],
        ];
    }
}
