#!/usr/bin/env bash
#
# Archive + upload the NucleicRemote iPhone app to App Store Connect (→ TestFlight).
#
#   scripts/ios-release.sh [--archive-only]
#
# iOS has no parallel "channels" like the Mac app: betas and RCs are TestFlight builds of the
# single app (bundle id xyz.blakeslee.nucleic.remote), distinguished by build number / TestFlight group,
# and a store release is a promoted TestFlight build. So this one script covers beta, rc, and
# release — what differs is which build you promote in App Store Connect.
#
# Prerequisites (one-time — see signing/README.md):
#   • An "Apple Distribution" certificate for the paid team (L7UDTQ6F5W) in the keychain.
#   • The App ID `xyz.blakeslee.nucleic.remote` registered + an app record created in App Store Connect.
#   • An App Store Connect API key (the same one notarization uses), passed by env:
#       NUCLEIC_ASC_KEY_PATH=/path/AuthKey_XXXX.p8
#       NUCLEIC_ASC_KEY_ID=<KeyID>
#       NUCLEIC_ASC_ISSUER_ID=<IssuerID>
#     With the key + -allowProvisioningUpdates, Xcode creates/downloads the distribution profile.
#
# Versioning: every TestFlight upload needs a unique build number. We pass MARKETING_VERSION
# (NUCLEIC_VERSION, default 1.0) and CURRENT_PROJECT_VERSION (NUCLEIC_IOS_BUILD, default the git
# commit count — same convention as the Mac app) so re-uploads don't collide.

set -euo pipefail

ARCHIVE_ONLY=0
[ "${1:-}" = "--archive-only" ] && ARCHIVE_ONLY=1

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"

PROJ="ios/NucleicRemote/NucleicRemote.xcodeproj"
SCHEME="NucleicRemote"
ARCHIVE="$ROOT/dist/ios/NucleicRemote.xcarchive"
EXPORT_DIR="$ROOT/dist/ios/export"

MARKETING_VERSION="${NUCLEIC_VERSION:-1.0}"
BUILD_NUMBER="${NUCLEIC_IOS_BUILD:-$(git rev-list --count HEAD 2>/dev/null || echo 1)}"

# App Store Connect API-key flags for xcodebuild (provisioning updates + upload).
AUTH=()
if [ -n "${NUCLEIC_ASC_KEY_PATH:-}" ] && [ -n "${NUCLEIC_ASC_KEY_ID:-}" ] && [ -n "${NUCLEIC_ASC_ISSUER_ID:-}" ]; then
  AUTH=(-authenticationKeyPath "$NUCLEIC_ASC_KEY_PATH"
        -authenticationKeyID "$NUCLEIC_ASC_KEY_ID"
        -authenticationKeyIssuerID "$NUCLEIC_ASC_ISSUER_ID")
elif [ "$ARCHIVE_ONLY" -eq 0 ]; then
  cat >&2 <<EOF
ios-release: upload requested but no App Store Connect API key in the environment.

  export NUCLEIC_ASC_KEY_PATH=/path/AuthKey_XXXX.p8
  export NUCLEIC_ASC_KEY_ID=<KeyID>
  export NUCLEIC_ASC_ISSUER_ID=<IssuerID>

Or run with --archive-only to just build the archive (then upload via Xcode Organizer).
EOF
  exit 1
fi

echo "▸ Archiving $SCHEME  (version $MARKETING_VERSION, build $BUILD_NUMBER)"
rm -rf "$ARCHIVE"
xcodebuild \
  -project "$PROJ" \
  -scheme "$SCHEME" \
  -configuration Release \
  -destination 'generic/platform=iOS' \
  -archivePath "$ARCHIVE" \
  -allowProvisioningUpdates \
  "${AUTH[@]}" \
  MARKETING_VERSION="$MARKETING_VERSION" \
  CURRENT_PROJECT_VERSION="$BUILD_NUMBER" \
  clean archive

if [ "$ARCHIVE_ONLY" -eq 1 ]; then
  echo "✓ Archive built: $ARCHIVE"
  echo "  Upload via Xcode → Window → Organizer → Distribute App → App Store Connect."
  exit 0
fi

echo "▸ Exporting + uploading to App Store Connect (TestFlight)"
rm -rf "$EXPORT_DIR"
xcodebuild -exportArchive \
  -archivePath "$ARCHIVE" \
  -exportOptionsPlist ios/ExportOptions.plist \
  -exportPath "$EXPORT_DIR" \
  -allowProvisioningUpdates \
  "${AUTH[@]}"

echo "✓ Uploaded build $BUILD_NUMBER ($MARKETING_VERSION) to App Store Connect."
echo "  It will appear in TestFlight after processing. Assign it to a beta/rc tester group there,"
echo "  or submit it for App Store review to ship a release."
