# 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
  def project_root
    File.expand_path("../..", __dir__)
  end

  def play_service_account_json
    ENV.fetch("GOOGLE_PLAY_SERVICE_ACCOUNT_JSON")
  end

  def play_build_command
    "cd #{project_root} && flutter build appbundle --release"
  end

  def play_aab_path
    File.join(project_root, "build/app/outputs/bundle/release/app-release.aab")
  end

  def play_package_name
    "com.meshcore.sar.meshcore_sar_app"
  end

  def direct_apk_build_command
    "cd #{project_root} && flutter build apk --release"
  end

  def direct_apk_path
    File.join(project_root, "build/app/outputs/flutter-apk/app-release.apk")
  end

  def skip_android_build?
    ENV["SKIP_ANDROID_BUILD"] == "1"
  end

  desc "Runs all the tests"
  lane :test do
    gradle(task: "test")
  end

  desc "Build release APK"
  lane :direct_apk do
    sh(direct_apk_build_command)

    UI.success("APK built successfully at: #{direct_apk_path}")
  end

  desc "Build Play release AAB and upload to internal testing"
  lane :internal do
    sh(play_build_command) unless skip_android_build?

    upload_to_play_store(
      aab: play_aab_path,
      package_name: play_package_name,
      track: "internal",
      release_status: "draft",
      json_key_data: play_service_account_json
    )
  end

  desc "Build Play release AAB and upload to production"
  lane :production do
    sh(play_build_command) unless skip_android_build?

    upload_to_play_store(
      aab: play_aab_path,
      package_name: play_package_name,
      track: "production",
      json_key_data: play_service_account_json
    )
  end
end
