<?php

namespace Modules\Travel\Http\Requests;

use App\Rules\Base64ImageValidator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use App\Helpers\CoreApp\Traits\ApiReturnFormatTrait;
use Illuminate\Http\Exceptions\HttpResponseException;

class TravelPlanRequest extends FormRequest
{
    use ApiReturnFormatTrait;

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'from_date'     => 'required|date|before_or_equal:to_date',
            'to_date'       => 'required|date|after_or_equal:from_date',
            'from_location' => 'required|string|max:255',
            'to_location'   => 'required|string|max:255',
            'purpose'       => 'required|string',
            'amount'        => 'required|numeric|min:0',
            'signature'     => ['required', new Base64ImageValidator],
            'signature_date'=> 'required|date',
        ];
    }

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

    public function messages()
    {
        return [
            'from_date.required'        => _trans('response.The start date is required.'),
            'from_date.date'            => _trans('response.The start date is not a valid date.'),
            'from_date.before_or_equal' => _trans('response.The start date must be before or equal to the end date.'),
            'to_date.required'          => _trans('response.The end date is required.'),
            'to_date.date'              => _trans('response.The end date is not a valid date.'),
            'to_date.after_or_equal'    => _trans('response.The end date must be after or equal to the start date.'),
            'from_location.required'    => _trans('response.The starting location is required.'),
            'from_location.string'      => _trans('response.The starting location must be a string.'),
            'from_location.max'         => _trans('response.The starting location may not be greater than 255 characters.'),
            'to_location.required'      => _trans('response.The destination is required.'),
            'to_location.string'        => _trans('response.The destination must be a string.'),
            'to_location.max'           => _trans('response.The destination may not be greater than 255 characters.'),
            'purpose.required'          => _trans('response.The purpose is required.'),
            'purpose.string'            => _trans('response.The purpose of travel must be a string.'),
            'amount.required'           => _trans('response.The amount is required.'),
            'amount.numeric'            => _trans('response.The travel amount must be a number.'),
            'amount.min'                => _trans('response.The travel amount must be at least 0.'),
            'signature.required'        => _trans('response.The signature field is required.'),
            'signature_date.required'   => _trans('response.The signature date field is required.'),
            'signature_date.date'       => _trans('response.The signature date is not a valid date.'),
        ];
    }

    /**
     * Handle a failed validation attempt.
     *
     * @param  Validator  $validator
     * @return void
     */
    public function failedValidation(Validator $validator)
    {
        // Check if the request expects JSON
        if ($this->expectsJson()) {
            // API response with JSON format
            throw new HttpResponseException(
                $this->responseWithError(__('Validation Error'), $validator->errors()->all(), 422)
            );
        }

        // Default behavior (web response)
        parent::failedValidation($validator);
    }
}
