platform :ios, '13.0'
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

project 'Runner', { 'Debug' => :debug, 'Profile' => :release, 'Release' => :release }

def flutter_root
  generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
  unless File.exist?(generated_xcode_build_settings_path)
    raise "#{generated_xcode_build_settings_path} must exist."
  end
  File.foreach(generated_xcode_build_settings_path) do |line|
    matches = line.match(/FLUTTER_ROOT\=(.*)/)
    return matches[1].strip if matches
  end
  raise "FLUTTER_ROOT not found"
end

require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)
flutter_ios_podfile_setup

target 'Runner' do
  use_frameworks!
  use_modular_headers!
  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
  target 'RunnerTests' do
    inherit! :search_paths
  end
end

target 'PushService' do
  use_frameworks!
  inherit! :search_paths

  pod 'Altcraft', '1.0.3'
end

require 'cocoapods/installer/xcode/target_validator'
Pod::Installer::Xcode::TargetValidator.send(:define_method, :verify_no_static_framework_transitive_dependencies) {}

post_install do |installer|
  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)
    target.build_configurations.each do |config|
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '13.0'
    end
  end

  # Disable strict concurrency for ALL targets
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['SWIFT_STRICT_CONCURRENCY'] = 'Disabled'
    end
  end

  # Fix duplicate symbols: remove direct Altcraft linkage from Runner
  # so it only gets Altcraft transitively through altcraft_sdk.
  runner_xcconfig_dir = File.expand_path('Pods/Target Support Files/Pods-Runner', __dir__)
  Dir.glob(File.join(runner_xcconfig_dir, 'Pods-Runner.*.xcconfig')).each do |xcconfig_file|
    content = File.read(xcconfig_file)
    content = content.gsub(/-framework "Altcraft" /, '')
    content = content.gsub(/-framework Altcraft /, '')
    File.write(xcconfig_file, content)
  end

  installer.aggregate_targets.each do |aggregate_target|
    aggregate_target.user_project.targets.each do |target|
      target.build_configurations.each do |config|
        config.build_settings['SWIFT_STRICT_CONCURRENCY'] = 'Disabled'
      end
    end
  end

  # Fix objectVersion in Pods.xcodeproj: xcodeproj gem writes 70 which it can't read back.
  # Patch the saved pbxproj to use 77 (Xcode 16.0).
  pods_pbxproj = File.expand_path('Pods/Pods.xcodeproj/project.pbxproj', __dir__)
  if File.exist?(pods_pbxproj)
    content = File.read(pods_pbxproj)
    content = content.gsub(/objectVersion = \d+;/, 'objectVersion = 77;')
    File.write(pods_pbxproj, content)
  end

  # Disable sandbox for PushService so CocoaPods resource scripts can run
  require 'xcodeproj'
  main_project = Xcodeproj::Project.open(File.expand_path('Runner.xcodeproj', __dir__))
  main_project.targets.each do |target|
    next unless target.name == 'PushService'
    target.build_configurations.each do |config|
      config.build_settings['ENABLE_USER_SCRIPT_SANDBOXING'] = 'NO'
    end
  end
  main_project.save
end
