platform :windows do
  lane :upload_s3 do |options|
    exe = options[:exe]

    aws_s3(
      files: [exe],
      endpoint: "https://sos-ch-dk-2.exo.io",
      app_directory: "releases"
    )
  end
end

platform :ios do
  lane :setup_signing do |options|
    signing_type = options[:type] # "development", "appstore"
    app_identifier = options[:app_identifier] # "ch.opengis.qfield", "ch.opengis.qfield-dev"

    setup_ci if is_ci

    match(
      type: signing_type,
      app_identifier: app_identifier
    )
  end

  lane :upload_s3 do |options|
    ipa = options[:ipa]

    setup_ci if is_ci

    aws_s3(
      ipa: ipa,
      endpoint: "https://sos-ch-dk-2.exo.io",
      app_directory: "ci-builds/ios/#{ENV['CI_PACKAGE_FILE_SUFFIX']}"
    )
  end

  lane :upload_testflight do |options|
    ipa = options[:ipa]

    setup_ci if is_ci

    api_key = app_store_connect_api_key(
      key_id: ENV["api_key_id"],
      issuer_id: ENV["issuer_id"],
      key_content: ENV["api_private_key"],
      duration: 1200
    )

    upload_to_testflight(
      ipa: ipa
    )
  end

  desc "Generate new certificates"
  lane :generate_new_certificates do
    setup_ci if is_ci

    api_key = app_store_connect_api_key(
      key_id: ENV["api_key_id"],
      issuer_id: ENV["issuer_id"],
      key_content: ENV["api_private_key"],
      duration: 1200
    )
    match_nuke(
      type: "development",
      app_identifier: ['ch.opengis.qfield', 'ch.opengis.qfield-dev'],
      skip_confirmation: true,
      readonly: false
    )
    match_nuke(
      type: "appstore",
      app_identifier: ['ch.opengis.qfield'],
      skip_confirmation: true,
      readonly: false
    )
    sync_code_signing(
      type: "development",
      app_identifier: ['ch.opengis.qfield', 'ch.opengis.qfield-dev'],
      force_for_new_devices: true,
      readonly: false
    )
    sync_code_signing(
      type: "appstore",
      app_identifier: ['ch.opengis.qfield'],
      force_for_new_devices: true,
      readonly: false
    )
  end
end
