#!/usr/bin/env bash
# ─────────────────────────────────────────────────────────────────────
# Upload a built artifact to a public file-share AND/OR to Hostinger
# over SCP, then write the resulting URL into the GitHub Actions
# step-summary so the operator gets a click-to-download link without
# needing actions/upload-artifact (which fails when the org storage
# quota is exhausted).
#
# Usage:  share-apk.sh <path/to/file.apk|.aab> <label>
#
# Reads (all optional, set as repo secrets):
#   HOSTINGER_HOST      ssh.example.com
#   HOSTINGER_USER      u703936502
#   HOSTINGER_SSH_KEY   private OpenSSH key (multi-line)
#   HOSTINGER_DEST      /home/u703936502/domains/prmtly.com/public_html
#
# Exits 0 on success of ANY mirror; non-zero only if every mirror fails.
# ─────────────────────────────────────────────────────────────────────
set -u

FILE="${1:-}"
LABEL="${2:-build}"
[ -f "$FILE" ] || { echo "::error::share-apk.sh: file not found: $FILE"; exit 1; }
NAME="$(basename "$FILE")"
SIZE_HUMAN="$(du -h "$FILE" | cut -f1)"

# GitHub step-summary file (markdown rendered on the run page).
SUMMARY="${GITHUB_STEP_SUMMARY:-/dev/null}"
{
  echo "## 📱 Android $LABEL artifact: \`$NAME\` ($SIZE_HUMAN)"
  echo ""
} >> "$SUMMARY"

ANY_OK=0

# ── Mirror 1: file.io (single-use, 14-day expiry) ────────────────
echo "─── uploading $NAME → file.io ───"
RESP=$(curl -sS --max-time 300 -F "file=@${FILE}" "https://file.io/?expires=14d" || true)
URL=$(printf '%s' "$RESP" | grep -oE 'https://file\.io/[A-Za-z0-9]+' | head -1)
if [ -n "$URL" ]; then
  echo "  ✓ $URL"
  echo "- **file.io** (single-use, 14 days): \`$URL\`" >> "$SUMMARY"
  ANY_OK=1
else
  echo "  ✗ file.io upload failed: $RESP"
  echo "- **file.io**: ❌ failed" >> "$SUMMARY"
fi

# ── Mirror 2: bashupload.com (3-day expiry, single-use) ──────────
echo "─── uploading $NAME → bashupload.com ───"
URL2=$(curl -sS --max-time 300 -T "$FILE" "https://bashupload.com/${NAME}" \
       2>/dev/null | grep -oE 'https://bashupload\.com/[^ ]+' | head -1 || true)
if [ -n "$URL2" ]; then
  echo "  ✓ $URL2"
  echo "- **bashupload.com** (single-use, 3 days): \`$URL2\`" >> "$SUMMARY"
  ANY_OK=1
else
  echo "  ✗ bashupload.com upload failed"
  echo "- **bashupload.com**: ❌ failed" >> "$SUMMARY"
fi

# ── Mirror 3: SCP straight to Hostinger (permanent) ──────────────
if [ -n "${HOSTINGER_HOST:-}" ] && [ -n "${HOSTINGER_USER:-}" ] && \
   [ -n "${HOSTINGER_SSH_KEY:-}" ] && [ -n "${HOSTINGER_DEST:-}" ]; then
  echo "─── SCP $NAME → $HOSTINGER_HOST:$HOSTINGER_DEST ───"
  KEY=$(mktemp); chmod 600 "$KEY"
  printf '%s\n' "$HOSTINGER_SSH_KEY" > "$KEY"
  if scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
         -i "$KEY" "$FILE" \
         "${HOSTINGER_USER}@${HOSTINGER_HOST}:${HOSTINGER_DEST}/${NAME}"; then
    PUB="https://prmtly.com/${NAME}"
    echo "  ✓ $PUB"
    echo "- **Hostinger** (permanent): \`$PUB\`" >> "$SUMMARY"
    ANY_OK=1
  else
    echo "  ✗ SCP failed"
    echo "- **Hostinger**: ❌ SCP failed (check HOSTINGER_* secrets)" >> "$SUMMARY"
  fi
  rm -f "$KEY"
else
  echo "  (skipping Hostinger SCP — set HOSTINGER_HOST/USER/SSH_KEY/DEST secrets to enable)"
  echo "- **Hostinger**: ⏭️ skipped (secrets not configured)" >> "$SUMMARY"
fi

echo "" >> "$SUMMARY"

if [ "$ANY_OK" -eq 0 ]; then
  echo "::error::All upload mirrors failed for $NAME"
  exit 1
fi
exit 0
