# Only internal testing is supported. No public store release lane exists.
opt_out_usage
fastlane_version '2.241.1'

def required_private_file(variable)
  path = ENV.fetch(variable) { UI.user_error!("Set #{variable} to a private file path") }
  UI.user_error!("Missing #{variable} file") unless File.file?(File.expand_path(path))
  File.expand_path(path)
end

def source_delivery
  source = required_private_file('XUONGSCAN_SOURCE_ARCHIVE')
  UI.message("Corresponding source archive supplied: #{File.basename(source)}")
  UI.message('Give this exact archive and included licenses to every binary recipient.')
  ensure_git_status_clean
end

platform :android do
  desc 'Upload an already verified signed AAB to Google Play internal testing only'
  lane :internal do
    source_delivery
    status = ENV.fetch('PLAY_RELEASE_STATUS', 'draft')
    UI.user_error!('Use draft or completed for internal testing') unless %w[draft completed].include?(status)
    upload_to_play_store(
      package_name: 'vn.genium.xuongscan',
      json_key: required_private_file('PLAY_SERVICE_ACCOUNT_JSON'),
      aab: required_private_file('XUONGSCAN_AAB'),
      track: 'internal', release_status: status,
      skip_upload_metadata: true, skip_upload_images: true,
      skip_upload_screenshots: true, skip_upload_changelogs: true
    )
    UI.message('Verify the internal track, tester membership and an actual installation separately.')
  end
end

platform :ios do
  desc 'Upload an already verified signed IPA for internal TestFlight only'
  lane :internal do
    source_delivery
    upload_to_testflight(
      app_identifier: 'vn.genium.xuongscan',
      ipa: required_private_file('XUONGSCAN_IPA'),
      api_key_path: required_private_file('ASC_API_KEY_JSON'),
      distribute_external: false, notify_external_testers: false,
      skip_waiting_for_build_processing: false,
      changelog: 'XưởngScan 0.1.0: thử nghiệm quét, OCR ngoại tuyến, PDF và lưu trữ Synology.'
    )
    UI.message('Check TestFlight processing and assign only an authorized internal tester group; verify installation.')
  end
end
