<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Auth\Access\AuthorizationException;

use Illuminate\Validation\Rule;

class CustomerRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
      switch ($this->method()) {
        case 'POST':
          return $this->user()->hasPermissionTo('customers.store');
        case 'PUT':
          return $this->user()->hasPermissionTo('customers.update');
        case 'DELETE':
          return $this->user()->hasPermissionTo('customers.destroy');
      }
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        switch ($this->method()) {
          case 'POST':
            return [
              'contact' => 'required|max:191',
              'email' => 'required|max:191|email|unique:customers',
              'address' => 'required|max:191',
              'state' => 'required|max:191',
            ];
          case 'PUT':
            return [
              'contact' => 'required|max:191',
              'email' => ['required', 'email', 'max:191',],
              'address' => 'required|max:191',
              'state' => 'required|max:191',
            ];
          case 'DELETE':
            return [
            ];
        }
        return [
          //
        ];
    }

    protected function failedAuthorization()
    {
        throw new AuthorizationException(__('errors.action_unauthorized'));
    }
}
