#This class represents a patient and includes related database methods

require 'mysql'

class Patient

   attr_accessor :patient_id
   attr_accessor :first_name
   attr_accessor :last_name
   attr_accessor :ssn
   attr_accessor :date_of_birth
   attr_accessor :text_messaging_allowed


   HOST = 'localhost'
   USER = 'root'
   DBPASSWORD = 'Agilexadmin99$'
   SCHEMA = 'VARDB'
  def populatePatient()

    self.patient_id ='P001'
    self.first_name = 'patient'
    self.last_name = 'onedemo'
    self.ssn = '666000001'
    self.date_of_birth = '1935-04-07 00:00:00'
    self.text_messaging_allowed = 0

  end

  def populateAndSavePatient()
    self.populatePatient
    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)
       insert_statement = "INSERT INTO `PATIENT`
(`PATIENT_ID`,
`FIRST_NAME`,
`LAST_NAME`,
`SSN`,
`DATE_OF_BIRTH`,
`TEXT_MESSAGING_ALLOWED`,
`TEXT_MESSAGING_PHONE_NUMBER`)
VALUES ('#{target_record.patient_id}',
'#{target_record.first_name}',
'#{target_record.last_name}',
'#{target_record.ssn}',
'#{target_record.date_of_birth}',
#{target_record.text_messaging_allowed},
NULL
);"
       rs = con.query(insert_statement)
       con.close

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

     ensure
       puts "insert patient 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.PATIENT WHERE PATIENT_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