opt_out_usage

# Have an easy way to get the root of the project
def root_path
  Dir.pwd.sub(/.*\Kfastlane/, '').sub(/.*\Kandroid/, '').sub(/.*\Kios/, '').sub(/.*\K\/\//, '')
end

# Have an easy way to run flutter tasks on the root of the project
lane :sh_on_root do |options|
  command = options[:command]
  sh("cd #{root_path} && #{command}")
end

# Tasks to be reused on each platform flow
lane :fetch_dependencies do
  sh_on_root(command: "flutter pub get --suppress-analytics")
end

# Tasks to be reused on each platform flow
lane :generate_localization do
  sh_on_root(command: "flutter gen-l10n")
end

# Tasks to be reused on each platform flow
lane :build_autogenerated_code do
  sh_on_root(command: "flutter pub run build_runner build --delete-conflicting-outputs")
end

# Tasks to be reused on each platform flow
lane :lint do
  sh_on_root(command: "flutter format --suppress-analytics --set-exit-if-changed -n lib/main.dart lib/src/ test/")
end

lane :build_flutter_app do |options|
  pubspec_version_number = get_version_from_pubspec()

  type = options[:type]
  build_number = options[:build_number]
  version_number = options[:version_number] || pubspec_version_number
  target = options[:target] || "lib/main.dart"
  no_codesign = options[:no_codesign] || false
  config_only = options[:config_only] || false
  obfuscate = options[:obfuscate] || true
  commit = last_git_commit

  command = "flutter build #{type} --release --no-pub --suppress-analytics"
  command += " --build-number=#{build_number}" if build_number.to_s != ""
  command += " --build-name=#{version_number}" if version_number.to_s != ""
  command += " --target=#{target}" if target.to_s != ""
  command += " --no-codesign" if no_codesign
  command += " --config-only" if config_only
  command += " --obfuscate" if obfuscate
  command += " --split-debug-info=./build/app/outputs/symbols" if obfuscate

	UI.message("Building #{type} - version: #{version_number} - build: #{build_number} - commit: #{commit[:abbreviated_commit_hash]}")

  fetch_dependencies

  # Check if build_runner exists in pubspec.yaml
  # If it does, run the build_runner command
  if File.exist?("#{root_path}/pubspec.yaml")
    pubspec_content = File.read("#{root_path}/pubspec.yaml")
    if pubspec_content.include?("build_runner:")
      build_autogenerated_code
    end
  end

  # Check if l10n exists
  # If it does, run the flutter gen-l10n command
  if File.exist?("#{root_path}/l10n.yaml")
    generate_localization
  end

  sh_on_root(command: command)
end

# Tasks to be reused on each platform flow
lane :test do |options|
  sh_on_root(command: "flutter test --no-pub --coverage --suppress-analytics")
end


# Private lane to verify all environment variables are set
private_lane :verify_env do |options|
	# array of ENVS to check
	envs = options.fetch(:envs, [])

	envs.each do |env|
		if ENV[env].nil? || ENV[env].empty?
			UI.user_error!("ENV \"#{env}\" is not set. Please set it in your environment variables (e.g. ios/fastlane/.env)")
		end
	end
end

# A helper method to get the path to the firebase service account json file
def google_service_account_json_path
  root_path + '/' + (ENV["GOOGLE_SERVICE_ACCOUNT_JSON_PATH"] || 'google_service_account.json')
end

# Build number is a unique identifier for each build that is uploaded to the app store.
# This method will get the latest build number from the app store and increment it by 1.
# Ensure authenticate_apple_store is called before this method
def get_build_number(store)
  return get_new_build_number(
    bundle_identifier: store == "appstore" ? ENV["APP_BUNDLE_ID"] : nil,
    package_name: store == "playstore" ? ENV["APP_PACKAGE_NAME"] : nil,
    google_play_json_key_path: google_service_account_json_path
  ).to_s
end


def get_version_from_pubspec
  require 'yaml'

  # Define the correct path to pubspec.yaml relative to the Fastlane directory
  pubspec_path = File.expand_path("#{root_path}/pubspec.yaml")

  # Check if the file exists to avoid errors
  unless File.exist?(pubspec_path)
    UI.error("pubspec.yaml file not found at path: #{pubspec_path}")
    return nil
  end

  # Parse the pubspec.yaml file
  pubspec_content = File.read(pubspec_path)

  # Use regex to find the version number line and extract both version number and build number
  version_line = pubspec_content.match(/version:\s*(\d+\.\d+\.\d+)\+(\d+)/)
  if version_line
    version_number = version_line[1]
  else
    UI.error("Version number not found in pubspec.yaml")
    return nil
  end

  return version_number
end
