# 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

# General parameters
app_name = 'Cuppa'
repo_name = 'ncosgray/cuppa_mobile'

# Load version details from the Flutter build configuration
require 'yaml'
pubspec = YAML.load_file('../pubspec.yaml')
version_string = pubspec['version']
version, build_number = version_string.split('+')

# Android lane definitions
platform :android do
  aab_path = 'build/app/outputs/bundle/release'
  apk_path = 'build/app/outputs/flutter-apk'
  tmp_path = '/tmp/fastlane/android'

  desc 'Build and upload to Play Store Alpha'
  lane :playalpha do
    sh('flutter build appbundle')
    upload_to_play_store(
      json_key: ENV['GOOGLE_PLAY_JSON_KEY_PATH'],
      aab: "#{aab_path}/app-release.aab",
      skip_upload_metadata: true,
      skip_upload_changelogs: true,
      skip_upload_images: true,
      skip_upload_screenshots: true,
      track: 'alpha',
    )
  end

  desc 'Release Play Store Alpha to Prod'
  lane :playprod do
    sh("mkdir -p #{tmp_path}")
    sh("cp -r metadata/android/en-US #{tmp_path}")
    upload_to_play_store(
      json_key: ENV['GOOGLE_PLAY_JSON_KEY_PATH'],
      version_code: build_number,
      skip_upload_aab: true,
      skip_upload_apk: true,
      metadata_path: tmp_path,
      track: 'alpha',
      track_promote_to: 'production',
    )
  end

  desc 'Build an APK and release to GitHub'
  lane :github do
    sh('../build_fdroid.sh')
    sh("cp ../#{apk_path}/app-release.apk ../#{apk_path}/#{app_name}-#{version}.apk")
    set_github_release(
      repository_name: repo_name,
      api_token: ENV['GITHUB_API_TOKEN'],
      tag_name: version,
      name: "#{app_name} #{version}",
      description: File.read("metadata/android/en-US/changelogs/#{build_number}.txt"),
      upload_assets: ["#{apk_path}/#{app_name}-#{version}.apk"],
    )
    sh("rm ../#{apk_path}/#{app_name}-#{version}.apk")
  end
end

# iOS lane definitions
platform :ios do
  ipa_path = 'build/ios/ipa'
  tmp_path = '/tmp/fastlane/ios'

  desc 'Build and upload to Apple TestFlight'
  lane :appletest do
    sh('flutter build ipa')
    build_app(
      workspace: 'ios/Runner.xcworkspace',
      scheme: 'Runner',
      output_directory: ipa_path,
    )
    upload_to_testflight(
      api_key_path: ENV['APPSTORE_API_KEY_PATH'],
      ipa: "#{ipa_path}/Runner.ipa",
    )
  end

  desc 'Release Apple TestFlight to Prod'
  lane :appleprod do
    sh("mkdir -p #{tmp_path}")
    sh("cp -r metadata/ios/metadata/*.txt #{tmp_path}")
    sh("cp -r metadata/ios/metadata/en-US #{tmp_path}")
    upload_to_app_store(
      api_key_path: ENV['APPSTORE_API_KEY_PATH'],
      build_number: build_number,
      app_version: version,
      skip_binary_upload: true,
      metadata_path: tmp_path,
      screenshots_path: 'fastlane/metadata/ios/screenshots',
      overwrite_screenshots: true,
      release_notes: {
        "en-US" => File.read("metadata/ios/en-US/changelogs/#{build_number}.txt")
      },
      precheck_include_in_app_purchases: false,
      submission_information: {
        add_id_info_uses_idfa: false,
        export_compliance_encryption_updated: false,
        export_compliance_uses_encryption: false
      },
      force: true,
      submit_for_review: true,
      automatic_release: true,
    )
  end
end
