<?php

namespace LaraBB\Post\UI\Web\Requests;

use Illuminate\Foundation\Http\FormRequest;
use LaraBB\Thread\Models\Thread;
use LaraBB\Thread\Tasks\FindTask;

/**
 * Class Create
 * @package LaraBB\Post\UI\Web\Requests
 */
class Create extends FormRequest
{
    /**
     * @var Thread|null
     */
    public ?Thread $thread;

    /**
     * @param FindTask $getThreadTask
     * @return bool
     */
    public function authorize(FindTask $getThreadTask): bool
    {
        $this->thread = $getThreadTask->bySlug($this->route('slug'), ['forum']);
        if(is_null($this->thread)) {
            abort(404, __('The desired thread does not exist.'));
        }

        if(!$this->user()->forumPermissions[$this->thread->forum_uuid]->create_posts) {
            abort(403, __('You are not authorized to create postings in this forum.'));
        }

        return true;
    }

    /**
     * @return array
     */
    public function rules(): array
    {
        return [];
    }

    /**
     * @return array
     */
    public function messages(): array
    {
        return [];
    }
}
