# 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 "Runs all the tests"
  lane :test do
    gradle(task: "test")
  end

  desc "Submit a new Beta Build to Crashlytics Beta"
  lane :beta do
    gradle(task: "clean assembleRelease")
    crashlytics
  
    # sh "your_script.sh"
    # You can also use other beta testing services here
  end

  desc "Deploy a new version to the Google Play"
  lane :deploy do
    gradle(task: "clean assembleRelease")
    upload_to_play_store
  end

  desc "Take application screenshots"
  lane :screenshots do
      # To run from terminal: fastlane screenshots
      # Build the debug and AndroidTest packages so they are available for screenshots
      gradle(task: "assembleDebug assembleDebugAndroidTest")

      # Dynamically locate APKs
      app_apk = Dir.glob(File.expand_path("../app/build/outputs/apk/debug/com.aisleron-*-debug.apk")).first
      tests_apk = Dir.glob(File.expand_path("../app/build/outputs/apk/androidTest/debug/com.aisleron-*-debug-androidTest.apk")).first

      # Take light mode screenshots
      screengrab(
          output_directory: "fastlane/screenshots/light-mode",
          device_type: "phone",
          locales: ["en-US"],
          app_package_name: "com.aisleron.debug",
          use_tests_in_packages: ["com.aisleron.screenshots"],
          test_instrumentation_runner: "com.aisleron.di.KoinInstrumentationTestRunner",
          use_timestamp_suffix: false,
          app_apk_path: app_apk,
          tests_apk_path: tests_apk,
          skip_open_summary: true
      )

      # Take dark mode screenshots
      screengrab(
          output_directory: "fastlane/screenshots/dark-mode",
          device_type: "phone",
          locales: ["en-US"],
          app_package_name: "com.aisleron.debug",
          use_tests_in_packages: ["com.aisleron.screenshots"],
          test_instrumentation_runner: "com.aisleron.di.KoinInstrumentationTestRunner",
          use_timestamp_suffix: false,
          app_apk_path: app_apk,
          tests_apk_path: tests_apk,
          skip_open_summary: true,
          launch_arguments: ["uiMode dark"]
      )
  end

  desc "Build Release APK and AAB and move to custom folder"
  lane :assemble_release_artifacts do
    # Build the artifacts
    gradle(task: "clean assembleRelease bundleRelease")

    # Define the destination
    output_dir = File.expand_path(ENV["RELEASE_ARTIFACT_PATH"])

    # Find and copy the APK
    apk_path = Dir.glob("../app/build/outputs/apk/release/com.aisleron-*-release.apk").first
    if apk_path
      FileUtils.cp(apk_path, output_dir)
      UI.success("Copied APK: #{File.basename(apk_path)}")
    else
      UI.error("Could not find any APK in the release folder!")
    end

    # Find and copy the AAB
    bundle_path = Dir.glob("../app/build/outputs/bundle/release/com.aisleron-*-release.aab").first
    if bundle_path
      FileUtils.cp(bundle_path, output_dir)
      UI.success("Copied AAB: #{File.basename(bundle_path)}")
    else
      UI.error("Could not find any AAB in the release folder!")
    end
  end
end
