<?php

namespace Modules\User\Requests\Api\Address;

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\Services\AccountService;
use Modules\User\Services\AddressService;

class AddressRequest extends FormRequest
{
    public function __construct(
        private AddressService $addressService,
        private AccountService $accountService,
    ){}
    /**
     * 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()
    {
        $account = $this->accountService->getAccount();
        $rules = [];
        if (request()->method() == 'PUT')
        {
            $address = $this->addressService->getItem($this->id);
            if (!$address) {
                return
                    [
                        'not_exist' => 'required'
                    ];
            }
            if ($address->user_id != $account['user']->id || $address->user_status != $account['status']) {
                return
                    [
                        'not_exist' => 'required'
                    ];
            }
        }
        $statuses = AddressStatusEnum::values();
        $rules =
            [
                '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',
                    ]
            ];

        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $attributes= [
            'email'=>__('web.email'),
            'user_name'=>__('web.user_name'),
            'address'=>__('web.address'),
            'phone'=>__('web.phone'),
            'country_id'=>__('web.country_id'),
            'region_id'=>__('web.region_id'),
            'city_id'=>__('web.city_id'),
            'delivery_status'=>__('web.delivery_status'),
            'setDefault'=>__('web.setDefault'),
            'latitude'=>__('web.map'),
            'longitude'=>__('web.map'),
        ];

        return $attributes;
    }
    /**
     * Handle a failed validation attempt.
     *
     * Override the parent method action if the request is for AJAX
     *
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function failedValidation(Validator $validator)
    {
        // Change response attitude if the request done via Ajax requests like API requests

        $errors = (new ValidationException($validator))->errors();
        $message = $validator->errors()->first();
        throw new HttpResponseException(response()->json(['message'=> $message, 'errors' => $errors],  400));

    }
}
