<?php

namespace LaraBB\User\UI\Web\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\RedirectResponse;
use Illuminate\Validation\Rule;
use LaraBB\Application\Traits\Request;
use LaraBB\Group\Models\Group;

/**
 * Class Store
 * @package LaraBB\User\UI\Web\Requests
 */
class Store extends FormRequest
{
    use Request;

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

    /**
     * @return array
     */
    public function rules(): array
    {
        $rules             = $this->getRules('user', ['uuid']);
        $rules['password'] = str_replace('nullable', 'required', $rules['password']);
        $rules['groups.*'] = [
            Rule::in(Group::all()->pluck('uuid')->toArray())
        ];

        return $rules;
    }

    /**
     * @return array|string[]
     */
    public function messages(): array
    {
        return $this->getMessages('user', ['uuid']);
    }

    /**
     * @return RedirectResponse
     */
    public function success(): RedirectResponse
    {
        return redirect()->route('admin.users')->with([
            'success'       => 'Das Benutzerkonto wurde erfolgreich erstellt.',
            'alert-options' => 'aside'
        ]);
    }

    /**
     * @return RedirectResponse
     */
    public function failed(): RedirectResponse
    {
        return redirect()->route('admin.users.create')->with([
            'error' => 'Das Benutzerkonto konnte aufgrund einer technischen Störung nicht erstellt werden.'
        ])->withInput();
    }
}
