<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class CityRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $method = strtolower($this->method());
        $city = $this->route('city');
        switch ($method) {
            case 'post':
                $rules = [
                    'name'                  => 'required|unique:cities,name',
                    'country_id'            => 'required',
                    'fixed_charges'         => 'required',
                    'cancel_charges'        => 'required',
                    'min_distance'          => 'required',
                    'min_weight'            => 'required',
                    'per_distance_charges'  => 'required',
                    'per_weight_charges'    => 'required',
                    'admin_commission'      => 'required',

                ];
                break;
            case 'patch':
                $rules = [
                    'name'                  => 'required|unique:cities,name,' . $city,
                    'country_id'            => 'required',
                    'fixed_charges'         => 'required',
                    'cancel_charges'        => 'required',
                    'min_distance'          => 'required',
                    'min_weight'            => 'required',
                    'per_distance_charges'  => 'required',
                    'per_weight_charges'    => 'required',
                    'admin_commission'      => 'required',
                ];
                break;
        }

        return $rules;
    }

    public function messages()
    {
        return [
            'country_id.required'                   => 'You must select at least one country.',
            'name.required'                         => 'The name field is required.',
            'fixed_charges.required'                => 'The Fixed Chareges required',
            'cancel_charges.required'               => 'The Cancel Chareges required',
            'min_distance.required'                 => 'The Min Chareges required',
            'min_weight.required'                   => 'The Min Weight required',
            'per_distance_charges.required'         => 'The Per Distance Chareges required',
            'per_weight_charges.required'           => 'The Per Weight Chareges required',
            'admin_commission.required'             => 'The Admin commision required',

        ];
    }
    /**
     * @param Validator $validator
     */
    protected function failedValidation(Validator $validator)
    {
        $data = [
            'status' => 422,
            'message' => $validator->errors()->first(),
        ];

        if ( request()->is('api*')){
           throw new HttpResponseException( response()->json($data,422) );
        }

        if ($this->ajax()) {
            throw new HttpResponseException(response()->json($data,422));
        } else {
            throw new HttpResponseException(redirect()->back()->withInput()->with('errors', $validator->errors()));
        }
    }
}
