require "rspec_power/version"
require "rspec_power/engine"

# because some commands loads gem, before we need, for example RAILS_ENV=test bundle exec rails db:schema:load
return unless defined?(RSpec) && RSpec.respond_to?(:shared_context)

require "rspec_power/logging"
require "rspec_power/env"
require "rspec_power/i18n"
require "rspec_power/time"
require "rspec_power/request_dump"
require "rspec_power/db_dump"
require "rspec_power/ci"
require "rspec_power/sql"
require "rspec_power/performance"
require "rspec_power/benchmark"

RSpec.configure do |config|
  # Logging
  config.include RSpecPower::LoggingHelpers
  config.include_context "rspec_power::logging:verbose", with_log: true
  config.include_context "rspec_power::logging:verbose", with_logs: true
  config.include_context "rspec_power::logging:active_record", with_log_ar: true

  # Environment variable overrides
  config.include RSpecPower::EnvHelpers
  config.include_context "rspec_power::env:override", :with_env

  # I18n
  config.include RSpecPower::I18nHelpers
  config.include_context "rspec_power::i18n:dynamic", :with_locale

  # Time manipulation
  config.include RSpecPower::TimeHelpers
  config.include_context "rspec_power::time:freeze", :with_time_freeze
  config.include_context "rspec_power::time:zone", :with_time_zone

  # CI-only guards
  # Prefer new tags with "with_" prefix; keep old ones for compatibility
  config.include_context "rspec_power::ci:only", :with_ci_only
  config.include_context "rspec_power::ci:skip", :with_skip_ci
  config.include_context "rspec_power::ci:only", :ci_only
  config.include_context "rspec_power::ci:skip", :skip_ci

  # SQL guards
  config.include RSpecPower::ActiveRecordHelpers
  config.include_context "rspec_power::sql:none", :with_no_sql_queries
  config.include_context "rspec_power::sql:must", :with_sql_queries

  # Performance
  config.include RSpecPower::PerformanceHelpers
  config.include_context "rspec_power::performance:maximum_execution_time", :with_maximum_execution_time

  # Benchmark
  config.include_context "rspec_power::benchmark:run", :with_benchmark

  # Request dump helpers (session/cookies/flash/headers)
  config.include RSpecPower::RequestDumpHelpers
  config.include_context "rspec_power::request_dump:after", :with_request_dump

  # DB dump on failure
  # Prefer :with_dump_db_on_fail; keep :dump_db_on_fail for backward compatibility
  config.include_context "rspec_power::db_dump:on_fail", :with_dump_db_on_fail
  config.include_context "rspec_power::db_dump:on_fail", :dump_db_on_fail
end
