<?php

namespace Modules\User\Requests\Web;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
use Modules\User\Enums\AddressStatusEnum;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Services\AccountService;
use Modules\User\Services\UserService;

class VerifyAddressRequest extends FormRequest
{
    public function __construct(
        private AccountService $accountService,
        private UserService $userService,
    ){}

    /**
     * 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()
    {
        $statuses = AddressStatusEnum::values();
        $rules =
            [
                'code_1'=>
                    [
                        'required'
                    ],
                'code_2'=>
                    [
                        'required'
                    ],
                'code_3'=>
                    [
                        'required'
                    ],
                'code_4'=>
                    [
                        'required'
                    ],
                'user_name'=>
                    [
                        'required',
                        'string'
                    ],
                'address'=>
                    [
                        'required',
                        'string'
                    ],
                'phone'=>
                    [
                        'required',
                        'numeric'
                    ],
                'country_id'=>
                    [
                        'required',
                        Rule::exists('countries','id')->whereNull('deleted_at')
                    ],
                'region_id'=>
                    [
                        'required',
                        Rule::exists('regions','id')->whereNull('deleted_at')
                    ],
                'city_id'=>
                    [
                        'required',
                        Rule::exists('cities','id')->whereNull('deleted_at')
                    ],
                'delivery_status'=>
                    [
                        'required',
                        'in:'.implode(',',$statuses)
                    ],
                'setDefault'=>
                    [
                        'nullable',
                    ],
                'latitude'=>
                    [
                        'nullable',
                    ],
                'longitude'=>
                    [
                        'nullable',
                    ]
            ];
        if ( request('code_1') && request('code_2') && request('code_3') && request('code_4'))
        {
            $code = request('code_1').request('code_2').request('code_3').request('code_4');
            $account = $this->accountService->getAccount();
            $code = $this->userService->checkCode([
                'email'=>$account['user']->email,
                'code'=>$code,
            ]);
            if (!$code)
            {
                $rules['not_correct_code'] = 'required';
            }
        }
        return $rules;
    }

    public function messages()
    {
        return [
            'not_correct_code.required' => __('web.not_correct_code')
        ];
    }
    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $attributes= [
            'email'=>__('web.email'),
        ];

        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);
        }
    }
}
