<?php

namespace App\Http\Requests\Api\User;

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

class UserRequest 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 auth()->user()->uid === $this->route('userUid');
    }

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'firstName' => "required|max:30",
            'lastName' => "required|max:30",
            'shortBio' => "nullable|max:120",
            'location' => "nullable|max:120",
        ];
    }
}
