<?php

namespace App\Http\Requests\Vendor;

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

class CreateVendorServiceRequest 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
    {
        return [
            'service_item_id' => 'required|uuid|exists:service_items,id',
            'name' => 'required|string|max:255',
            'slug' => 'nullable|string|max:255|unique:vendor_services,slug',
            'description' => 'nullable|string|max:1000',
            'price' => 'required|numeric|min:0',
            'min_order_quantity' => 'nullable|numeric|min:0',
            'max_order_quantity' => 'nullable|numeric|min:0',
            'production_days' => 'nullable|integer|min:1',
            'notes' => 'nullable|string|max:1000',
            'is_active' => 'boolean',
            'features' => 'nullable|array',
            'features.*.feature_option_id' => 'required|uuid|exists:feature_options,id',
            'features.*.price_adjustment' => 'nullable|numeric|min:-999999',
            'features.*.price_adjustment_type' => 'required_if:features.*.price_adjustment,!=,null|in:fixed,percentage',
            'features.*.is_available' => 'required|boolean',
            'features.*.production_days_adjustment' => 'nullable|integer|min:0',
            'features.*.notes' => 'nullable|string|max:500',
        ];
    }

    /**
     * Get the custom error messages for the defined validation rules.
     *
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'service_item_id.required' => 'Service item is required.',
            'service_item_id.exists' => 'Selected service item does not exist.',
            'name.required' => 'Service name is required.',
            'name.max' => 'Service name may not be greater than 255 characters.',
            'slug.unique' => 'Service slug must be unique.',
            'price.required' => 'Price is required.',
            'price.numeric' => 'Price must be a number.',
            'price.min' => 'Price must be at least 0.',
            'production_days.min' => 'Production days must be at least 1.',
            'features.*.feature_option_id.required' => 'Feature option is required.',
            'features.*.feature_option_id.exists' => 'Selected feature option does not exist.',
            'features.*.price_adjustment.numeric' => 'Price adjustment must be a number.',
            'features.*.price_adjustment_type.in' => 'Price adjustment type must be fixed or percentage.',
            'features.*.is_available.boolean' => 'Feature availability must be true or false.',
        ];
    }

    /**
     * Configure the validator instance to enforce Printing & Branding category.
     */
    public function withValidator($validator)
    {
        $validator->after(function ($validator) {
            $serviceItemId = $this->input('service_item_id');

            if ($serviceItemId) {
                $serviceItem = \App\Models\ServiceItem::with('serviceCategory')
                    ->find($serviceItemId);

                if (!$serviceItem || $serviceItem->serviceCategory->name !== 'Printing & Branding') {
                    $validator->errors()->add('service_item_id', 'Only Printing & Branding services are allowed.');
                }
            }
        });
    }
}
