<?php

namespace App\Http\Requests\Dashboard\Service;

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

class Update extends FormRequest
{
    /**
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name_uz' => 'required|string',
            'name_ru' => 'required|string',
            'image' => 'nullable|image',
            'status' => 'required|in:published,new,soon',
            'position' => 'nullable|numeric',
            'is_power' => 'nullable|boolean',
        ];
    }

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

    public function getPosition()
    {
        $position = $this->position;
        if ($position == null) {
            $service = Service::orderBy('position', 'desc')->first();
            if ($service == null) {
                return 1;
            }
            return $service->position + 1;
        }

        return $position;
    }

    public function generateSlug()
    {
        $slug = str_slug($this->name_ru);
        $service = Service::where('type', $slug)->first();
        if ($service == null) {
            return $slug;
        }

        return $slug . '-' . time();
    }
}
