<?php

namespace App\Http\Requests\TicketType;

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

class UpdateTicketTypeRequest 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, ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [

            'code' => [
                'required',
                'string',
                'max:50',
                Rule::unique('ticket_types', 'code')
                    ->ignore($this->ticket_type->id)
            ],

            'name' => [
                'required',
                'string',
                'max:100'
            ],

            'need_approval' => [
                'nullable',
                'boolean'
            ],

            'sla_minutes' => [
                'nullable',
                'integer',
                'min:1'
            ],

            'require_photo' => [
                'nullable',
                'boolean'
            ],

            'require_material' => [
                'nullable',
                'boolean'
            ],

            'need_customer' => [
                'nullable',
                'boolean'
            ],

            'need_incident_type' => [
                'nullable',
                'boolean'
            ]
        ];
    }

    protected function prepareForValidation(): void
    {
        $this->merge([

            'need_approval' => filter_var(
                $this->need_approval,
                FILTER_VALIDATE_BOOLEAN
            ),

            'require_photo' => filter_var(
                $this->require_photo,
                FILTER_VALIDATE_BOOLEAN
            ),

            'require_material' => filter_var(
                $this->require_material,
                FILTER_VALIDATE_BOOLEAN
            ),

            'need_customer' => filter_var(
                $this->need_customer,
                FILTER_VALIDATE_BOOLEAN
            ),

            'need_incident_type' => filter_var(
                $this->need_incident_type,
                FILTER_VALIDATE_BOOLEAN
            )
        ]);
    }
}
