<?php

namespace App\Http\Requests;

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

class PaymentMethodRequest 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('payment_methods.store');
        case 'PUT':
          return $this->user()->hasPermissionTo('payment_methods.update');
        case 'DELETE':
          return $this->user()->hasPermissionTo('payment_methods.destroy');
      }
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
      switch ($this->method()) {
        case 'POST':
          return [
            'name' => 'required|max:191',
          ];
        case 'PUT':
          return [
            'payment_method_id' => 'required|exists:payment_methods,id',
            'name' => 'required|max:191',
          ];
        case 'DELETE':
          return [
            'payment_method_id' => 'required|exists:payment_methods,id',
          ];
      }
      return [
        //
      ];
    }

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