<?php

namespace App\Modules\Admin\Tenancy\Requests;

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

class UpdateModuleRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->can('update', $this->route('module')) ?? false;
    }

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

        return [
            'slug' => ['required', 'string', 'max:100', 'alpha_dash', Rule::unique('modules', 'slug')->ignore($moduleId)],
            'name' => ['required', 'string', 'max:150'],
            'description' => ['nullable', 'string', 'max:500'],
            'icon' => ['nullable', 'string', 'max:100'],
            'route_name' => ['nullable', 'string', 'max:150'],
            'category' => ['nullable', 'string', 'max:100'],
            'is_core' => ['sometimes', 'boolean'],
            'is_active' => ['sometimes', 'boolean'],
        ];
    }

    protected function prepareForValidation(): void
    {
        $this->merge([
            'is_active' => $this->boolean('is_active'),
            'is_core' => $this->boolean('is_core'),
        ]);
    }
}
