<?php

namespace Modules\Lpj\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class KJPPRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        $rules = [
            'name' => 'required|string|not_regex:/^\d+$/|max:255',
            'jenis_kantor' => 'required',
            'nomor_ijin_usaha' => 'required',
            'province_code' => 'required',
            'city_code' => 'nullable',
            'district_code' => 'nullable',
            'village_code' => 'nullable',
            'address' => 'required',
            'postal_code' => 'nullable|numeric',
            'nomor_telepon_kantor' => 'nullable|numeric|digits_between:8,15',
            'email_kantor' => 'required|email',
            'detail_email_kantor' => 'nullable',
            'detail_email_kantor.email_kantor.*' => 'email',
            'nama_pimpinan' => 'required|string|not_regex:/^\d+$/|max:255',
            'detail_nama_pimpinan' => 'nullable',
            'detail_nama_pimpinan.nama_pimpinan.*' => 'string|not_regex:/^\d+$/|max:255',
            'nomor_hp_pimpinan' => 'required|numeric|digits_between:10,15',
            'detail_nomor_hp_pimpinan' => 'nullable',
            'detail_nomor_hp_pimpinan.nomor_hp_pimpinan.*' => 'numeric|digits_between:10,15',
            'nama_pic_reviewer' => 'nullable|string|not_regex:/^\d+$/|max:255',
            'detail_nama_pic_reviewer' => 'nullable',
            'detail_nama_pic_reviewer.nama_pic_reviewer.*' => 'string|not_regex:/^\d+$/|max:255',
            'nomor_hp_pic_reviewer' => 'nullable|numeric|digits_between:10,15',
            'detail_nomor_hp_pic_reviewer' => 'nullable',
            'detail_nomor_hp_pic_reviewer.nomor_hp_pic_reviewer.*' => 'numeric|digits_between:10,15',
            'nama_pic_admin' => 'nullable|string|not_regex:/^\d+$/|max:255',
            'detail_nama_pic_admin' => 'nullable',
            'detail_nama_pic_admin.nama_pic_admin.*' => 'string|not_regex:/^\d+$/|max:255',
            'nomor_hp_pic_admin' => 'nullable|numeric|digits_between:10,15',
            'detail_nomor_hp_pic_admin' => 'nullable',
            'detail_nomor_hp_pic_admin.nomor_hp_pic_admin.*' => 'numeric|digits_between:10,15',
            'nama_pic_marketing' => 'nullable|string|not_regex:/^\d+$/|max:255',
            'detail_nama_pic_marketing' => 'nullable',
            'detail_nama_pic_marketing.nama_pic_marketing.*' => 'string|not_regex:/^\d+$/|max:255',
            'nomor_hp_pic_marketing' => 'nullable|numeric|digits_between:10,15',
            'detail_nomor_hp_pic_marketing' => 'nullable',
            'detail_nomor_hp_pic_marketing.nomor_hp_pic_marketing.*' => 'numeric|digits_between:10,15',
            'ijin_usaha_id' => 'required|array',
            'ijin_usaha_id.*' => 'exists:ijin_usaha,code',
            'jenis_aset_id' => 'nullable|array',
            'jenis_aset_id.*' => 'exists:jenis_jaminan,code',
            'attachment' => 'nullable|mimes:pdf|max:1024'
        ];

        if ($this->method() == 'PUT') {
            $rules['code'] = 'required|max:50|unique:kjpp,code,' . $this->id;
        } else {
            $rules['code'] = 'required|max:50|unique:kjpp,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 KJPP Wajib diisi!',
            'code.max' => 'Kode KJPP maksimal 255 huruf!',
            'code.unique' => 'Kode KJPP tidak boleh sama!',
            'name.required' => 'Nama KJPP Wajib diisi!',
            'name.not_regex' => 'Nama KJPP harus berupa huruf!',
            'name.max'   => 'Nama KJPP maksimal 255 huruf!',
            'jenis_kantor.required' => 'Jenis Kantor Wajib diisi!',
            'nomor_ijin_usaha.required' => 'Nomor Ijin Usaha Wajib diisi!',
            'nomor_ijin_usaha.max'   => 'Nomor Ijin Usaha maksimal 255 huruf!',
            'province_code.required' => 'Provinsi Wajib diisi!',
            'postal_code.numeric' => 'Kode Pos harus berupa angka!',
            'address.required' => 'Alamat Kantor Wajib diisi!',
            'nomor_telepon_kantor.numeric' => 'Nomor Telepon Kantor harus berupa angka!',
            'nomor_telepon_kantor.digits_between' => 'Nomor Telepon Kantor minimum 8 digit dan maksimum 15 digit!',
            'email_kantor.required' => 'Email Kantor Wajib diisi!',
            'email_kantor.email' => 'Email Kantor tidak valid!',
            'detail_email_kantor.email_kantor.*.email' => 'Email Kantor tidak valid!',
            'nama_pimpinan.required' => 'Nama Pimpinan Wajib diisi!',
            'nama_pimpinan.not_regex' => 'Nama Pimpinan harus berupa huruf!',
            'detail_nama_pimpinan.nama_pimpinan.*.not_regex' => 'Nama Pimpinan harus berupa huruf!',
            'nomor_hp_pimpinan.required' => 'Nomor HP Pimpinan Wajib diisi!',
            'nomor_hp_pimpinan.numeric' => 'Nomor HP Pimpinan harus berupa angka!',
            'nomor_hp_pimpinan.digits_between' => 'Nomor HP Pimpinan minimum 10 digit dan maksimum 15 digit!',
            'detail_nomor_hp_pimpinan.nomor_hp_pimpinan.*.numeric' => 'Nomor HP Pimpinan harus berupa angka!',
            'detail_nomor_hp_pimpinan.nomor_hp_pimpinan.*.digits_between' => 'Nomor HP Pimpinan minimum 10 digit dan maksimum 15 digit!',
            'nama_pic_reviewer.not_regex' => 'Nama PIC Reviewer harus berupa huruf!',
            'detail_nama_pic_reviewer.nama_pic_reviewer.*.not_regex' => 'Nama PIC Reviewer harus berupa huruf!',
            'nomor_hp_pic_reviewer.numeric' => 'Nomor HP PIC Reviewer harus berupa angka!',
            'nomor_hp_pic_reviewer.digits_between' => 'Nomor HP PIC Reviewer minimum 10 digit dan maksimum 15 digit!',
            'detail_nomor_hp_pic_reviewer.nomor_hp_pic_reviewer.*.numeric' => 'Nomor HP PIC Reviewer harus berupa angka!',
            'detail_nomor_hp_pic_reviewer.nomor_hp_pic_reviewer.*.digits_between' => 'Nomor HP PIC Reviewer minimum 10 digit dan maksimum 15 digit!',
            'nama_pic_admin.not_regex' => 'Nama PIC Admin harus berupa huruf!',
            'detail_nama_pic_admin.nama_pic_admin.*.not_regex' => 'Nama PIC Admin harus berupa huruf!',
            'nomor_hp_pic_admin.numeric' => 'Nomor HP PIC Admin harus berupa angka!',
            'nomor_hp_pic_admin.digits_between' => 'Nomor HP PIC Admin minimum 10 digit dan maksimum 15 digit!',
            'detail_nomor_hp_pic_admin.nomor_hp_pic_admin.*.numeric' => 'Nomor HP PIC Admin harus berupa angka!',
            'detail_nomor_hp_pic_admin.nomor_hp_pic_admin.*.digits_between' => 'Nomor HP PIC Admin minimum 10 digit dan maksimum 15 digit!',
            'nama_pic_marketing.not_regex' => 'Nama PIC Marketing harus berupa huruf!',
            'detail_nama_pic_marketing.nama_pic_marketing.*.not_regex' => 'Nama PIC Marketing harus berupa huruf!',
            'nomor_hp_pic_marketing.numeric' => 'Nomor HP PIC Marketing harus berupa angka!',
            'nomor_hp_pic_marketing.digits_between' => 'Nomor HP PIC Marketing minimum 10 digit dan maksimum 15 digit!',
            'detail_nomor_hp_pic_marketing.nomor_hp_pic_marketing.*.numeric' => 'Nomor HP PIC Marketing harus berupa angka!',
            'detail_nomor_hp_pic_marketing.nomor_hp_pic_marketing.*.digits_between' => 'Nomor HP PIC Marketing minimum 10 digit dan maksimum 15 digit!',
            'ijin_usaha_id.required' => 'Ijin Usaha Wajib diisi!',
            'attachment.mimes' => 'Attachment harus berformat pdf!',
            'attachment.max' => 'Attachment berukuran maksimum 1 MB!',
        ];
    }
}
