# 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

default_platform(:android)

platform :android do

  desc "Run detekt"
  lane :detekt do
    FileUtils.cp("../app/src/main/assets/tangem-app-config/android/google-services/dev/google-services.json", "../app")
    FileUtils.cp("../tangem-android-tools/CI/gradle_properties/tests_ci_gradle.properties", "../gradle.properties")
    puts File.read("../gradle.properties")
    gradle(task: "detekt detektGoogleDebug --continue")
  end

  desc "Run tests"
  lane :test do
    FileUtils.cp("../app/src/main/assets/tangem-app-config/android/google-services/dev/google-services.json", "../app")
    FileUtils.cp("../tangem-android-tools/CI/gradle_properties/tests_ci_gradle.properties", "../gradle.properties")
    puts File.read("../gradle.properties")
    gradle(task: "testGoogleDebugUnitTest testDebugUnitTest --continue")
  end

  desc "Build internal APK Firebase App Distribution"
  lane :buildInternal do |options|
    FileUtils.cp("../app/src/main/assets/tangem-app-config/android/google-services/dev/google-services.json", "../app")
    FileUtils.cp("../tangem-android-tools/CI/gradle_properties/build_ci_gradle.properties", "../gradle.properties")
    puts File.read("../gradle.properties")

    gradle(
      task: "assemble",
      build_type: "GoogleInternal",
      properties: {
        'versionCode' => ENV['version_code'],
        'versionName' => ENV['version_name'],
      }
  )
  end

  desc "Publish internal build to Firebase App Distribution"
  lane :publishToFirebase do |options|
    uploaded_release = firebase_app_distribution(
      firebase_cli_token: ENV['firebase_cli_token'],
      app: ENV['app_id_internal'],
      apk_path: ENV['apk_path_internal'],
      groups: ENV['groups'],
      release_notes: ENV['release_notes'],
    )
    release_url = uploaded_release[:testingUri]

    # Applying base64 encoding twice to prevent GA bug "Skip output 'output' since it may contain secret."
    # See https://github.com/orgs/community/discussions/13082#discussioncomment-6776428 for details
    encoded_release_url = Base64.strict_encode64(Base64.strict_encode64(release_url))
    sh("echo 'encoded_release_url=#{encoded_release_url}' >> /workspace/github_output.txt")
  end

end
