<?php

namespace App\Http\Requests\PurchaseOrder;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdatePurchaseOrderRequest extends FormRequest
{
	public function authorize(): bool
	{
		return true;
	}

	// public function rules(): array
	// {
	// 	$current = $this->route('purchase_order');
	// 	$currentId = $current ? $current->id : null;

	// 	return [
	// 		'po_number' => [
	// 			'required', 'string', 'max:255',
	// 			Rule::unique('purchase_orders', 'po_number')->ignore($currentId),
	// 		],
	// 		'purchase_request_id' => ['required', 'integer', 'exists:purchase_requests,id'],
	// 		'vendor_id' => ['nullable', 'integer', 'exists:contacts,id'],
	// 		'order_date' => ['required', 'date'],
	// 		'expected_delivery_date' => ['nullable', 'date', 'after_or_equal:order_date'],
	// 		'remarks' => ['nullable', 'string', 'max:1000'],
	// 		'status' => ['required', Rule::in(['draft', 'confirmed', 'partially_received', 'completed', 'cancelled'])],
	// 		'payment_status' => ['required', Rule::in(['unpaid', 'partially_paid', 'paid'])],
	// 		'total_amount' => ['nullable', 'numeric', 'min:0'],
	// 		'items' => ['required', 'array', 'min:1'],
	// 		'items.*.item_id' => ['required', 'integer', 'exists:accounts,id', 'distinct'],
	// 		'items.*.purchase_request_item_id' => ['nullable', 'integer', 'exists:purchase_request_items,id'],
	// 		'items.*.uom_id' => ['nullable', 'integer', 'exists:uoms,id'],
	// 		'items.*.unit_code' => ['nullable', 'string', 'max:50'],
	// 		'items.*.quantity' => ['required', 'numeric', 'min:0.000001'],
	// 		'items.*.unit_price' => ['required', 'numeric', 'min:0.0001'],
	// 		'items.*.total_price' => ['nullable', 'numeric', 'min:0'],
	// 	];
	// }

	public function rules(): array
    {
        $current   = $this->route('purchase_order');
        $currentId = $current ? $current->id : null;

        return [
            'po_number'                        => ['required', 'string', 'max:255', Rule::unique('purchase_orders', 'po_number')->ignore($currentId)],
            'purchase_request_id'              => ['sometimes', 'nullable', 'integer', 'exists:purchase_requests,id'],
            'vendor_id'                        => ['sometimes', 'nullable', 'integer', 'exists:accounts,id'],
            'expected_date'                    => ['sometimes', 'nullable', 'date'],
            'remarks'                          => ['nullable', 'string', 'max:1000'],
            'total_amount'                     => ['nullable', 'numeric', 'min:0'],
            'items'                            => ['required', 'array', 'min:1'],
            'items.*.item_id'                  => ['required', 'integer', 'exists:accounts,id'],
            'items.*.purchase_request_item_id' => ['nullable', 'integer', 'exists:purchase_request_items,id'],
            'items.*.uom_id'                   => ['nullable', 'integer', 'exists:uoms,id'],
            'items.*.unit_code'                => ['nullable', 'string', 'max:50'],
            'items.*.quantity'                 => ['required', 'numeric', 'min:0.000001'],
            'items.*.unit_price'               => ['required', 'numeric', 'min:0.0001'],
            'items.*.total_price'              => ['nullable', 'numeric', 'min:0'],
        ];
    }

	public function messages(): array
	{
		return [
			'purchase_request_id.required' => 'Please select a Purchase Request.',
			'purchase_request_id.exists' => 'The selected Purchase Request is invalid.',
			'vendor_id.exists' => 'The selected vendor is invalid.',
			'order_date.required' => 'Order Date is required.',
			'expected_delivery_date.after_or_equal' => 'Expected Delivery Date must be on or after the Order Date.',
			'status.required' => 'Status is required.',
			'status.in' => 'The selected status is invalid.',
			'payment_status.required' => 'Payment Status is required.',
			'payment_status.in' => 'The selected payment status is invalid.',
			'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' => 'The selected item is invalid.',
			'items.*.item_id.distinct' => 'Duplicate items are not allowed.',
			'items.*.quantity.required' => 'Order quantity is required.',
			'items.*.quantity.min' => 'Order quantity must be greater than 0.',
			'items.*.unit_price.required' => 'Unit price is required.',
			'items.*.unit_price.min' => 'Unit price must be greater than 0.',
		];
	}
}
