<commit_before>class TripScheduler
  attr_reader :trip, :run

  def initialize(trip_id, run_id)
    @trip = Trip.find_by_id(trip_id)
    @run = get_run(run_id.to_i)
  end

  def execute
    return response_as_json(false) if !@trip || !@run 
    return response_as_json(true) if @trip.adjusted_run_id == @run.id

    case @run.id
    when Run::UNSCHEDULED_RUN_ID
      unschedule
    when Run::CAB_RUN_ID 
      schedule_to_cab
    else
      schedule_to_run
    end
  end

  private

  def get_run(run_id)
    case run_id
    when Run::CAB_RUN_ID 
      Run.fake_cab_run
    when Run::UNSCHEDULED_RUN_ID
      Run.fake_unscheduled_run
    else
      Run.find_by_id(run_id)
    end
  end

  def unschedule
    @trip.update_attribute :cab, false
    @trip.update_attribute :run, nil
    response_as_json true
  end

  def schedule_to_cab
    @trip.update_attribute :cab, true
    @trip.update_attribute :run, nil
    response_as_json true
  end

  def schedule_to_run
    if !validate_time_availability
      response_as_json false, TranslationEngine.translate_text(:not_fit_in_run_schedule)
    elsif !validate_vehicle_availability 
      response_as_json false, TranslationEngine.translate_text(:vehicle_unavailable)
    elsif !validate_driver_availability 
      response_as_json false, TranslationEngine.translate_text(:driver_unavailable)
    else
      @trip.cab = false
      @trip.run = @run
      response_as_json @trip.save, @trip.errors.full_messages.join(';')
    end
  end

  # run avaiability validations

  # run can hold a trip
  def validate_time_availability
    run_start_time = @run.scheduled_start_time
    run_end_time = @run.scheduled_end_time

    if run_start_time && run_end_time
      @trip.pickup_time >= @run.scheduled_start_time && (@trip.appointment_time.nil? || @trip.appointment_time <= @run.scheduled_end_time)
    else
      true
    end
  end

  def validate_vehicle_availability
    @run.vehicle && @run.vehicle.active
  end

  def validate_driver_availability
    @run.driver && @run.driver.active
  end

  def response_as_json(is_success, error_text = '')
    {
      success: is_success,
      message: error_text || '',
      trip_event_json: is_success ? @trip.as_run_event_json : nil
    }
  end

end<commit_msg>Fix run time comparison to trip time<commit_after>class TripScheduler
  attr_reader :trip, :run

  def initialize(trip_id, run_id)
    @trip = Trip.find_by_id(trip_id)
    @run = get_run(run_id.to_i)
  end

  def execute
    return response_as_json(false) if !@trip || !@run 
    return response_as_json(true) if @trip.adjusted_run_id == @run.id

    case @run.id
    when Run::UNSCHEDULED_RUN_ID
      unschedule
    when Run::CAB_RUN_ID 
      schedule_to_cab
    else
      schedule_to_run
    end
  end

  private

  def get_run(run_id)
    case run_id
    when Run::CAB_RUN_ID 
      Run.fake_cab_run
    when Run::UNSCHEDULED_RUN_ID
      Run.fake_unscheduled_run
    else
      Run.find_by_id(run_id)
    end
  end

  def unschedule
    @trip.update_attribute :cab, false
    @trip.update_attribute :run, nil
    response_as_json true
  end

  def schedule_to_cab
    @trip.update_attribute :cab, true
    @trip.update_attribute :run, nil
    response_as_json true
  end

  def schedule_to_run
    if !validate_time_availability
      response_as_json false, TranslationEngine.translate_text(:not_fit_in_run_schedule)
    elsif !validate_vehicle_availability 
      response_as_json false, TranslationEngine.translate_text(:vehicle_unavailable)
    elsif !validate_driver_availability 
      response_as_json false, TranslationEngine.translate_text(:driver_unavailable)
    else
      @trip.cab = false
      @trip.run = @run
      response_as_json @trip.save, @trip.errors.full_messages.join(';')
    end
  end

  # run avaiability validations

  # run can hold a trip
  def validate_time_availability
    run_start_time = @run.scheduled_start_time
    run_end_time = @run.scheduled_end_time

    if run_start_time && run_end_time
      (to_utc_time_only(@trip.pickup_time) >= to_utc_time_only(run_start_time)) && 
      (@trip.appointment_time.nil? || to_utc_time_only(@trip.appointment_time) <= to_utc_time_only(run_end_time))
    else
      true
    end
  end

  def validate_vehicle_availability
    @run.vehicle && @run.vehicle.active
  end

  def validate_driver_availability
    @run.driver && @run.driver.active
  end

  def response_as_json(is_success, error_text = '')
    {
      success: is_success,
      message: error_text || '',
      trip_event_json: is_success ? @trip.as_run_event_json : nil
    }
  end

  private

  def to_utc_time_only(time)
    time.utc.strftime("%H%M%S%N") if time
  end

end
{'fraction_non_alphanumeric': 0.07740004276245456, 'fraction_numerical': 0.0, 'mean_word_length': 3.392488262910798, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '792374', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1608, 'n_words': 312}