<?php

namespace Guru\Product\Http\Requests;

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

class ProductAdminWebRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize(Request $request)
    {
        $product = $this->route('product');

        if (is_null($product)) {
            // Determine if the user is authorized to access product module,
            return $request->user('admin.web')->canDo('product.product.view');
        }

        if ($request->isMethod('POST') || $request->is('*/create')) {
            // Determine if the user is authorized to create an entry,
            return $request->user('admin.web')->can('create', $product);
        }

        if ($request->isMethod('PUT') || $request->isMethod('PATCH') || $request->is('*/edit')) {
            // Determine if the user is authorized to update an entry,
            return $request->user('admin.web')->can('update', $product);
        }

        if ($request->isMethod('DELETE')) {
            // Determine if the user is authorized to delete an entry,
            return $request->user('admin.web')->can('delete', $product);
        }

        // Determine if the user is authorized to view the module.
        return $request->user('admin.web')->can('view', $product);

    }

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

        if ($request->isMethod('PUT') || $request->isMethod('PATCH')) {
            // Validation rule for update request.
            return [
                'model'               => 'required',
                'title'               => 'required',
                'category_id'         => 'required',
            ];
        }

        // Default validation rule.
        return [

        ];
    }
}