<?php

namespace App\Http\Requests\Api\Gallery;

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

class GalleryItemUpdateRequest 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()
    {
        $user = auth()->user();

        return $user->mainAccount->uid === $this->route('accountUid');
    }

    /**
     * Prepare the data for validation.
     *
     * @return void
     */
    protected function prepareForValidation()
    {
        $this->merge([
            'limit' => $this->formatter->transformItem($this->input('limit', 9), 'strip_tags|xss_clean|trim'),
            'offset' => $this->formatter->transformItem($this->input('offset', 0), 'strip_tags|xss_clean|trim'),
            'state' => $this->formatter->transformItem($this->input('state', Gallery::STATE_ACTIVE), 'strip_tags|xss_clean|trim'),
            'isFavourite' => $this->input('isFavourite') === null ? null : $this->input('isFavourite') === 'true',
        ]);
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'limit' => "required|integer",
            'offset' => "required|integer",
            'isFavourite' => "nullable|boolean",
            'state' => "required|string|max:20"
        ];
    }
}
