# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
update_fastlane

opt_out_usage
default_platform(:ios)
setup_ci if ENV['CI']

platform :ios do
  desc "Build development version"
  lane :dev do
    sync_code_signing(
      type: "development",
      git_private_key: ENV["APPLE_CERTIFICATES_SSH_KEY"]
    )
    build_app(
      scheme: "Runner",
      configuration: "Debug",
      workspace: "Runner.xcworkspace",
      export_method: "development",
    )
  end

  desc "Build and deploy release version"
  lane :release do
    # App Store Connect API setup
    app_store_connect_api_key(
      key_id: ENV["APPLE_KEY_ID"],
      issuer_id: ENV["APPLE_ISSUER_ID"],
      key_content: ENV["APPLE_KEY_CONTENT"]
    )
    sync_code_signing(
      type: "appstore",
      git_private_key: ENV["APPLE_CERTIFICATES_SSH_KEY"]
    )
    increment_build_number(
      build_number: latest_testflight_build_number + 1
    )
    increment_version_number(
      version_number: last_git_tag.gsub("v", "").split("-").first
    )
    build_app(
      scheme: "Runner",
      configuration: "Release",
      workspace: "Runner.xcworkspace",
      export_method: "app-store"
    )
    # Write the xcarchive path to a file so CI can locate dSYMs for Sentry upload.
    # lane_context[XCODEBUILD_ARCHIVE] is set by build_app and contains the exact path.
    archive_path = Actions.lane_context[SharedValues::XCODEBUILD_ARCHIVE]
    File.write("gym_archive_path.txt", archive_path.to_s) if archive_path
    upload_to_testflight(
      skip_waiting_for_build_processing: true,
      ipa: "Runner.ipa"
    )
  end
end
