<?php

namespace App\Http\Requests;

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

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

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

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