<?php

namespace App\Http\Requests;

use App\Traits\HasResponse;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

class StockProductRequest extends FormRequest
{
    use HasResponse;

    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'idproduct'             => ['required', 'numeric', 'validate_ids_exist:Product'],
            'idwarehouse'           => ['required', 'numeric', 'validate_ids_exist:Store'],
            'idcompany'             => ['required', 'numeric', 'validate_ids_exist:Company'],
            'quantity'              => ['required', 'numeric'],
            'type_movement'         => ['required', 'integer', 'min:1', 'max:2'],
            'isAdjustment'          => ['nullable', 'integer', 'min:0', 'max:1']
        ];
    }

    public function failedValidation(Validator $validator)
    {
        throw new HttpResponseException($this->errorResponse('Formato inválido.', 400, $validator->errors()));
    }
}
