<?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 Update
 * @package LaraBB\User\UI\Web\Requests
 */
class Update extends FormRequest
{
    use Request;

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

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

        return $rules;
    }

    /**
     * @return array
     */
    public function messages(): array
    {

        return $this->getMessages('user', ['username.unique']);
    }

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

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