<?php

namespace App\Http\Requests\Dashboard\UsefulInfoRequest;

use App\Models\UsefulInfo;
use Illuminate\Foundation\Http\FormRequest;

class Store extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name_uz' => 'required|string',
            'name_ru' => 'required|string',
            'image' => 'required|image',
            'position' => 'nullable|integer'
        ];
    }

    /**
     * @return array
     */
    public function getName(): array
    {
        return [
            'uz' => $this->get('name_uz'),
            'ru' => $this->get('name_ru')
        ];
    }

    public function getPosition(): int|null
    {
        if ($this->position) {
            return $this->get('position');
        } else {
            if (UsefulInfo::count() === 0) {
                return 0;
            }
            return UsefulInfo::max('position') + 1;
        }
    }
}
