<?php

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;

class ProductCustomFieldOrderRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        $product = $this->route('product');

        return $this->user()->can('update', $product);
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'fields' => 'required|array',
            'fields.*.id' => 'required|integer|exists:product_custom_fields,id',
            'fields.*.order' => 'required|integer|min:0',
        ];
    }

    /**
     * Get custom error messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'fields.required' => __('admin/products.custom_fields.validation.fields.required'),
            'fields.array' => __('admin/products.custom_fields.validation.fields.array'),
            'fields.*.id.required' => __('admin/products.custom_fields.validation.field_id.required'),
            'fields.*.id.integer' => __('admin/products.custom_fields.validation.field_id.integer'),
            'fields.*.id.exists' => __('admin/products.custom_fields.validation.field_id.exists'),
            'fields.*.order.required' => __('admin/products.custom_fields.validation.field_order.required'),
            'fields.*.order.integer' => __('admin/products.custom_fields.validation.field_order.integer'),
            'fields.*.order.min' => __('admin/products.custom_fields.validation.field_order.min'),
        ];
    }
}
