<?php

namespace Modules\Travel\Http\Requests;

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

class TravelMeetingRequest extends FormRequest
{
    use ApiReturnFormatTrait;

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'date'                         => 'required|date',
            'meeting_schedule_time'        => ['required', 'regex:/^\d{2}:\d{2}(:\d{2})?$/'],
            'meeting_start_time'           => ['required', 'regex:/^\d{2}:\d{2}(:\d{2})?$/', 'before:meeting_end_time'],
            'meeting_end_time'             => ['required', 'regex:/^\d{2}:\d{2}(:\d{2})?$/', 'after:meeting_start_time'],
            'customer_company_name'        => 'required|string|max:255',
            'customer_name'                => 'required|string|max:255',
            'customer_email'               => 'required|email|max:255',
            'customer_phone'               => 'required|string|max:50',
            'purpose'                      => 'required|string',
            'remark'                       => 'nullable|string',
            'file'                         => 'nullable|file|mimes:jpg,jpeg,png,pdf,doc,docx|max:2048',
            'file_id'                      => 'nullable|integer|exists:uploads,id',
            'rating_of_meeting_experience' => 'nullable|integer|min:1|max:5',
        ];

        // Add the travel_plan_id validation for API requests only
        if ($this->isApiRequest()) {
            $rules['travel_plan_id'] = 'required|integer';
        }

        return $rules;
    }


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

    /**
     * Custom messages for validation errors.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'travel_plan_id.required'              => _trans('response.The travel plan field is required.'),
            'travel_plan_id.integer'               => _trans('response.The travel plan ID must be a valid number.'),
            'date.required'                        => _trans('response.The date field is required.'),
            'date.date'                            => _trans('response.The date is not a valid date.'),
            'meeting_schedule_time.required'       => _trans('response.The schedule time field is required.'),
            'meeting_schedule_time.regex'          => _trans('response.The schedule time format is invalid. The accepted format is HH:mm or HH:mm:ss.'),
            'meeting_start_time.required'          => _trans('response.The start time field is required.'),
            'meeting_start_time.regex'             => _trans('response.The start time format is invalid. The accepted format is HH:mm or HH:mm:ss.'),
            'meeting_start_time.before'            => _trans('response.The start time must be before the end time.'),
            'meeting_end_time.required'            => _trans('response.The end time field is required.'),
            'meeting_end_time.regex'               => _trans('response.The end time format is invalid. The accepted format is HH:mm or HH:mm:ss.'),
            'meeting_end_time.after'               => _trans('response.The end time must be after the start time.'),
            'customer_company_name.required'       => _trans('response.The company name field is required.'),
            'customer_company_name.max'            => _trans('response.The company name may not be greater than 255 characters.'),
            'customer_name.required'               => _trans('response.The customer name field is required.'),
            'customer_name.max'                    => _trans('response.The customer name may not be greater than 255 characters.'),
            'customer_email.required'              => _trans('response.The email field is required.'),
            'customer_email.email'                 => _trans('response.The email must be a valid email address.'),
            'customer_phone.required'              => _trans('response.The phone field is required.'),
            'customer_phone.max'                   => _trans('response.The phone number may not be greater than 50 characters.'),
            'purpose.required'                     => _trans('response.The purpose field is required.'),
            'file.mimes'                           => _trans('response.The file must be a type of: jpg, jpeg, png, pdf, doc, docx.'),
            'file.max'                             => _trans('response.The file may not be greater than 2MB.'),
            'file_id.integer'                      => _trans('response.The file ID must be a valid number.'),
            'file_id.exists'                       => _trans('response.The selected file ID is invalid or does not exist.'),
            'rating_of_meeting_experience.integer' => _trans('response.The rating must be a number between 1 and 5.'),
            'rating_of_meeting_experience.min'     => _trans('response.The rating must be at least 1.'),
            'rating_of_meeting_experience.max'     => _trans('response.The rating may not be greater than 5.'),
        ];
    }


    /**
     * 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
    }
}
