<?php

namespace Modules\Inventory\Http\Requests\Purchase;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Modules\Core\Enums\AmountTypeEnum;
use Modules\Core\Traits\FailedValidation;

class UpdatePurchaseInvoiceRequest extends FormRequest
{
    use FailedValidation;
    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'supplier_id' => [
                'required',
                'exists:suppliers,id'
            ],
            'invoice_date' => [
                'required',
                'date'
            ],
            'note' => [
                'nullable',
                'string'
            ],
            'items' => [
                'required',
                'array',
                'min:1'
            ],
            'items.*.product_id' => [
                'required',
                'exists:products,id'
            ],
            'items.*.unit_id' => [
                'required',
                'exists:units,id'
            ],
            'items.*.qty' => [
                'required',
                'numeric',
                'gt:0'
            ],
            'items.*.unit_price' => [
                'required',
                'numeric',
                'gte:0'
            ],
            'items.*.discount_type' => [
                'nullable',
                Rule::enum(AmountTypeEnum::class)
            ],
            'items.*.discount_amount' => [
                'nullable',
                'numeric',
                'gte:0'
            ],
            'items.*.batch_no' => [
                'nullable',
                'string'
            ],
            'items.*.expiry_date' => [
                'nullable',
                'date'
            ],
        ];
    }

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