<?php

namespace App\Http\Requests\PurchaseRequest;

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

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

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

		return [
			'pr_number' => [
				'required', 'string', 'max:255',
				Rule::unique('purchase_requests', 'pr_number')->ignore($currentId),
			],
			'title' => ['nullable', 'string', 'max:255'],
			'description' => ['nullable', 'string'],
			'required_date' => ['nullable', 'date'],
			'expected_date' => ['required', 'date'],
			'priority' => ['nullable', 'string', 'in:low,medium,high,urgent'],
			'unit_code' => ['nullable', 'string', 'max:50'],
			'remarks' => ['nullable', 'string'],
			'status' => ['sometimes', Rule::in(['draft', 'submitted', 'approved', 'rejected', 'converted_to_po'])],
			'items' => ['required', 'array', 'min:1'],
			'items.*.item_id' => ['required', 'integer', 'exists:accounts,id', 'distinct'],
			'items.*.item_variation_id' => ['nullable', 'integer', 'exists:item_variations,id'],
			'items.*.quantity' => ['required', 'numeric', 'min:0.000001'],
			'items.*.unit_code' => ['nullable', 'string', 'max:50'],
		];
	}
}
