#!/usr/bin/env bash
set -euo pipefail

script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
if [[ $# -ne 1 ]]; then
  echo "usage: $0 <verified-candidate-root>" >&2
  exit 64
fi
candidate=$(cd "$1" && pwd -P)
manifest="$candidate/BUILD-MANIFEST.txt"
[[ -f "$manifest" && ! -L "$manifest" ]] || {
  echo "candidate BUILD-MANIFEST.txt is missing" >&2
  exit 1
}

platforms=$(awk -F= '
  $1 == "platforms" { count += 1; value = $2 }
  END { if (count != 1) exit 1; print value }
' "$manifest") || {
  echo "candidate manifest must contain exactly one platforms value" >&2
  exit 1
}
case "$platforms" in
  ios) scope=ios ;;
  ios,tvos) scope=apple ;;
  *)
    echo "candidate platforms must be exactly ios or ios,tvos" >&2
    exit 1
    ;;
esac
export BEAM_VLCKIT_PLATFORMS=$platforms
# shellcheck source=common.sh
source "$script_dir/common.sh"
beam_require_command gh

# This command only stages the exact local bytes in a private draft so CI can
# reverify and consume them. Public/TestFlight distribution remains guarded by
# IOS_LIBVLC_DISTRIBUTION_APPROVED in release-ios.yml. Do not conflate the two
# decisions: an internal draft is useful before the recipient-facing notices,
# source location, and replacement path are ready.
repository_visibility=$(gh repo view --json visibility --jq '.visibility')
[[ "$repository_visibility" == PRIVATE ]] || \
  beam_die "candidate staging is allowed only in a private repository draft"

version=$(awk -F= '$1 == "vlckit_version" { count += 1; value = $2 } END { if (count != 1) exit 1; print value }' "$manifest") || \
  beam_die "candidate manifest must contain one vlckit_version"
revision=$(awk -F= '$1 == "candidate_revision" { count += 1; value = $2 } END { if (count != 1) exit 1; print value }' "$manifest") || \
  beam_die "candidate manifest must contain one candidate_revision"
tag="beam-vlckit-$scope-$version-$revision"
[[ ${BEAM_VLCKIT_UPLOAD_CONFIRM:-} == "$tag" ]] || \
  beam_die "set BEAM_VLCKIT_UPLOAD_CONFIRM=$tag to confirm this one-time upload"

"$script_dir/verify_candidate.sh" "$candidate"
[[ -f "$candidate/VERIFIED" && -f "$candidate/ios/VERIFIED" ]] || \
  beam_die "candidate lacks its required verified markers"
if beam_platform_selected tvos; then
  [[ -f "$candidate/tvos/VERIFIED" ]] || beam_die "candidate lacks its tvOS verified marker"
fi

if gh release view "$tag" >/dev/null 2>&1; then
  [[ "$(gh release view "$tag" --json isDraft --jq '.isDraft')" == true ]] || \
    beam_die "Refusing to append assets to a published release: $tag"
  asset_count=$(gh release view "$tag" --json assets --jq '.assets | length')
  [[ "$asset_count" == 0 ]] || \
    beam_die "Refusing to append to a draft that already contains assets: $tag"
else
  gh release create "$tag" --draft \
    --title "BEAM VLCKit $scope $version-$revision" \
    --notes "Locally source-built and audited candidate. Draft status is not distribution approval."
fi

assets=(
  "$candidate"/artifacts/*
  "$candidate/BUILD-MANIFEST.txt"
  "$candidate/SOURCE-MANIFEST.txt"
  "$candidate/SHA256SUMS"
)
gh release upload "$tag" "${assets[@]}"
echo "Uploaded the immutable local candidate once to draft release $tag."
