<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'thematique' => [
                'required',
                'string'
            ],
            'annee' => [
                'required',
                'integer',
                Rule::unique('forums')
            ],
            'objectif' => [
                'string',
                'nullable'
            ],
            'detail' => [
                'string',
                'nullable'
            ],
            'synthese' => [
                'string',
                'nullable'
            ],
            'contexte' => [
                'string',
                'nullable'
            ]
        ];

        if ($this->getMethod() == 'PUT') {
            $rules['annee'] = [
                'required',
                'integer',
                Rule::unique('forums')
                    ->ignore($this->route('forum')->id, 'id')
            ];
        }

        return $rules;
    }

    public function getForumPayload()
    {
        return collect($this->validated())
            ->toArray();
    }
}
