<?php

namespace App\Http\Requests\Api\Vendor;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Vinkla\Hashids\Facades\Hashids;

class UpdateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        // Get the ID of the vendor being updated from the route model binding
         $convertedId = Hashids::decode($this->route('vendor'));
         $id = $convertedId[0];
        return [
            'agency_name' => 'required|string|max:255',
            'agency_code' => [
                'nullable',
                Rule::unique('vendors', 'agency_code')->ignore($id),
            ],
            'contact_person' => 'required|string|max:255',
            'email' => [
                'required',
                'email',
                Rule::unique('vendors', 'email')->ignore($id),
            ],
            'phone' => 'required|string|max:20',
            'alternate_phone' => 'nullable|string|max:20',
            'address' => 'nullable|string',
            'city' => 'nullable|string|max:100',
            'state' => 'nullable|string|max:100',
            'pincode' => 'nullable|string|max:10',
            'gst_number' => 'nullable|string|max:20',
            'pan_number' => 'nullable|string|max:20',
            
            // Categories must be an array, and each item must be a string
            'service_categories' => 'nullable|array',
            'service_categories.*' => 'string',
            
            'rating' => 'nullable',
            'status' => 'required|in:active,inactive',
        ];
    }
}