<?php

namespace App\Http\Requests\Loading;

use Illuminate\Foundation\Http\FormRequest;

class SaveRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            "ids" => ["required", "array", "min:1"],

            "ids.*" => [
                "required",
                "integer",
                "distinct",
                "exists:chick_loading_headers,id",
            ],

            "signature" => [
                "required",
                "file",
                "mimes:jpeg,png,jpg,webp",
                "max:10240",
            ],
        ];
    }

    /**
     * Custom Attribute Names
     */
    public function attributes(): array
    {
        return [
            "ids.*" => "loading id",
            "signature" => "signature file",
        ];
    }

    /**
     * Custom Error Messages
     */
    public function messages(): array
    {
        return [
            "ids.required" => "At least one loading id is required.",
            "ids.array" => "The loading ids must be an array.",
            "ids.min" => "At least one loading id must be provided.",

            "ids.*.integer" => "The :attribute must be a valid number.",
            "ids.*.distinct" => "The loading id :input is duplicated.",
            "ids.*.exists" => "The selected :attribute :input does not exist.",

            "signature.file" => "The signature must be a valid file.",
            "signature.mimes" => "The signature must be a file of type: jpeg, png, jpg, webp.",
            "signature.max" => "The signature must not be larger than 10MB.",
        ];
    }
}
