<?php

namespace App\Http\Requests;

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

class UpdateGarmentTypeRequest extends FormRequest
{
    public function authorize(): bool
    {
        /** @var GarmentType $garmentType */
        $garmentType = $this->route('garmentType');

        return $this->user()?->can('update', $garmentType) ?? false;
    }

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

        return [
            // code estable: se permite enviar solo si no cambia.
            'code' => ['sometimes', 'string', 'max:64', Rule::in([$garmentType->code])],
            'name' => ['sometimes', 'string', 'max:255'],
            'sort_order' => ['sometimes', 'integer', 'min:0'],
            'is_active' => ['sometimes', 'boolean'],
            'default_load_type_id' => ['nullable', 'integer', 'exists:load_types,id'],
        ];
    }
}
