##
# You can use other adapters like:
#
#   ActiveRecord::Base.configurations[:development] = {
#     :adapter   => 'mysql2',
#     :encoding  => 'utf8',
#     :reconnect => true,
#     :database  => 'your_database',
#     :pool      => 5,
#     :username  => 'root',
#     :password  => '',
#     :host      => 'localhost',
#     :socket    => '/tmp/mysql.sock'
#   }
#

=begin
ActiveRecord::Base.configurations[:development] = {
  :adapter => 'sqlite3',
  :database => Padrino.root('db', 'buildwise_development.db')

}


ActiveRecord::Base.configurations[:test] = {
  :adapter => 'sqlite3',
  :database => Padrino.root('db', 'buildwise_test.db')

}

ActiveRecord::Base.configurations[:production] = {
  :adapter => 'mysql2',
  :encoding  => 'utf8',
  :reconnect => true,
  :host => 'localhost',
  :database  => 'buildwise_production',
  :pool      => 5,
  :username  => 'root',
  :password  => '',  
}
=end


if ActiveRecord.version.release() < Gem::Version.new('6.0')
  YAML.load_file("#{Padrino.root}/config/database.yml").each do |env, conf|
      ActiveRecord::Base.configurations[env.to_sym] = conf.symbolize_keys
  end
else
  require 'erb'
  db_yml_template = File.read "#{Padrino.root}/config/database.yml"
  db_yml_content = ERB.new(db_yml_template).result(binding)
  ActiveRecord::Base.configurations = YAML.load(db_yml_content).to_hash
  # ActiveRecord::Base.configurations = YAML.load_file("#{Padrino.root}/config/database.yml").to_hash
end


# Setup our logger
ActiveRecord::Base.logger = logger

if ActiveRecord::VERSION::MAJOR.to_i < 4
  # Raise exception on mass assignment protection for Active Record models.
  ActiveRecord::Base.mass_assignment_sanitizer = :strict

  # Log the query plan for queries taking more than this (works
  # with SQLite, MySQL, and PostgreSQL).
  ActiveRecord::Base.auto_explain_threshold_in_seconds = 0.5
end

# Doesn't include Active Record class name as root for JSON serialized output.
ActiveRecord::Base.include_root_in_json = false

# Store the full class name (including module namespace) in STI type column.
ActiveRecord::Base.store_full_sti_class = true

# Use ISO 8601 format for JSON serialized times and dates.
ActiveSupport.use_standard_json_time_format = true

# Don't escape HTML entities in JSON, leave that for the #json_escape helper
# if you're including raw JSON in an HTML page.
ActiveSupport.escape_html_entities_in_json = false

# Now we can establish connection with our db.
if ActiveRecord::Base.configurations.respond_to?("configs_for")
  ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations.configs_for(env_name: Padrino.env.to_s).first)
else
  # Pre ActiveRecord 6.1
  ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[Padrino.env])  
end

# Timestamps are in the utc by default.
# the below deprecated in Rails 7.1
# ActiveRecord::Base.default_timezone = :utc

ActiveRecord.default_timezone = :utc
