# 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

def using_https_git_auth?
  # Check if gh binary exists before attempting to use it
  return false unless system("which gh > /dev/null 2>&1")

  begin
    auth_data = `gh auth status 2>&1`
    auth_data.include?("Logged in to github.com account") &&
      auth_data.include?("Git operations protocol: https")
  rescue Errno::ENOENT, Errno::EACCES => e
    puts "gh auth status failed, assuming no HTTPS auth -- will try SSH"
    puts "  Reason: #{e.message}"
    false
  end
end

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 => linkage.to_sym
end

target "SelfDemoApp" 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}/..",
    :fabric_enabled => false,
    :hermes_enabled => true,
  )

  # Use the custom NFCPassportReader fork
  is_selfxyz_repo = ENV["GITHUB_REPOSITORY"]&.start_with?("selfxyz/") || ENV["GITHUB_REPOSITORY"].nil?

  nfc_repo_url = if !is_selfxyz_repo
      puts "📦 Using public NFCPassportReader for external fork (#{ENV["GITHUB_REPOSITORY"]})"
      "https://github.com/PLACEHOLDER/NFCPassportReader.git"
    elsif ENV["GITHUB_ACTIONS"] == "true"
      # CI: NEVER embed credentials in URLs. Rely on workflow-provided auth via:
      # - ~/.netrc or a Git credential helper, and token masking in logs.
      "https://github.com/selfxyz/NFCPassportReader.git"
    elsif using_https_git_auth?
      # Local development with HTTPS GitHub auth via gh - use HTTPS to private repo
      "https://github.com/selfxyz/NFCPassportReader.git"
    else
      # Local development in selfxyz repo - use SSH to private repo
      puts "📦 Using SSH for private NFCPassportReader (local selfxyz development)"
      "git@github.com:selfxyz/NFCPassportReader.git"
    end

  # pod "NFCPassportReader", git: nfc_repo_url, commit: "04ede227cbfd377e2b4bc9b38f9a89eebdcab52f"
  # pod "NFCPassportReader", :git => "git@github.com:selfxyz/NFCPassportReader.git", :commit => "9eff7c4e3a9037fdc1e03301584e0d5dcf14d76b"

  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
    )

    # Add Swift module search paths for NFCPassportReader embedded in SelfSDK
    # installer.pods_project.targets.each do |target|
    #   if target.name == 'mobile-sdk-alpha'
    #     target.build_configurations.each do |config|
    #       xcframework_path = "$(PODS_ROOT)/../../mobile-sdk-alpha/ios/Frameworks/NFCPassportReader.xcframework"
    #       modules_path_device = "#{xcframework_path}/ios-arm64/SelfSDK.framework/Modules"
    #       modules_path_sim = "#{xcframework_path}/ios-arm64_x86_64-simulator/SelfSDK.framework/Modules"

    #       # Add module search paths
    #       config.build_settings['OTHER_SWIFT_FLAGS'] ||= ['$(inherited)']
    #       config.build_settings['OTHER_SWIFT_FLAGS'] << "-I#{modules_path_device}"
    #       config.build_settings['OTHER_SWIFT_FLAGS'] << "-I#{modules_path_sim}"
    #     end
    #   end
    # end
  end
end
