<?php

namespace Modules\Lpj\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class IjinUsahaRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        $rules = [
            'name'   => 'required|string|not_regex:/^\d+$/|max:255'
        ];

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

        return $rules;
    }

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

    public function messages(): array
    {
        return [
            'code.required' => 'Kode Ijin Usaha harus diisi!',
            'code.max' => 'Kode Ijin Usaha maksimal 255 huruf!',
            'code.unique' => 'Kode Ijin Usaha tidak boleh sama!',
            'name.required' => 'Nama Ijin Usaha harus diisi!',
            'name.not_regex' => 'Nama Ijin Usaha harus berupa huruf!',
            'name.max'   => 'Nama Ijin Usaha maksimal 255 huruf!'
        ];
    }
}
