<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

use App\Models\Announcement;

class AnnouncementValidation extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        if (!auth()->check()) {
            return false;
        }

        if ($this->route('id')) {
            $announcement = Announcement::findorFail($this->route('id'));
            return $this->user()->can('update', $announcement);
        }
        return $this->user()->can('create', Announcement::class);
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'version.name' => 'required|string',
            'version.title' => 'string|nullable',
            'version.unlisted' => 'boolean',
        ];
    }
}
