<?php

namespace App\Http\Requests\Category;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;

class CategoryStoreRequest extends FormRequest
{
    /**
     * @return bool
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * @return void
     */
    protected function prepareForValidation(): void
    {
        $this->merge([
            'slug' => Str::slug($this->input('category')),
            'parent_id' => $this->filled('parent_id') ? $this->input('parent_id') : null,
            'show_in_main' => $this->has('show_in_main')
        ]);
    }

    /**
     * @return array
     */
    public function rules(): array
    {
        return [
            'parent_id' => ['nullable','numeric'],
            'slug' => ['nullable', 'string', 'max:200'],
            'category' => ['nullable','string','max:200','nullable'],
            'show_in_main' => ['sometimes','boolean']
        ];
    }
}
