<?php

namespace App\Http\Requests;

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

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
      switch ($this->method()) {
        case 'POST':
          return [
            'folio' => 'required|max:191',
            'subtotal' => 'required|numeric',
            'total' => 'required|numeric',
            'status' => 'required|numeric',
            'details.*.quantity' => 'required|numeric',
            'details.*.unit_price' => 'required|numeric',
            'details.*.total' => 'required|numeric',
          ];
        case 'PUT':
          return [
            'folio' => 'required|max:191',
            'subtotal' => 'required|numeric',
            'total' => 'required|numeric',
            'status' => 'required|numeric',
            'details.*.quantity' => 'required|numeric',
            'details.*.unit_price' => 'required|numeric',
            'details.*.total' => 'required|numeric',
          ];
        case 'DELETE':
          return [
          ];
      }
      return [
        //
      ];
    }

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