<?php

namespace App\Http\Requests\Admin\ServiceItem;

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

class UpdateServiceItemRequest extends BaseRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        $serviceItemId = $this->route('service_item_id');
        
        return [
            'service_category_id' => 'required|uuid|exists:service_categories,id',
            'name' => 'required|string|max:255|unique:service_items,name,' . $serviceItemId . ',id',
            'slug' => 'nullable|string|max:255|unique:service_items,slug,' . $serviceItemId . ',id',
            'description' => 'nullable|string|max:1000',
            'image' => 'nullable|url|max:500',
            'base_price' => 'required|numeric|min:0',
            'unit' => 'required|string|max:50',
            'is_active' => 'nullable|boolean',
            'sort_order' => 'nullable|integer|min:0',
            'features' => 'nullable|array',
            'features.*.feature_type_id' => 'required|uuid|exists:feature_types,id',
            'features.*.is_required' => 'nullable|boolean',
            'features.*.sort_order' => 'nullable|integer|min:0',
            'features.*.options' => 'nullable|array',
            'features.*.options.*.feature_option_id' => 'required|uuid|exists:feature_options,id',
            'features.*.options.*.is_available' => 'nullable|boolean',
            'features.*.options.*.price_adjustment' => 'nullable|numeric|min:0',
            'features.*.options.*.price_adjustment_type' => 'nullable|in:fixed,percentage',
            'features.*.options.*.sort_order' => 'nullable|integer|min:0',
        ];
    }

    public function messages()
    {
        return [
            'service_category_id.required' => 'Service category is required.',
            'service_category_id.exists' => 'Selected service category does not exist.',
            'name.required' => 'Service item name is required.',
            'name.unique' => 'This service item name already exists.',
            'slug.unique' => 'This slug already exists.',
            'base_price.required' => 'Base price is required.',
            'base_price.numeric' => 'Base price must be a number.',
            'unit.required' => 'Unit is required.',
            'features.*.feature_type_id.required' => 'Feature type is required.',
            'features.*.feature_type_id.exists' => 'Selected feature type does not exist.',
            'features.*.options.*.feature_option_id.required' => 'Feature option is required.',
            'features.*.options.*.feature_option_id.exists' => 'Selected feature option does not exist.',
            'features.*.options.*.price_adjustment_type.in' => 'Price adjustment type must be either fixed or percentage.',
        ];
    }
}
