<?php declare(strict_types=1);

namespace App\Http\Requests\Private\Roles;

use App\Contracts\FormRequestDto;
use App\Dto\Request\Private\Role\StoreUpdate;
use App\Rules\Permission;
use Illuminate\Foundation\Http\FormRequest;

final class StoreUpdateRequest extends FormRequest implements FormRequestDto
{
    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        $rules = [
            'name' => ['required', 'max:255'],
            'permissions' => ['array', new Permission()]
        ];
        if ($this->getMethod() === 'POST') {
            $rules['code'] = ['required', 'min:3', 'max:255', 'regex:/^[a-z0-9_-]+$/i'];
        }

        return $rules;
    }


    public function getDto(): StoreUpdate
    {
        return new StoreUpdate(
            name: $this->input('name'),
            code: $this->input('code', null),
            permissions: $this->input('permissions', [])
        );
    }
}
