<?php

namespace Modules\Rate\Requests\Api;

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\Order\Services\OrderService;
use Modules\Rate\Enums\RateEnum;
use Modules\Rate\Services\RateService;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Services\AccountService;

class RateRequest extends FormRequest
{
    public function __construct(
        private AccountService $accountService,
        private RateService $rateService,
        private OrderService $orderService,
    ){}

    /**
     * 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();
        $rate = $this->rateService->get([
            'product_id'=>request('product_id') ?? 0 ,
            'user_id'=> $account['user']->id ,
        ])->first();
        if($rate)
        {
            return [
                'already_exist'=>'required'
            ];
        }
        $order = $this->orderService->get([
            'product_id'=>request('product_id') ?? 0 ,
            'user_id'=> $account['user']->id ,
        ])->first();

        if(!$order)
        {
            return [
                'not_exist'=>'required'
            ];
        }
        $rates = RateEnum::values();
        $rules =
        [
            'product_id'=>[
                'required',
                Rule::exists('products','id')->whereNull('deleted_at')
            ],
            'rate'=>[
                'required',
                'in:'.implode(',',$rates)
            ],
            'comment'=>[
                'required',
                'string',
                'max:500'
            ],
        ];

        return $rules;
    }

    public function messages()
    {
        return
        [
            'already_exist.required' => __('web.already_exist'),
            'not_exist.required' => __('web.not_exist')
        ];
    }
    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        $attributes= [
            'product_id'=>__('web.product_id'),
            'rate'=>__('web.rate'),
            'comment'=>__('web.comment'),
        ];
        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));

    }
}
