default_platform(:android)

def qsfera_env(primary, legacy)
  ENV[primary].to_s.empty? ? ENV[legacy] : ENV[primary]
end

platform :android do
  desc "Pull latest translations from Transifex"
  lane :pull_translations do
    Dir.chdir("..") do
      sh("tx", "pull", "-a", "-f", "--skip")
      #sh("tx" "pull" "--force" "--skip" "--all" "-w" "1" "-m" "onlytranslated")
    end
    files = Dir.glob("../qsferaApp/src/main/res/values-*/strings.xml")
    if files.empty?
      UI.important("No values-*/strings.xml present after tx pull — nothing was downloaded.")
    else
      UI.success("#{files.size} translation file(s) on disk:")
      files.sort.each { |f| UI.message("  #{f}") }
    end
  end

  desc "Build the originalRelease AAB (pulls translations first)"
  lane :build_release do
    {
      "QSFERA_RELEASE_KEYSTORE" => "OC_RELEASE_KEYSTORE",
      "QSFERA_RELEASE_KEYSTORE_PASSWORD" => "OC_RELEASE_KEYSTORE_PASSWORD",
      "QSFERA_RELEASE_KEY_ALIAS" => "OC_RELEASE_KEY_ALIAS",
      "QSFERA_RELEASE_KEY_PASSWORD" => "OC_RELEASE_KEY_PASSWORD"
    }.each do |primary, legacy|
      next unless qsfera_env(primary, legacy).to_s.empty?

      UI.user_error!(
        "#{primary} is not set — release AAB would be unsigned and rejected by Play. " \
        "#{legacy} is still accepted as a legacy fallback."
      )
    end
    pull_translations
    gradle(
      task: "bundle",
      flavor: "Original",
      build_type: "Release"
    )
    sh("ls", "-l", "../qsferaApp/build/outputs/bundle/originalRelease/qsferaApp-original-release.aab")
  end

  desc "Pull translations, build, and upload to Play Store. Override track with: fastlane deploy track:beta"
  lane :deploy do |options|
    track = options[:track] || "internal"
    build_release
    upload_to_play_store(
      track: track,
      aab: lane_context[SharedValues::GRADLE_AAB_OUTPUT_PATH],
      skip_upload_apk: true,
      skip_upload_metadata: true,
      skip_upload_changelogs: true,
      skip_upload_images: true,
      skip_upload_screenshots: true
    )
  end
end
