<?php

namespace Modules\Refund\Requests\Api;

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\Order\Services\CartService;
use Modules\Order\Services\OrderService;
use Modules\Refund\Services\RefundService;
use Modules\User\Services\AccountService;

class CreateRequest extends FormRequest
{
    public function __construct(
        private OrderService $orderService,
        private RefundService $refundService,
        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();
        $product = $this->orderService->getProduct([
            'id'=>request('order_product_id') ?? 0,
            'user_id'=>$account['user']->id
        ]);

        $rules = [];
        if (!$product )
        {
            return
                [
                    'not_exist' => 'required'
                ];
        }
        $refund = $this->refundService->get([
            'order_product_id'=>request('order_product_id') ?? 0,
        ])->first();
        if ($refund)
        {
            return [
                'refunded'=>'required'
            ];
        }
        $rules['reason_id'] =
        [
            'required',
            Rule::exists('reasons','id')->whereNull('deleted_at')
        ];
        $rules['note']=
        [
            'nullable',
            'string'
        ];
        $rules['image'] =
        [
            'nullable',
            'image',
            'mimes:jpg,jpeg,png'
        ];
        $rules['user_card_id'] =
        [
            'required',
            Rule::exists('user_cards','id')->where('user_id',auth()->user()->id)->whereNull('deleted_at')
        ];
        return $rules;
    }

    public function messages()
    {
        return [
            'refunded.required' => __('web.refunded'),
            'not_exist.required' => __('web.not_exist')
        ];
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {

        $attributes= [
            'product_id' => __('web.product_id'),
            'count' => __('web.count'),
            'option_id' => __('web.option'),
        ];
        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));

    }
}
