<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdateCartRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            // Avoid min/max numeric validator path in production.
            'qty' => ['required', 'regex:/^[0-9]+$/'],
        ];
    }

    public function withValidator($validator): void
    {
        $validator->after(function ($validator) {
            $qty = (int) $this->input('qty', '0');

            if ($qty < 0 || $qty > 100) {
                $validator->errors()->add('qty', 'The qty field must be between 0 and 100.');
            }
        });
    }
}
