<?php

namespace App\Http\Requests\Stock;

use Illuminate\Foundation\Http\FormRequest;

class StockTransferOutRequest 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 [
            'destination_farm_id' => ['required', 'integer', 'exists:farms,id'],
            'transfer_date'       => ['required', 'date'],
            'remarks'             => ['nullable', 'string'],
            'items'               => ['required', 'array', 'min:1'],
            'items.*.item_id'     => ['required', 'integer', 'exists:items,id'],
            'items.*.quantity'    => ['required', 'numeric', 'min:0.0001'],
        ];
    }

    public function messages(): array
    {
        return [
            'destination_farm_id.required' => 'Destination farm is required.',
            'destination_farm_id.exists'   => 'Selected destination farm does not exist.',
            'transfer_date.required'       => 'Transfer date is required.',
            'items.required'               => 'At least one item is required.',
            'items.min'                    => 'At least one item is required.',
            'items.*.item_id.required'     => 'Item is required.',
            'items.*.item_id.exists'       => 'Selected item does not exist.',
            'items.*.quantity.required'    => 'Quantity is required.',
            'items.*.quantity.min'         => 'Quantity must be greater than zero.',
        ];
    }
}
