# Load environment variables from .env files
def load_env_file(file_path)
  if File.exist?(file_path)
    puts "📄 Loading #{File.basename(file_path)}..."
    File.readlines(file_path).each do |line|
      line = line.strip
      next if line.empty? || line.start_with?('#')
      key, value = line.split('=', 2)
      ENV[key] = value if key && value
    end
  end
end

# Load .env files in order of precedence
load_env_file('../.env')
load_env_file('../.env.local')

# Enhanced logging for architecture detection
puts "=" * 50
puts "🏗️  React Native Architecture Configuration"
puts "-" * 50

# Set the architecture based on ITBL_NEW_ARCH
new_arch_value = ENV['ITBL_NEW_ARCH'] || 'true'
# Convert 'true'/'false' to '1'/'0' for RCT_NEW_ARCH_ENABLED
rct_new_arch_enabled = (new_arch_value == 'true' || new_arch_value == '1') ? '1' : '0'
ENV['RCT_NEW_ARCH_ENABLED'] = rct_new_arch_enabled

puts "🔍 New Architecture enabled: #{rct_new_arch_enabled == '1'}"
puts "=" * 50

ENV['USE_HERMES'] = '1'


# Resolve react_native_pods.rb with node to allow for hoisting
require Pod::Executable.execute_command('node', ['-p',
  'require.resolve(
    "react-native/scripts/react_native_pods.rb",
    {paths: [process.argv[1]]},
  )', __dir__]).strip

platform :ios, min_ios_version_supported
prepare_react_native_project!

linkage = ENV['USE_FRAMEWORKS']
if linkage != nil
  Pod::UI.puts "Configuring Pod with #{linkage}ally linked Frameworks".green
  use_frameworks! :linkage => :dynamic
end

target 'ReactDev' do
  config = use_native_modules!

  use_react_native!(
    :path => config[:reactNativePath],
    # An absolute path to your application root.
    :app_path => "#{Pod::Config.instance.installation_root}/..",
    :hermes_enabled => true
  )


  # Uncomment the following line if you want to develop using your local ios sdk
  # pod 'Iterable-iOS-SDK', :path => '../../../ios/iterable-swift-sdk'

  post_install do |installer|
    # https://github.com/facebook/react-native/blob/main/packages/react-native/scripts/react_native_pods.rb#L197-L202
    react_native_post_install(
      installer,
      config[:reactNativePath],
      :mac_catalyst_enabled => false,
      # :ccache_enabled => true,
    )
  end
end


target 'Rich Notification Extension' do
  pod 'Iterable-iOS-AppExtensions'
  # If you want to develop using your local ios sdk, uncomment the following
  # line and comment out the one above.
  # pod 'Iterable-iOS-AppExtensions', :path => '../../../ios/iterable-swift-sdk'
end