# frozen_string_literal: true

if ENV["CC_REPORT"]
  require "simplecov"
  SimpleCov.start do
    add_filter "/spec/"
  end
end

require "active_record"
require "sequel"
require "clowne"
require "factory_bot"

begin
  require "pry-byebug"
rescue LoadError
end

%w[active_record sequel].each do |orm|
  require_relative "./support/#{orm}/initializer.rb"
end

Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].sort.each { |f| require f }

Clowne.default_adapter = :base

RSpec.configure do |config|
  config.example_status_persistence_file_path = ".rspec_status"
  config.filter_run focus: true
  config.run_all_when_everything_filtered = true

  config.order = :random

  config.expect_with :rspec do |c|
    c.syntax = :expect
  end

  config.include FactoryBot::Syntax::Methods
  config.include_context "adapter:active_record", adapter: :active_record
  config.include_context "adapter:sequel", adapter: :sequel

  config.before(:each) do
    Clowne::Utils::Operation.clear!
  end

  config.before(:each, cleanup: true) do
    cleanup(ActiveRecord::Base, &:delete_all)

    cleanup(Sequel::Model) do |sequel_class|
      sequel_class.dataset.delete
    end
  end

  def cleanup(base_class)
    base_class.subclasses.each do |orm_class|
      yield(orm_class)
      orm_class.remove_instance_variable(:@_clowne_cloner) if
        orm_class.instance_variable_defined?(:@_clowne_cloner)
    end
  end

  config.after(:each, cleandb: true) do
    ActiveRecord::Base.subclasses.each(&:delete_all)
  end
end
