<?php

namespace App\Http\Requests\Api;

use Illuminate\Foundation\Http\FormRequest;
use App\Support\Formatter;
use RuntimeException;

class UserSignupRequest extends FormRequest
{
    public function __construct(Formatter $formatter)
    {
        $this->formatter = $formatter;
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Prepare the data for validation.
     *
     * @return void
     */
    protected function prepareForValidation()
    {
        $this->merge([
            'fullName' => $this->formatter->transformItem($this->fullName, 'strip_tags|xss_clean|trim'),
            'email' => $this->formatter->transformItem($this->email, 'strip_tags|xss_clean|trim|strtolower')
        ]);
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'fullName' => 'required|max:45',
            'email' => 'required|max:255|email|unique:users',
            'password' => $this->getPasswordRule()
        ];
    }

    protected function getPasswordRule($isRequired = true)
    {
        return ($isRequired ? 'required|' : '') . config('options.password_rules');
    }
}
