# typed: strict
# frozen_string_literal: true

require "simplecov"
require "simplecov-lcov"

SimpleCov::Formatter::LcovFormatter.config do |c|
  c.report_with_single_file = true
  c.lcov_file_name = "lcov.info"
end

SimpleCov.start do
  enable_coverage :branch
  add_filter "spec"
  formatter(
    SimpleCov::Formatter::MultiFormatter.new(
      [
        SimpleCov::Formatter::LcovFormatter, # Add Lcov as an output when generating code coverage report
        SimpleCov::Formatter::HTMLFormatter # Add other outputs for the code coverage report
      ]
    )
  )
end

require "pry"
require "active_record"
require "active_record/errors"
require "has_aux_table"

# Configure ActiveRecord to use in-memory SQLite database
ActiveRecord::Base.establish_connection(
  adapter: "sqlite3",
  database: ":memory:"
)

require_relative "spec_models"

RSpec.configure do |config|
  # Enable flags like --only-failures and --next-failure
  config.example_status_persistence_file_path = ".rspec_status"

  # Disable RSpec exposing methods globally on `Module` and `main`
  config.disable_monkey_patching!

  config.backtrace_inclusion_patterns = [/\bactiverecord\b/]

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

  config.around(:each) do |example|
    ActiveRecord::Base.transaction do
      example.run
      raise ActiveRecord::Rollback
    end
  end
end

module SpecHelper
  extend T::Sig
  extend T::Helpers
  LOG_QUERIES = T.let(false, T::Boolean)

  # Helper method to count queries
  sig { params(block: T.proc.void).returns(Integer) }
  def self.count_queries(&block)
    query_count = 0
    query_callback =
      lambda { |name, start, finish, message_id, values| query_count += 1 }

    ActiveSupport::Notifications.subscribed(
      query_callback,
      "sql.active_record"
    ) do
      if LOG_QUERIES
        old_logger = ActiveRecord::Base.logger
        ActiveRecord::Base.logger = Logger.new(STDOUT)
        ActiveRecord::Base.logger.level = Logger::DEBUG
      end
      block.call
      ActiveRecord::Base.logger = old_logger if LOG_QUERIES
    end

    query_count
  end
end
