<?php

    namespace Modules\Lpj\Http\Requests;

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

    class StatusPermohonanRequest extends FormRequest
    {
        /**
         * Get the validation rules that apply to the request.
         */
        public function rules()
        : array
        {
            $rules = [
                'description' => 'nullable|max:255',
                'status'      => 'required|boolean',
                'slug'        => 'nullable|max:255',
            ];

            if ($this->method() == 'PUT') {
                $rules['name'] = 'required|max:50|unique:status_permohonan,name,' . $this->id;
            } else {
                $rules['name'] = 'required|max:50|unique:status_permohonan,name';
            }
            return $rules;
        }

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

        public function prepareForValidation()
        {
            return $this->merge([
                'status' => isset($this->status) ? 1 : 0,
                'slug'   => Str::slug($this->name),
            ]);
        }
    }
