<?php

namespace App\Http\Requests\Dashboard;

use Illuminate\Foundation\Http\FormRequest;

class CategoryRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            "color" => "required|regex:/#[0-9A-Fa-f]{6}\b/",
        ];

        if($this->isMethod("patch")) {
            $rules["name"] = "required|min:2|unique:categories,id," . $this->route("id");
        } else {
            $rules["name"] = "required|min:2|unique:categories";
        }

        return $rules;
    }
}
