<?php

namespace Modules\Travel\Http\Requests;

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

class TravelWorkflowRequest extends FormRequest
{
    use ApiReturnFormatTrait;
    
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'date'                          => 'required|date',
            'start_time'                    => ['required', 'regex:/^\d{1,2}:\d{2}(:\d{2})?$/', 'before:end_time'],
            'end_time'                      => ['required', 'regex:/^\d{1,2}:\d{2}(:\d{2})?$/', 'after:start_time'],
            'calls_made'                    => 'required|numeric|min:0',
            'positive_leads'                => 'required|numeric|min:0',
            'total_sales'                   => 'required|string|max:255',
            'is_update_pending_and_leads'   => 'required|in:Yes,No,Other',
            'pending_and_leads_other_note'  => 'nullable|required_if:is_update_pending_and_leads,Other|string|max:255',
            'is_worked_on_recovery_today'   => 'required|in:Yes,No,Other',
            'recovery_today_other_note'     => 'nullable|required_if:is_worked_on_recovery_today,Other|string|max:255',
            'daily_report_summary'          => 'required|string|min:10',
            'complaints_questions_comments' => 'nullable|string|max:255',
            'file'                          => 'nullable|file|mimes:jpeg,png,jpg,pdf,docx|max:2048',
            'how_was_your_day_mark'         => 'required|integer|between:1,10',
        ];
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'date.required'                            => _trans('response.The date is required.'),
            'date.date'                                => _trans('response.The date must be a valid date.'),
            'start_time.required'                      => _trans('response.The start time field is required.'),
            'start_time.regex'                         => _trans('response.The start time format is invalid') . '. ' . _trans('response.The accepted format is HH:mm or HH:mm:ss.'),
            'start_time.before'                        => _trans('response.The start time must be before the end time.'),
            'end_time.required'                        => _trans('response.The end time field is required.'),
            'end_time.regex'                           => _trans('response.The end time format is invalid') . '. ' . _trans('response.The accepted format is HH:mm or HH:mm:ss.'),
            'end_time.after'                           => _trans('response.The end time must be after the start time.'),
            'calls_made.required'                      => _trans('response.Calls made are required.'),
            'calls_made.numeric'                       => _trans('response.Calls made must be a number.'),
            'positive_leads.required'                  => _trans('response.Positive leads are required.'),
            'positive_leads.numeric'                   => _trans('response.Positive leads must be a number.'),
            'total_sales.required'                     => _trans('response.Total sales are required.'),
            'total_sales.string'                       => _trans('response.Total sales must be a string.'),
            'is_update_pending_and_leads.required'     => _trans('response.Please select an option for pending leads update.'),
            'pending_and_leads_other_note.required_if' => _trans('response.Please provide additional details for "Other" option.'),
            'is_worked_on_recovery_today.required'     => _trans('response.Please select an option for recovery work today.'),
            'recovery_today_other_note.required_if'    => _trans('response.Please provide additional details for "Other" option.'),
            'daily_report_summary.required'            => _trans('response.Daily report summary is required.'),
            'daily_report_summary.min'                 => _trans('response.Daily report summary must be at least 10 characters.'),
            'complaints_questions_comments.string'     => _trans('response.Complaints/questions/comments must be a string.'),
            'file.file'                                => _trans('response.The file must be a valid file.'),
            'file.mimes'                               => _trans('response.The file must be of type: jpeg, png, jpg, pdf, docx.'),
            'file.max'                                 => _trans('response.The file size may not exceed 2MB.'),
            'how_was_your_day_mark.required'           => _trans('response.Please rate your day in terms of production.'),
            'how_was_your_day_mark.integer'            => _trans('response.The rating must be an integer.'),
            'how_was_your_day_mark.between'            => _trans('response.The rating must be between 1 and 10.'),
        ];
    }

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


    /**
     * Check if the request is coming from an API.
     *
     * @return bool
     */
    protected function isApiRequest()
    {
        return $this->expectsJson();
    }

    /**
     * Handle a failed validation attempt.
     *
     * @param  \Illuminate\Contracts\Validation\Validator  $validator
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function failedValidation(Validator $validator)
    {
        if ($this->isApiRequest()) {
            throw new HttpResponseException(
                $this->responseWithError(
                    _trans('response.Validation Error'),
                    $validator->errors()->all(),
                    422
                )
            );
        }

        parent::failedValidation($validator); // For web requests, use the standard behavior
    }
}
