require 'dotenv/load'
Dotenv.load("../../.env")

default_platform(:android)

platform :android do
  desc "Build and deploy to Google Play Store"
  lane :release do
    # Get version from pubspec.yaml
    pubspec_path = "#{ENV['PWD']}/../pubspec.yaml"
    pubspec_content = File.read(pubspec_path)
    version_match = pubspec_content.match(/version:\s*(.+)\+(\d+)/)

    if version_match
      version_name = version_match[1].strip
      version_code = version_match[2].to_i
      UI.message("Using version: #{version_name}+#{version_code}")
    else
      UI.user_error!("Could not extract version from pubspec.yaml")
    end

    debug_info_dir = "./debug-info/#{version_name}+#{version_code}"
    git_commit = `git rev-parse --short HEAD`.strip

    # Build the Flutter app
    sh("cd #{ENV['PWD']}/.. && flutter build appbundle --dart-define=ENABLE_IN_APP_REVIEW=true --dart-define=ENABLE_SENTRY=true --dart-define=GIT_COMMIT=#{git_commit} --dart-define=SENTRY_ENVIRONMENT=play-store --obfuscate --split-debug-info=#{debug_info_dir}/aab")

    upload_to_play_store(
      track: "production",
      release_status: "completed",
      version_code: version_code,
      aab: "../build/app/outputs/bundle/release/app-release.aab"
    )

    # Build universal APK for Amazon Appstore (doesn't accept AABs, excludes x86 via env var)
    sh("cd #{ENV['PWD']}/.. && AMAZON=1 flutter build apk --release --dart-define=ENABLE_SENTRY=true --dart-define=GIT_COMMIT=#{git_commit} --dart-define=SENTRY_ENVIRONMENT=amazon --obfuscate --split-debug-info=#{debug_info_dir}/apk")

    upload_to_amazon_appstore(
      apk_paths: [
        "../build/app/outputs/flutter-apk/app-release.apk"
      ],
      overwrite_upload: true,
      overwrite_upload_mode: 'reuse',
      changes_not_sent_for_review: true
    )
  end
end
