class Hygien::ConnectionManager
  def initialize(config = {})
    @config = config
    @connection_settings = @config[:db_config][:connection]
    establish_connection
  end

  def self.supported_adapters
    [
     {
       name: "mysql2",
       identifiers: %w{ mysql mysql2 },
       must_keys: [:database, :username, :password],
       orm: "active_record"
     },
     {
       name: "postgresql",
       identifiers: %w{ postgresql psql },
       must_keys: [:database, :username, :password],
       orm: "active_record"
     },
     {
       name: "sqlite3",
       identifiers: %w{ sqlite sqlite3 },
       must_keys: [:database],
       orm: "active_record"
     },
    ]
  end

  def current_adapter_details
    self
      .class
      .supported_adapters
      .select { |adapter| adapter[:name].include? @connection_settings[:adapter]  }
      .first
  end
  
  def establish_connection
    @handle = ("Hygien::ConnectionManager::" + current_adapter_details[:orm]
               .classify)
               .constantize
               .new(@config)
  end

  def connected?
    @handle.connected?
  end

  def disconnect!
    @handle.disconnect!
  end

  def sanitize!
    @handle.start_sanitization!
  end
end
