default_platform(:ios)

def app_store_api_key
  issuer_id = ENV["APP_STORE_CONNECT_ISSUER_ID"].to_s.strip
  app_store_connect_api_key(
    key_id: ENV.fetch("APP_STORE_CONNECT_API_KEY_ID"),
    issuer_id: issuer_id.empty? ? nil : issuer_id,
    key_content: ENV.fetch("APP_STORE_CONNECT_API_PRIVATE_KEY_BASE64"),
    is_key_content_base64: true,
    duration: 1200,
    in_house: false
  )
end

platform :ios do
  desc "Validate and upload the signed IPA, then wait for App Store Connect processing"
  lane :upload_internal do
    api_key = app_store_api_key
    upload_to_testflight(
      api_key: api_key,
      app_identifier: ENV.fetch("IOS_BUNDLE_ID"),
      ipa: ENV.fetch("IOS_IPA_PATH"),
      changelog: ENV.fetch("TESTFLIGHT_WHAT_TO_TEST", "Automatisk intern build fra verificeret master."),
      distribute_external: false,
      notify_external_testers: false,
      skip_submission: true,
      skip_waiting_for_build_processing: false,
      wait_processing_interval: 30,
      reject_build_waiting_for_review: false
    )
  end

  desc "Create/update the Denmark App Store candidate without submitting it for review"
  lane :prepare_candidate do
    api_key = app_store_api_key
    deliver(
      api_key: api_key,
      app_identifier: ENV.fetch("IOS_BUNDLE_ID"),
      app_version: ENV.fetch("IOS_MARKETING_VERSION"),
      build_number: ENV.fetch("IOS_BUILD_NUMBER"),
      metadata_path: "fastlane/metadata",
      screenshots_path: "fastlane/screenshots",
      skip_binary_upload: true,
      skip_metadata: false,
      skip_screenshots: false,
      overwrite_screenshots: true,
      force: true,
      submit_for_review: false,
      automatic_release: true,
      phased_release: false,
      run_precheck_before_submit: false,
      precheck_include_in_app_purchases: false
    )
  end
end
