#This class represents an appointment request and includes related database methods
#TODO: replace the elements and methods in this class and use a real ORM gem as this class is a hack

require 'mysql'
# require 'yaml'

class AppointmentRequest #< ActiveRecord::Base

   attr_accessor :appointment_request_id
   attr_accessor :patient_id
   attr_accessor :last_updated_date
   attr_accessor :created_date
   attr_accessor :deleted_date
   attr_accessor :active
   attr_accessor :second_request
   attr_accessor :appointment_date
   attr_accessor :appointment_time
   attr_accessor :option_date_1
   attr_accessor :option_time_1
   attr_accessor :option_date_2
   attr_accessor :option_time_2
   attr_accessor :option_date_3
   attr_accessor :option_time_3
   attr_accessor :status
   attr_accessor :appointment_type
   attr_accessor :facility_code
   attr_accessor :email
   attr_accessor :phone_number
   attr_accessor :text_messaging_allowed
   attr_accessor :text_messaging_phone_number
   attr_accessor :purpose_of_visit
   attr_accessor :other_purpose_of_visit
   attr_accessor :visit_type
   attr_accessor :provider_id
   attr_accessor :provider_name
   attr_accessor :provider_person_class
   attr_accessor :provider_option
   attr_accessor :second_request_submitted
   attr_accessor :parent_request_id
   attr_accessor :has_veteran_new_message
   attr_accessor :has_provider_new_message
   attr_accessor :provider_seen_appt_req
   attr_accessor :requested_phone_call


   HOST = 'localhost'
   USER = 'root'
   DBPASSWORD = 'Agilexadmin99$'
   SCHEMA = 'VARDB'

  def populateDefaultAppointmentRequest()
    
    # self.appointment_request_id ='00000000000000000000000000000910'
    self.appointment_request_id ='402880eb45b8fff30145b9338fb00004'
    # self.patient_id = 'P004' #ssn: 666-00-0004
    self.patient_id = 'P001' #ssn: 666-00-0001
    self.last_updated_date = DateTime.now.strftime('%Y-%m-%d %H:%M:%S')
    self.created_date = DateTime.now.strftime('%Y-%m-%d %H:%M:%S')
    self.deleted_date = 'NULL' #DateTime.now.strftime('%Y-%m-%d %H:%M:%S') #"null" #nil
    self.active = 1
    self.second_request = 0
    self.appointment_date = ''
    self.appointment_time = ''
    self.option_date_1 = (DateTime.now + 10).strftime('%m/%d/%Y')
    self.option_time_1 = 'PM'
    self.option_date_2 = 'No Date Selected'
    self.option_time_2 = 'No Time Selected'
    self.option_date_3 = 'No Date Selected'
    self.option_time_3 = 'No Time Selected'
    self.status = 'Submitted'
    self.appointment_type = 'Primary Care'
    self.facility_code = '777'
    self.email = ''
    self.phone_number = '444-444-4444'
    self.text_messaging_allowed = 0
    self.text_messaging_phone_number = ''
    self.purpose_of_visit = 'New Issue'
    self.other_purpose_of_visit = ''
    self.visit_type = 'Phone Call'
    self.provider_id = 'PROV1'
    self.provider_name = 'zztest appointment_provider1'
    self.provider_person_class = ''
    self.provider_option = 'NULL'
    self.second_request_submitted = 0
    self.parent_request_id = 'NULL'
    self.has_veteran_new_message = 0
    self.has_provider_new_message = 0
    self.provider_seen_appt_req = 0
    self.requested_phone_call = 1

  end

  def populateAndSaveDefaultAppointmentRequest()
    self.populateDefaultAppointmentRequest
    self.insertRecord(self)
  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 `APPOINTMENT_REQUEST`
(`APPOINTMENT_REQUEST_ID`,
`PATIENT_ID`,
`LAST_UPDATED_DATE`,
`CREATED_DATE`,
`DELETED_DATE`,
`ACTIVE`,
`SECOND_REQUEST`,
`APPOINTMENT_DATE`,
`APPOINTMENT_TIME`,
`OPTION_DATE_1`,
`OPTION_TIME_1`,
`OPTION_DATE_2`,
`OPTION_TIME_2`,
`OPTION_DATE_3`,
`OPTION_TIME_3`,
`STATUS`,
`APPOINTMENT_TYPE`,
`FACILITY_CODE`,
`EMAIL`,
`PHONE_NUMBER`,
`TEXT_MESSAGING_ALLOWED`,
`TEXT_MESSAGING_PHONE_NUMBER`,
`PURPOSE_OF_VISIT`,
`OTHER_PURPOSE_OF_VISIT`,
`VISIT_TYPE`,
`PROVIDER_ID`,
`PROVIDER_NAME`,
`PROVIDER_PERSON_CLASS`,
`PROVIDER_OPTION`,
`SECOND_REQUEST_SUBMITTED`,
`PARENT_REQUEST_ID`,
`HAS_VETERAN_NEW_MESSAGE`,
`HAS_PROVIDER_NEW_MESSAGE`,
`PROVIDER_SEEN_APPT_REQ`,
`REQUESTED_PHONE_CALL`)
VALUES ('#{target_record.appointment_request_id}',
'#{target_record.patient_id}',
'#{target_record.last_updated_date}',
'#{target_record.created_date}',
#{target_record.deleted_date},
#{target_record.active},
#{target_record.second_request},
'#{target_record.appointment_date}',
'#{target_record.appointment_time}',
'#{target_record.option_date_1}',
'#{target_record.option_time_1}',
'#{target_record.option_date_2}',
'#{target_record.option_time_2}',
'#{target_record.option_date_3}',
'#{target_record.option_time_3}',
'#{target_record.status}',
'#{target_record.appointment_type}',
'#{target_record.facility_code}',
'#{target_record.email}',
'#{target_record.phone_number}',
#{target_record.text_messaging_allowed},
'#{target_record.text_messaging_phone_number}',
'#{target_record.purpose_of_visit}',
'#{target_record.other_purpose_of_visit}',
'#{target_record.visit_type}',
'#{target_record.provider_id}',
'#{target_record.provider_name}',
'#{target_record.provider_person_class}',
#{target_record.provider_option},
#{target_record.second_request_submitted},
#{target_record.parent_request_id},
#{target_record.has_veteran_new_message},
#{target_record.has_provider_new_message},
#{target_record.provider_seen_appt_req},
#{target_record.requested_phone_call}
);"
       rs = con.query(insert_statement)
       con.close

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

     ensure
       puts "insert appointment request 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 VARDB.APPOINTMENT_REQUEST where APPOINTMENT_REQUEST_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
end