#This class represents a best time to call record and includes related database methods

require 'mysql'

class ArDetailCode

  #TODO: put the database connection info in a config file.
  HOST = 'localhost'
  USER = 'root'
  DBPASSWORD = 'Agilexadmin99$'
  SCHEMA = 'HADB'

  attr_accessor :ar_detail_code_id
  attr_accessor :detail_code_id
  attr_accessor :appointment_request_id
  attr_accessor :created_date
  attr_accessor :user_id

  def findByAppointmentRequestId(ar_id)

    #create the sql statement from the incoming target_date
    begin
      con = Mysql.new(HOST, USER, DBPASSWORD, SCHEMA)
      # con = Mysql.new(dbconfig.host, dbconfig.username, dbconfig.password, dbconfig.database)
      request_string = "SELECT * FROM HADB.AR_DETAIL_CODE where APPOINTMENT_REQUEST_ID = '#{ar_id}';"
      puts "request_string: #{request_string}"
      rs = con.query(request_string)
      rs.each { |result|
        puts "#{result}"
        # rcd =
        self.populateFromDb(result)}
      con.close

    rescue Exception => e
      puts "MySQL Exception: " + e.to_s

    ensure
      puts "select statement executed"
    end

    # return rcd

  end


  # def populateDefaultTestRecord()
  #
  #   self.appointment_request_id ='VAMC3AR000006' #THIS IS THE SAME ID AS THE TEST AR POPULATE METHOD WHICH MUST BE KEPT IN SYNC
  #   self.best_time = '9 AM - 11 AM'
  #
  #   self.insertRecord(self)
  #   puts "inserted record: /n#{self.findAppointmentRequestById(self.appointment_request_id)}"
  #
  # end


  def populateFromDb(result)

    self.ar_detail_code_id = result[0]
    self.detail_code_id = result[1]
    self.appointment_request_id = result[2]
    self.created_date = result[3]
    self.user_id = result[4]

    puts "  -values from db: [ar_dtl_code => #{ar_detail_code_id}, dtl_code => #{detail_code_id},
appt_rqst => #{appointment_request_id}, created_dt => #{created_date}, user => #{user_id}]"

  end

#   def insertRecord(target_record)
#     #create the insert statement and post to the db
#     begin
#       con = Mysql.new(HOST, USER, DBPASSWORD, SCHEMA)
#       # con = Mysql.new(dbconfig.host, dbconfig.username, dbconfig.password, dbconfig.database)
#       insert_statement = "INSERT INTO `BEST_TIME_TO_CALL`
# (`APPOINTMENT_REQUEST_ID`,
# `BEST_TIME`)
# VALUES ('#{target_record.appointment_request_id}',
# '#{target_record.best_time}'
# );"
#
#       puts "insert_statement: #{insert_statement}"
#       rs = con.query(insert_statement)
#       rs.each { |result| puts "#{result}" }
#       con.close
#
#     rescue Exception=>e
#       puts "MySQL Exception: " + e.to_s
#
#     ensure
#       puts "insert statement executed"
#     end
#
#   end

  def deleteRecordById(record_id)
    #create the sql statement from the incoming target_date
    begin
      con = Mysql.new(HOST, USER, DBPASSWORD, SCHEMA)
      # con = Mysql.new(dbconfig.host, dbconfig.username, dbconfig.password, dbconfig.database)
      rs = con.query("Delete FROM HADB.AR_DETAIL_CODE where AR_DETAIL_CODE_ID= '#{record_id}';")
      rs.each { |result|
        puts "#{result}"}
      con.close

    rescue Exception => e
      puts "MySQL Exception: " + e.to_s

    ensure
      puts "delete statement executed"
    end
  end

  def deleteRecordByAppointmentRequestId(appointment_request_id)
    self.findByAppointmentRequestId(appointment_request_id)
    puts "record id: #{self.ar_detail_code_id}; appt_rqst_id: #{self.appointment_request_id}"
    self.deleteRecordById(self.ar_detail_code_id)
  end
end