<?php

namespace App\Http\Requests;

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

class UpdateClienteCategoriaRequest extends FormRequest
{
    public function authorize(): bool
    {
        $categoria = $this->route('clienteCategoria');

        return $categoria instanceof ClienteCategoria
            && ($this->user()?->can('update', $categoria) ?? false);
    }

    public function rules(): array
    {
        /** @var ClienteCategoria $categoria */
        $categoria = $this->route('clienteCategoria');

        return [
            'code' => ['sometimes', 'string', 'max:64', 'alpha_dash', Rule::unique('cliente_categorias', 'code')->ignore($categoria->id)],
            'name' => ['sometimes', 'required', 'string', 'max:255'],
            'color' => ['nullable', 'string', 'regex:/^#[0-9A-Fa-f]{6}$/'],
            'sort_order' => ['nullable', 'integer', 'min:0'],
            'is_active' => ['sometimes', 'boolean'],
        ];
    }
}
