<?php

namespace LaraBB\User\UI\Web\Requests\Register;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\ValidationException;
use JetBrains\PhpStorm\ArrayShape;

/**
 * Class Show
 * @package LaraBB\User\Http\Requests\Register
 */
class Show extends FormRequest
{
    /**
     * @var string
     */
    protected $redirectRoute = 'register';

    /**
     * @return bool
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * @return string[]
     */
    #[ArrayShape(['code' => "string"])]
    public function rules(): array
    {
        return [
            'code' => 'nullable|exists:users_invites,code'
        ];
    }

    /**
     * @return array
     */
    #[ArrayShape(['code.exists' => "mixed"])]
    public function messages(): array
    {
        return [
            'code.exists' => __('The given invite code is invalid. Redirecting to guest registration.')
        ];
    }

    /**
     * @param null $keys
     * @return array
     */
    public function all($keys = null): array
    {
        $data         = parent::all($keys);
        $data['code'] = $this->route('code');

        return $data;
    }

    /**
     * @param Validator $validator
     * @throws ValidationException
     */
    public function failedValidation(Validator $validator)
    {
        session()->flash('alert-options', 'auto-hide');

        parent::failedValidation($validator); // TODO: Change the autogenerated stub
    }
}
