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

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"

# Use the repo's portable PHP build when it exists (see README).
if [ -x "$ROOT/.tools/php-common/php" ]; then
  export PATH="$ROOT/.tools/php-common:$PATH"
fi

echo "→ Building front-end assets…"
npm ci
npm run build

echo "→ Preparing a clean staging copy…"
STAGE="$ROOT/dist-cpanel/stage"
OUT="$ROOT/dist-cpanel/dge-cpanel.zip"

rm -rf "$STAGE" "$OUT"
mkdir -p "$ROOT/dist-cpanel" "$STAGE"

rsync -a \
  --exclude '.git' \
  --exclude '/.env' \
  --exclude '/.env.local' \
  --exclude '/.env.*.local' \
  --exclude '.tools' \
  --exclude 'node_modules' \
  --exclude 'dist-cpanel' \
  --exclude 'reference' \
  --exclude 'screenshots' \
  --exclude '.DS_Store' \
  --exclude '.freebuff' \
  --exclude '.phpunit.result.cache' \
  --exclude '.phpunit.cache' \
  --exclude '.codex' \
  --exclude '.cursor' \
  --exclude '.idea' \
  --exclude '.nova' \
  --exclude '.vscode' \
  --exclude '.zed' \
  --exclude 'database/database.sqlite' \
  --exclude 'storage/logs/*' \
  --exclude 'storage/app/installed.lock' \
  --exclude 'storage/framework/cache/data/*' \
  --exclude 'storage/framework/sessions/*' \
  --exclude 'storage/framework/views/*' \
  --exclude 'bootstrap/cache/*.php' \
  --exclude 'tests' \
  --exclude 'phpunit.xml' \
  --exclude 'public/uploads/*' \
  "$ROOT/" "$STAGE/"

# Keep the uploads guard and an empty directory ready for media.
cp "$ROOT/public/uploads/.htaccess" "$STAGE/public/uploads/.htaccess"

# The CMS reads this manifest before applying an in-app update, so a zip that
# is not a real build (or a random archive) is refused.
VERSION="$(tr -d '\r\n' < "$ROOT/VERSION" 2>/dev/null || echo 'development')"
BUILT_AT="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
cat > "$STAGE/deploy-manifest.json" <<JSON
{
    "app": "DGE Content Studio",
    "version": "${VERSION:-development}",
    "built_at": "$BUILT_AT",
    "php": "8.3"
}
JSON
echo "→ Stamped build: v${VERSION:-development} ($BUILT_AT)"

# Strip development dependencies so the archive only ships production code.
run_composer() {
  if command -v composer >/dev/null 2>&1; then
    composer "$@"
  elif [ -f "$ROOT/.tools/composer.phar" ] && command -v php >/dev/null 2>&1; then
    php "$ROOT/.tools/composer.phar" "$@"
  else
    return 127
  fi
}

if run_composer --version >/dev/null 2>&1; then
  echo "→ Pruning development dependencies…"
  # Everything in the lock file is already available, so no network is needed.
  if ! (cd "$STAGE" && COMPOSER_DISABLE_NETWORK=1 run_composer install --no-dev --no-interaction --no-progress --optimize-autoloader --no-scripts); then
    echo "! Pruning failed — restoring vendor/ from the working copy."
    rm -rf "$STAGE/vendor"
    rsync -a "$ROOT/vendor/" "$STAGE/vendor/"
  fi
else
  echo "! Composer not found — vendor/ still includes dev dependencies."
fi

echo "→ Zipping…"
(cd "$STAGE" && zip -rq "$OUT" .)
rm -rf "$STAGE"

SIZE="$(du -h "$OUT" | cut -f1)"
echo "✓ cPanel package ready: dist-cpanel/dge-cpanel.zip ($SIZE)"
echo "  Follow DEPLOYMENT-CPANEL.md to upload and install it."
