<?php

namespace App\Http\Requests\PurchaseOrders;

use App\Http\Requests\ApiFormRequest;
use App\Models\PurchaseOrder;

class PurchaseOrderStoreRequest extends ApiFormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'po_number' => ['required', 'string', 'max:60'],
            'supplier_id' => ['required', 'integer', 'min:1', 'exists:suppliers,id'],
            'status' => ['nullable', 'string', 'in:' . implode(',', PurchaseOrder::allowedStatuses())],
            'order_date' => ['nullable', 'date'],
            'expected_date' => ['nullable', 'date', 'after_or_equal:order_date'],
            'notes' => ['nullable', 'string', 'max:5000'],
            'items' => ['required', 'array', 'min:1'],
            'items.*.supply_id' => ['required', 'integer', 'min:1', 'exists:supplies,id'],
            'items.*.description' => ['nullable', 'string', 'max:1000'],
            'items.*.quantity' => ['required', 'integer', 'min:1'],
            'items.*.unit_cost' => ['required', 'numeric', 'min:0'],
        ];
    }
}
