default_platform(:ios)

def asc_api_key
  app_store_connect_api_key(
    key_id: ENV.fetch("IOS_APP_STORE_CONNECT_API_KEY_ID"),
    issuer_id: ENV.fetch("IOS_APP_STORE_CONNECT_ISSUER_ID"),
    key_filepath: ENV.fetch("IOS_APP_STORE_CONNECT_API_KEY_PATH"),
    duration: 1200,
    in_house: false
  )
end

platform :ios do
  desc "Upload a signed IPA to TestFlight"
  lane :upload_testflight do |options|
    wait_for_processing = ENV.fetch("IOS_WAIT_FOR_PROCESSING", "false") == "true"

    upload_to_testflight(
      api_key: asc_api_key,
      app_identifier: ENV.fetch("IOS_APP_BUNDLE_ID"),
      ipa: options[:ipa] || ENV.fetch("IOS_IPA_PATH"),
      changelog: ENV["IOS_RELEASE_NOTES"],
      distribute_external: false,
      skip_submission: true,
      skip_waiting_for_build_processing: !wait_for_processing
    )
  end

  desc "Submit the current build to App Store review"
  lane :submit_app_store do
    deliver(
      api_key: asc_api_key,
      app_identifier: ENV.fetch("IOS_APP_BUNDLE_ID"),
      apple_id: ENV.fetch("IOS_APP_STORE_APP_ID"),
      app_version: ENV.fetch("IOS_BUILD_NAME"),
      build_number: ENV.fetch("IOS_BUILD_NUMBER"),
      skip_binary_upload: true,
      skip_metadata: true,
      skip_screenshots: true,
      force: true,
      submit_for_review: true,
      automatic_release: ENV.fetch("IOS_AUTO_RELEASE_ON_APPROVAL", "false") == "true",
      precheck_include_in_app_purchases: false,
      run_precheck_before_submit: false
    )
  end
end
