<commit_before>
# can be used both for searching flight or booking flight
class Kynort::Flights::Query
  attr_writer :flight_key
  def flight_key
    # flight key is separated from each other by 5 dots
    fk_dotted = ""
    @flight_key.each { |each_fk| fk_dotted << "#{each_fk}....." }
    fk_dotted
  end
  attr_writer :use_cache
  def use_cache
    if @use_cache
      return 1
    else
      return 0
    end
  end

  attr_accessor :depart, :arrival
  attr_accessor :from, :to
  attr_accessor :adult, :child, :infant
  attr_accessor :captcha

  attr_accessor :agent_first_name
  attr_accessor :agent_middle_name # optional
  attr_accessor :agent_last_name # optional
  attr_accessor :agent_comp_name
  attr_accessor :agent_comp_addr
  attr_accessor :agent_comp_npwp # optional
  attr_accessor :agent_comp_fax # optional
  attr_accessor :agent_comp_phone
  attr_accessor :agent_comp_email

  # only adult passenger can be given right to be a contact person (0-indexed)
  # attr_accessor :contact_who
  # attr_accessor :contact_hp
  attr_accessor :contact_email

  attr_accessor :use_insurance

  attr_reader :passengers

  def initialize
    self.child = 0
    self.infant = 0
    @passengers ||= []
    @flight_key ||= []

    super
  end

  def is_searching?
    (self.flight_key.blank? || self.passengers.blank?)
  end

  def validate!
    # automatically set use_cache to false, if booking
    if passengers.any? && flight_key.any?
      self.use_cache = false
    end
    raise "use_cache cannot be nil/blank, it must be either true or false" unless @use_cache.is_a?(TrueClass) || @use_cache.is_a?(FalseClass)

    validate_journey
    # only validate agent and contact and passengers while not on pick request,
    # search request no need to fill in those values
    unless is_searching?
      validate_agent
      validate_contact
      validate_passengers
    end

    nil
  end

  def add_passenger(passenger)
    raise "passenger must be an instance of Kynort::Flights::Passenger" unless passenger.is_a?(Kynort::Flights::Passenger)
    @passengers ||= []
    @passengers << passenger
  end

  def to_hash
    # validate first
    validate!
    raise "flight key cannot be nil/blank" if (flight_key.nil? || flight_key.blank?) && !is_searching?

    data = {
        access_token: Kynort.token,

        flight_key: flight_key,
        use_cache: use_cache,
        depart: depart,
        arrival: arrival,
        from: from,
        adult: adult,
        child: child,
        infant: infant,

        agent_fn: agent_first_name,
        agent_mn: agent_middle_name,
        agent_ln: agent_last_name,
        agent_comp_name: agent_comp_name,
        agent_comp_addr: agent_comp_addr,<commit_msg>is_searching?<commit_after>
# can be used both for searching flight or booking flight
class Kynort::Flights::Query
  attr_writer :flight_key
  def flight_key
    # flight key is separated from each other by 5 dots
    fk_dotted = ""
    @flight_key.each { |each_fk| fk_dotted << "#{each_fk}....." }
    fk_dotted
  end
  attr_writer :use_cache
  def use_cache
    if @use_cache
      return 1
    else
      return 0
    end
  end

  attr_accessor :depart, :arrival
  attr_accessor :from, :to
  attr_accessor :adult, :child, :infant
  attr_accessor :captcha

  attr_accessor :agent_first_name
  attr_accessor :agent_middle_name # optional
  attr_accessor :agent_last_name # optional
  attr_accessor :agent_comp_name
  attr_accessor :agent_comp_addr
  attr_accessor :agent_comp_npwp # optional
  attr_accessor :agent_comp_fax # optional
  attr_accessor :agent_comp_phone
  attr_accessor :agent_comp_email

  # only adult passenger can be given right to be a contact person (0-indexed)
  # attr_accessor :contact_who
  # attr_accessor :contact_hp
  attr_accessor :contact_email

  attr_accessor :use_insurance

  attr_reader :passengers

  def initialize
    self.child = 0
    self.infant = 0
    @passengers ||= []
    @flight_key ||= []

    super
  end

  def is_searching?
    (@flight_key.blank? || self.passengers.blank?)
  end

  def validate!
    # automatically set use_cache to false, if booking
    if passengers.any? && @flight_key.any?
      self.use_cache = false
    end
    raise "use_cache cannot be nil/blank, it must be either true or false" unless @use_cache.is_a?(TrueClass) || @use_cache.is_a?(FalseClass)

    validate_journey
    # only validate agent and contact and passengers while not on pick request,
    # search request no need to fill in those values
    unless is_searching?
      validate_agent
      validate_contact
      validate_passengers
    end

    nil
  end

  def add_passenger(passenger)
    raise "passenger must be an instance of Kynort::Flights::Passenger" unless passenger.is_a?(Kynort::Flights::Passenger)
    @passengers ||= []
    @passengers << passenger
  end

  def to_hash
    # validate first
    validate!
    raise "flight key cannot be nil/blank" if (@flight_key.nil? || @flight_key.blank?) && !is_searching?

    data = {
        access_token: Kynort.token,

        flight_key: flight_key,
        use_cache: use_cache,
        depart: depart,
        arrival: arrival,
        from: from,
        adult: adult,
        child: child,
        infant: infant,

        agent_fn: agent_first_name,
        agent_mn: agent_middle_name,
        agent_ln: agent_last_name,
        agent_comp_name: agent_comp_name,
        agent_comp_addr: agent_comp_addr,
{'fraction_non_alphanumeric': 0.07449751060298727, 'fraction_numerical': 0.0022127973446431862, 'mean_word_length': 3.1787365177195688, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, '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': '2739425', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1744, 'n_words': 481}