# 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
#
opt_out_usage

default_platform(:android)

platform :android do

  desc "Execute static analysis"
  lane :analyse do
    gradle(task: "detekt")
  end

  desc "Assemble the devDebug APK"
  lane :assembleDevDebug do
    gradle(task: "assembleDevDebug", properties: { "enableFcmService" => false })
  end

  desc "Assemble the androidTest APK"
  lane :assembleDevDebugAndroidTest do
    gradle(task: "assembleDevDebugAndroidTest")
  end

  desc "Assemble the release APK for alpha flavor"
  lane :assembleAlphaRelease do
    bumpAppVersion
    gradle(task: "assembleAlphaRelease")
  end

  desc "Assemble the release APK for prod flavor"
  lane :assembleProdRelease do
    bumpAppVersion
    gradle(task: "assembleProdRelease")
  end

  desc "Bump the version code"
  lane :bumpAppVersion do
    sh("../scripts/release/bump_version.sh")
  end

  desc "Runs Unit Tests"
  lane :unitTest do
    gradle(
      tasks: [
        "testDevDebugUnitTest",
        "testDebugUnitTest",
        "testUtilsUnitTest",
        "coberturaXmlReport",
        "globalLineCoverage"
      ],
      flags: "-Pci --console=plain"
    )
  end

  desc "Setup UI Tests assets"
    lane :setupUiTestsAssets do
    sh("../scripts/uitests/setup-core-assets.sh")
    sh("../scripts/uitests/setup-mock-network-assets.sh setup-remote")
  end

  desc "Runs Proton Core Libraries Tests Suite on Firebase Test lab"
  lane :coreLibsTest do
    sh("../scripts/run_firebase_ui_tests.sh core-libs-test")
  end

  desc "Runs Smoke Tests Suite on Firebase Test lab"
  lane :smokeTest do
    sh("../scripts/run_firebase_ui_tests.sh smoke-test")
  end

  desc "Runs Rust Ready Smoke Tests Suite on Firebase Test lab"
  lane :rustReadySmokeTest do
    sh("../scripts/run_firebase_ui_tests.sh rust-test")
  end

  desc "Runs all UI tests on a wide set of devices on Firebase Test lab"
  lane :fullRegressionTest do
    sh("../scripts/run_firebase_ui_tests.sh full-regression-test")
  end

  desc "Publish alpha build to firebase dev group"
  lane :deployToFirebaseDevGroup do
    firebase_app_distribution(
      app: '1:75309174866:android:d354e9e5da9113aa78cf8b',
      android_artifact_path: './signedArtifacts/app-alpha-release.apk',
      # Service account is created on the CI from an env var each run (destroyed when finishing)
      service_credentials_file: '/tmp/service-account.json',
      groups: 'et-dev-builds-testers',
      release_notes_file: '/tmp/release_notes.txt'
    )
  end

  desc "Publish nightly alpha build to firebase nightly group"
  lane :deployToFirebaseNightlyGroup do
    firebase_app_distribution(
      app: '1:75309174866:android:d354e9e5da9113aa78cf8b',
      android_artifact_path: './signedArtifacts/app-alpha-release.apk',
      # Service account is created on the CI from an env var each run (destroyed when finishing)
      service_credentials_file: '/tmp/service-account.json',
      groups: 'et-nightly-builds-testers',
      release_notes_file: '/tmp/release_notes.txt'
    )
  end

  desc "Publish prod build to firebase alpha group"
  lane :deployToFirebaseInternalAlphaGroup do
    firebase_app_distribution(
      app: '1:75309174866:android:e4f24581e0ac723978cf8b',
      android_artifact_path: './signedArtifacts/app-prod-release.apk',
      # Service account is created on the CI from an env var each run (destroyed when finishing)
      service_credentials_file: '/tmp/service-account.json',
      groups: 'et-closed-alpha-testers',
      release_notes_file: '/tmp/release_notes.txt'
    )
  end

  desc "Tag commit with release version name and code"
  lane :tagRelease do
    sh("../scripts/release/tag_release.sh")
  end

  desc "Deploy to Play Store (Internal Track)"
  lane :deployToPlayStoreInternal do
    upload_to_play_store(
      package_name: "ch.protonmail.android",
      track: "internal",
      apk: "./signedArtifacts/app-prod-release.apk",
      mapping_paths: [
        "./app/build/outputs/mapping/prodRelease/mapping.txt",
        "./app/build/outputs/native-debug-symbols/prodRelease/native-debug-symbols.zip"
      ],
      json_key: "/tmp/play_store_service_account.json"
    )
  end
end
