<?php

namespace Modules\User\Requests\Api\PaymentCard;

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\UserService;

class PaymentCardRequest extends FormRequest
{
    public function __construct(
        private UserService $userService,
        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')
        {
            $card = $this->userService->getCard($this->id);
            if (!$card )
            {
                return
                    [
                        'not_exist' => 'required'
                    ];
            }
            if($card->user_id != $account['user']->id)
            {
                return
                    [
                        'not_exist' => 'required'
                    ];
            }
        }
        $rules =
        [
            'card_number'=>
                [
                    'required',
                    'numeric'
                ],
            'month'=>
                [
                    'required',
                    'numeric',
                    'min:1',
                    'max:12'
                ],
            'year'=>
                [
                    'required',
                    'numeric',
                    'min:'.date('y')
                ],
        ];

        return $rules;
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $attributes= [
            'card_number'=>__('web.card_number'),
            'month'=>__('web.month'),
            'year'=>__('web.year'),
        ];

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

    }
}
