# 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"])
  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 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 "Generate test coverage report based on the last test run"
  lane :coverageReport do
    gradle(task: "-Pci --console=plain coberturaXmlReport globalLineCoverage :coverage:koverHtmlReport -x :coverage:jacocoToCobertura")
  end

  desc "Publish alpha build to firebase dev group"
  lane :deployToFirebaseDevGroup do
    firebase_app_distribution(
      app: '1:75309174866:android:d354e9e5da9113aa78cf8b',
      android_artifact_path: 'app/build/outputs/apk/alpha/release/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: 'v6-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: 'app/build/outputs/apk/alpha/release/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: 'v6-nightly-builds-testers',
      release_notes_file: '/tmp/release_notes.txt'
    )
  end

  desc "Publish alpha build to firebase alpha group"
  lane :deployToFirebaseInternalAlphaGroup do
    firebase_app_distribution(
      app: '1:75309174866:android:d354e9e5da9113aa78cf8b',
      android_artifact_path: 'app/build/outputs/apk/alpha/release/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: 'v6-internal-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",
      json_key: "/tmp/play_store_service_account.json"
    )
  end
end

