<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ChatCategoryRequest 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
    {
        return [
            'name' => ['required', 'string', 'max:191'],
            'description' => ['nullable', 'string', 'max:1000'],
            'icon' => ['nullable', 'string', 'max:50', Rule::in($this->getAvailableIcons())],
            'color' => ['nullable', 'string', 'max:20', Rule::in($this->getAvailableColors())],
            'is_active' => ['nullable', 'boolean'],
            'sort_order' => ['nullable', 'integer', 'min:0', 'max:999'],
        ];
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'name.required' => 'Please enter a category name.',
            'name.max' => 'The category name cannot exceed 191 characters.',
            'description.max' => 'The description is too long. Maximum is 1000 characters.',
            'icon.in' => 'Please select a valid icon.',
            'color.in' => 'Please select a valid color.',
            'sort_order.integer' => 'Sort order must be a number.',
            'sort_order.min' => 'Sort order must be at least 0.',
            'sort_order.max' => 'Sort order cannot exceed 999.',
        ];
    }

    /**
     * Prepare the data for validation.
     */
    protected function prepareForValidation(): void
    {
        // XSS prevention
        if ($this->has('name')) {
            $this->merge([
                'name' => strip_tags($this->name),
            ]);
        }

        if ($this->has('description')) {
            $this->merge([
                'description' => strip_tags($this->description),
            ]);
        }

        // Set defaults
        $this->merge([
            'is_active' => $this->boolean('is_active'),
            'sort_order' => $this->input('sort_order', 0),
        ]);
    }

    /**
     * Get available icon options.
     */
    protected function getAvailableIcons(): array
    {
        return [
            'comments',
            'question-circle',
            'headset',
            'tools',
            'shopping-cart',
            'user-tie',
            'book',
            'life-ring',
            'envelope',
            'phone',
        ];
    }

    /**
     * Get available color options.
     */
    protected function getAvailableColors(): array
    {
        return [
            'green',
            'blue',
            'purple',
            'orange',
            'red',
            'teal',
            'indigo',
            'pink',
            'gray',
            'yellow',
        ];
    }

    /**
     * Get validated data with defaults.
     */
    public function validatedWithDefaults(): array
    {
        $data = $this->validated();

        // Set default values
        $data['icon'] = $data['icon'] ?? 'comments';
        $data['color'] = $data['color'] ?? 'green';
        $data['is_active'] = $data['is_active'] ?? true;
        $data['sort_order'] = $data['sort_order'] ?? 0;

        return $data;
    }
}
