<?php

namespace Article\Article\Http\Requests;

use App\Http\Requests\Request as FormRequest;
use Illuminate\Http\Request;
use Gate;

class ArticleUserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize(Request $request)
    {
        $article = $this->route('article');
        // Determine if the user is authorized to access article module,
        if (is_null($article)) {
            return $request->user()->canDo('article.article.view');
        }

        // Determine if the user is authorized to create an entry,
        if ($request->isMethod('POST') || $request->is('*/create')) {
            return Gate::allows('create', $article);
        }

        // Determine if the user is authorized to update an entry,
        if ($request->isMethod('PUT') || $request->isMethod('PATCH') || $request->is('*/edit')) {
            return Gate::allows('update', $article);
        }

        // Determine if the user is authorized to delete an entry,
        if ($request->isMethod('DELETE')) {
            return Gate::allows('delete', $article);
        }

        // Determine if the user is authorized to view the module.
        return Gate::allows('view', $article);

    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(Request $request)
    {
        // validation rule for create request.
        if ($request->isMethod('POST')) {
            return [
                'title'               => 'required',
                'slug'                => 'required',
                'sapo'                => 'required',
                'content'             => 'required',
                'category_id'         => 'required',
            ];
        }
        // Validation rule for update request.
        if ($request->isMethod('PUT') || $request->isMethod('PATCH')) {
            return [
                'title'               => 'required',
                'slug'                => 'required',
                'sapo'                => 'required',
                'content'             => 'required',
                'category_id'         => 'required',
            ];
        }

        // Default validation rule.
        return [

        ];
    }
}