require 'fastlane/actions/scan'

require_relative "helpers/git"
require_relative "helpers/github"
require_relative "helpers/config"
require_relative 'helpers/pretty_table'

default_platform :ios

platform :ios do
  before_all do
    skip_docs
    if is_ci
      setup_ci
    end
  end

  ###
  ### Development Lanes
  ###

  desc "Remove derived data and cached package artifacts"
  lane :clean do
    clear_derived_data(derived_data_path: ENV['DERIVED_DATA_PATH'])
    FileUtils.rm_rf(ENV['SOURCE_PACKAGES_PATH'])
  end

  desc "Resolve and print dependencies for the entire package or a specific target"
  lane :deps do |options|
    analyze_dependencies(
      target: options[:target],
      summary: !options[:target],
      skip_resolve_dependencies: options[:skip_resolve_dependencies]
    )
  end

  desc "Sync device UUIDs from Firebase and register them in App Store Connect"
  lane :devices do |options|
    sync_devices
  end

  desc "Sync provisioning profiles and certificates for the specified build types and flavors (renew with renew:true)"
  lane :profiles do |options|
    sync_profiles(
      build_type: options[:build_type],
      build_flavor: options[:build_flavor],
      sync_devices: options[:sync_devices],
      renew: options[:renew],
    )
  end

  desc "Runs all tests"
  lane :test do |options|
    run_sharded_tests(
      targets: options[:targets],
      mode: options[:mode],
      shard_index: options[:shard_index],
      shard_count: options[:shard_count],
      skip_resolve_dependencies: options[:skip_resolve_dependencies]
    )
  end

  desc "Runs lint on Swift files and Package.swift"
  lane :lint do |options|
    analyze_dependencies(
      check_for_issues: true,
      skip_resolve_dependencies: options[:skip_resolve_dependencies]
    )
    lint_source_files(
      use_baseline: options[:use_baseline],
      update_baseline: options[:update_baseline],
      target: options[:target],
    )
  end

  desc "Runs lint on Swift files and Package.swift"
  lane :analyze do |options|
    analyze_artifacts(**options)
  end

  ###
  ### Release Lanes
  ###

  desc "Increment version and build requested artifacts"
  lane :version do |options|
    version_info = build_version(**options)
    FastlaneCore::PrintTable.print_values(
      title: "Version Info",
      config: version_info
    )
    version_info.each do |key, value|
      Github.save_output(key: key.to_s, value: value.to_s)
    end
  end

  desc "Increment version and build requested artifacts"
  lane :build do |options|
    build_info = build_artifacts(
      build_type: options[:build_type],
      build_flavor: options[:build_flavor],
      skip_resolve_dependencies: options[:skip_resolve_dependencies],
      skip_sync_profiles: options[:skip_sync_profiles],
      pr_number: options[:pr_number]
    )
    FastlaneCore::PrintTable.print_values(
      title: "Build Info",
      config: build_info
    )
    build_info.each do |key, value|
      Github.save_output(key: key.to_s, value: value.to_s)
    end
  end

  desc "Distribute artifacts"
  lane :distribute do |options|
    install_url = distribute_artifacts(
      channel: options[:channel],
      artifact_path: options[:artifact_path],
      workflow_path: options[:workflow_path],
      workflow_branch: options[:workflow_branch],
      firebase_group: options[:firebase_group],
      firebase_testers: options[:firebase_testers],
    )
    unless install_url.nil? || install_url.empty?
      UI.success("Install URL: #{install_url}")
      Github.save_output(key: "install_url", value: install_url)
    end
  end
end
