<?php

namespace App\Http\Requests;

use App\Models\CoursewareCategory;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class CoursewareCategoryRequest extends FormRequest
{
    public function authorize(): bool
    {
        $category = $this->route('courseware_category');

        return $category
            ? $this->user()->can('update', $category)
            : $this->user()->can('create', CoursewareCategory::class);
    }

    public function rules(): array
    {
        $ignoreId = $this->route('courseware_category')?->id;

        return [
            'name' => ['required', 'string', 'max:255', Rule::unique('courseware_categories', 'name')->ignore($ignoreId)],
            'is_active' => ['nullable', 'boolean'],
        ];
    }
}
