<?php

namespace App\Http\Requests\Vendor;

use App\Http\Requests\BaseRequest;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdateVendorServiceRequest extends BaseRequest
{
    /**
     * 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
    {
        $serviceId = $this->route('service_id');
        
        return [
            'name' => 'required|string|max:255',
            'slug' => 'required|string|max:255|unique:vendor_services,slug,' . $serviceId,
            'description' => 'nullable|string',
            'base_price' => 'required|numeric|min:0',
            'category' => 'nullable|string|max:100',
            'delivery_time' => 'nullable|string|max:50',
            'requirements' => 'nullable|string|max:1000',
            'features' => 'nullable|array',
            'features.*.name' => 'required|string|max:255',
            'features.*.type' => 'required|in:text,number,boolean,choice',
            'features.*.options' => 'nullable|array',
            'features.*.price_adjustment' => 'nullable|numeric|min:-999999',
            'features.*.price_adjustment_type' => 'required_if:features.*.price_adjustment,!=,null|in:fixed,percentage',
            'features.*.is_required' => 'required|boolean',
            'features.*.sort_order' => 'required|integer|min:0',
            'items' => 'nullable|array',
            'items.*.name' => 'required|string|max:255',
            'items.*.price' => 'required|numeric|min:0',
            'items.*.unit' => 'required|string|max:50',
            'items.*.quantity' => 'required|integer|min:1',
            'items.*.min_quantity' => 'nullable|numeric|min:0',
            'items.*.max_quantity' => 'nullable|numeric|min:0',
            'items.*.is_available' => 'required|boolean',
            'items.*.sort_order' => 'required|integer|min:0',
            'image_url' => 'nullable|url|max:500',
            'status' => 'required|in:active,inactive,pending',
            'is_featured' => 'boolean',
            'sort_order' => 'integer|min:0',
        ];
    }

    /**
     * Get the custom error messages for the defined validation rules.
     *
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'name.required' => 'Service name is required.',
            'name.max' => 'Service name may not be greater than 255 characters.',
            'slug.required' => 'Service slug is required.',
            'slug.unique' => 'Service slug must be unique.',
            'description.required' => 'Service description is required.',
            'base_price.required' => 'Base price is required.',
            'base_price.numeric' => 'Base price must be a number.',
            'base_price.min' => 'Base price must be at least 0.',
            'features.required' => 'At least one feature is required.',
            'features.*.name.required' => 'Feature name is required.',
            'features.*.type.in' => 'Feature type must be text, number, boolean, or choice.',
            'items.*.name.required' => 'Item name is required.',
            'items.*.price.required' => 'Item price is required.',
            'items.*.price.numeric' => 'Item price must be a number.',
            'status.required' => 'Service status is required.',
            'status.in' => 'Service status must be active, inactive, or pending.',
        ];
    }
}
