default_platform(:android)

platform :android do
  desc "Deploy to Firebase from Mac"
  lane :deploy_firebase do
    
    # 1. Création du Keystore depuis le secret Gitea 
    if ENV['ANDROID_KEYSTORE_BASE64']
      File.open("upload-keystore.jks", "wb") do |f|
        f.write(Base64.decode64(ENV['ANDROID_KEYSTORE_BASE64']))
      end
    else
        UI.error("Secret Keystore manquant !")
    end

    # 2. Build de l'App Bundle (AAB)
    sh("flutter build appbundle --release")

    # 3. Envoi vers Firebase App Distribution
    firebase_app_distribution(
      app: ENV["FIREBASE_ANDROID_APP_ID"],
      service_credentials_file: "firebase_credentials.json",
      groups: "testers",
      android_artifact_path: "../build/app/outputs/bundle/release/app-release.aab",
      release_notes: "Build depuis Mac M1/M2. Commit: #{ENV['GITHUB_SHA']}"
    )
  end
end