<?php

namespace App\Http\Requests\Order;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use App\Models\Vendor;
use App\Models\User;
use App\Models\VendorSubCategory;
use Illuminate\Support\Str;

class PlaceOrderRequest extends FormRequest
{
    public function authorize()
    {
        return true; // Allow this for now, can customize for auth guards if needed
    }

    public function rules(): array
    {
        return [
            'user_id' => ['required', 'uuid', 'exists:users,user_id'], 
            'vendor_id' => ['required', 'uuid', 'exists:vendors,track_id'], // Must be a valid vendor track_id

            'items' => ['required', 'array', 'min:1'],
            'items.*.sub_category_track_id' => ['required', 'string', 'exists:vendor_sub_categories,track_id'],
            'items.*.quantity' => ['required', 'integer', 'min:1'],
            'items.*.unit_price' => ['required', 'numeric', 'min:0'],

            'delivery_address' => ['nullable', 'string'],
            'notes' => ['nullable', 'string'],
        ];
    }

    public function messages()
    {
        return [
            'items.*.sub_category_track_id.exists' => 'One or more selected subcategories are invalid.',
            'vendor_id.exists' => 'Vendor does not exist.',
            'user_id.exists' => 'User not found.',
        ];
    }
}
