<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\Request;

class UnitRequest 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()
    {
        $selectedCompounds = 'nesma_village_khobar,atheeb_village_khobar,nesma_village_jubail,nesma_roots_jubail,tahlia_compound_jeddah,rawda_aapartment';

        $phonePattern = '/^(05){1}([0-9]{8})$/';

        return [
            'applicant_name' => 'required|min:3|max:100',
            'nationality' => 'required|min:3|max:20',
            'dependent_no' => 'required|numeric|gt:0',
            'adult' => 'required|numeric|gt:0',
            'children' => 'required|numeric|gt:0',
            'id_type' => 'required|in:iqama,passport',
            'id_no' => 'required|numeric|regex:/[0-9]{10,30}/',
            'rental_source' => 'required|in:company,self',
            'company_name' => 'required_if:rental_source,company|nullable|min:3|max:100',
            'company_address' => 'required_if:rental_source,company|nullable|min:3|max:200',
            'company_contact_no' => 'required_if:rental_source,company|regex:'.$phonePattern,
            'person_name' => 'required_if:rental_source,self|nullable|min:3|max:100',
            'person_address' => 'required_if:rental_source,self|nullable|min:3|max:200',
            'person_contact_no' => 'required_if:rental_source,self|nullable|regex:'.$phonePattern,
            'applicant_contact' => 'required|array',
            'selected_compound' => "required|array|in:{$selectedCompounds}",
        ];
    }

    public function attributes()
    {
        return [
            'applicant_name' => __('units.applicant_name'),
            'nationality' => __('units.nationality'),
            'dependent_no' => __('units.dependent_no'),
            'adult' => __('units.adult'),
            'children' => __('units.children'),
            'id_type' => __('units.id_type'),
            'id_no' => __('units.id_no'),
            'rental_source' => __('units.rental_source'),
            'company_name' => __('units.company_name'),
            'company_address' => __('units.company_address'),
            'company_contact_no' => __('units.company_contact_no'),
            'person_name' => __('units.person_name'),
            'person_address' => __('units.person_address'),
            'person_contact_no' => __('units.person_contact_no'),
            'applicant_contact' => __('units.applicant_contact'),
            'selected_compound' => __('units.selected_compound'),
        ];
    }

    protected function failedValidation(Validator $validator)
    {
        // Change response attitude if the request done via Ajax requests like API requests
        if(Request::ajax()){
            $errors = $validator->errors()->first();
            throw new HttpResponseException(response()->json(['errors' => $errors]));
        } else {
            parent::failedValidation($validator);
        }
    }
}
