<?php

namespace App\Http\Requests;

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

/**
 * @property mixed video_type
 * @property mixed video_url
 * @property mixed images
 */
class SubscribeRequest 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=[
            'bank_name_from' => 'required|min:3|max:100',
            'bank_name_to' => 'required|min:3|max:100',
            'owner_name' => 'required|min:3|max:100',
            'email' => 'required|email|max:100',
            'account_no' => 'required|min:3|max:100',
            'amount' => 'required|numeric',
            'trans_date' => 'required|date',
            'trans_document' => 'required|image|mimes:png,jpg,jpeg',
            'terms' => 'required',
        ];
        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $local = getCurrentLocale();
        $attributes=[
            'bank_name_from' => metaFields('subscriptions', 'bank_name_from', $local) ??__('subscriptions.bank_name_from'),
            'bank_name_to' => metaFields('subscriptions', 'bank_name_to', $local) ??__('subscriptions.bank_name_to'),
            'owner_name' => metaFields('subscriptions', 'owner_name', $local) ??__('subscriptions.owner_name'),
            'email' => metaFields('subscriptions', 'email', $local) ??__('subscriptions.email'),
            'account_no' => metaFields('subscriptions', 'account_no', $local) ??__('subscriptions.account_no'),
            'amount' =>  metaFields('subscriptions', 'amount', $local) ??__('subscriptions.amount'),
            'trans_date' =>  metaFields('subscriptions', 'trans_date', $local) ??__('subscriptions.trans_date'),
            'trans_document' =>  metaFields('subscriptions', 'trans_document', $local) ??__('subscriptions.trans_document'),
            'terms' =>  metaFields('subscriptions', 'terms', $local) ??__('subscriptions.terms'),
        ];

        return $attributes;
    }


    /**
     * Handle a failed validation attempt.
     *
     * Override the parent method action if the request is for AJAX
     *
     * @param Validator $validator
     * @return void
     *
     * @throws ValidationException
     */
    protected function failedValidation(Validator $validator)
    {
        // Change response attitude if the request done via Ajax requests like API requests
        if(Request::wantsJson()){
            $errors = (new ValidationException($validator))->errors();
            throw new HttpResponseException(response()->json(['errors' => $errors], 422));
        } else {
            parent::failedValidation($validator);
        }
    }
}
