#!/bin/bash

set -e

MODULE_NAME="oc_telegram_shop"
CURRENT_DIR="$(dirname "$0")"

echo "Current dir: ${CURRENT_DIR}"
composer --version
node --version
php -v

SRC_PATH="$1"
BUILD_PATH="${GITHUB_WORKSPACE}/build"

mkdir -p "$BUILD_PATH"

echo "🔨 Starting build process..."
echo "ℹ️ Source path: ${SRC_PATH}"
echo "ℹ️ Build path: ${BUILD_PATH}"

if [ -z "$BUILD_PATH" ]; then
  echo "❌ Error: BUILD_PATH is required"
  exit 1
fi

if [ -z "$SRC_PATH" ]; then
  echo "❌ Error: SRC_PATH is required"
  exit 1
fi

echo "Build Telecart SPA Frontend..."
cd "${SRC_PATH}/frontend/spa"
npm install
npm run build

echo "Move manifest file"
cp "${SRC_PATH}/module/oc_telegram_shop/upload/image/catalog/tgshopspa/.vite/manifest.json" "${SRC_PATH}/module/oc_telegram_shop/upload/image/catalog/tgshopspa/manifest.json"
rm -rf "${SRC_PATH}/module/oc_telegram_shop/upload/image/catalog/tgshopspa/.vite"

echo "Build Admin Frontend"
cd "${SRC_PATH}/frontend/admin"
npm install
npm run build

echo "Calculate MD5 hash sum"
cd ../../module/oc_telegram_shop/upload/admin/view/javascript/telecart
FILES=("telecart.css" "telecart.js")
HASH=$(cat "${FILES[@]}" | md5sum | awk '{print $1}')
OUTPUT="checksum.hash"
echo "$HASH" > "$OUTPUT"
echo "Checksum in $OUTPUT: $HASH"

cd "$SRC_PATH"

echo "Cleanup frontend"
rm -fv module/oc_telegram_shop/upload/image/catalog/tgshopspa/vite.svg
rm -fv module/oc_telegram_shop/upload/image/catalog/tgshopspa/assets/*.map
rm -fv module/oc_telegram_shop/upload/admin/view/integration.js
rm -fv module/oc_telegram_shop/upload/admin/view/javascript/telecart/telecart.js.map
rm -fv module/oc_telegram_shop/upload/admin/view/javascript/telecart/favicon.ico

echo "Install Composer dependencies."
cd "${SRC_PATH}/module/oc_telegram_shop/upload/oc_telegram_shop"
composer install \
  --prefer-dist \
  --no-dev \
  --optimize-autoloader \
  --no-interaction \
  --no-cache

cd "$SRC_PATH"

echo "Copy .env for production"
cp "${SRC_PATH}/module/oc_telegram_shop/upload/oc_telegram_shop/.env.production" \
  "${SRC_PATH}/module/oc_telegram_shop/upload/oc_telegram_shop/.env"

# PHP Obfuscation
# Temporary disabled
#echo "Install PHP Obfuscation lib"
#cd "${GITHUB_WORKSPACE}"
#echo "Download yakpro-po"
#wget https://github.com/pk-fr/yakpro-po/archive/refs/heads/master.zip
#unzip -oq "master.zip"
#rm master.zip
#cd "${GITHUB_WORKSPACE}/yakpro-po-master"
#echo "Download PHP-Parser"
#wget https://github.com/nikic/PHP-Parser/archive/refs/heads/4.x.zip -O parser.zip
#unzip -oq "parser.zip"
#mv PHP-Parser-4.x PHP-Parser
#chmod a+x "${GITHUB_WORKSPACE}/yakpro-po-master/yakpro-po.php"
#
#echo "Version 4.19.4 (2024-09-29)" >> "${GITHUB_WORKSPACE}/yakpro-po-master/PHP-Parser/CHANGELOG.md"
#
#echo "PHP Obfuscation: framework"
#php "${GITHUB_WORKSPACE}/yakpro-po-master/yakpro-po.php" --silent --config-file \
#  "${SRC_PATH}/docker/config.php" \
#  "${SRC_PATH}/module/oc_telegram_shop/upload/oc_telegram_shop/framework" \
#  -o /tmp/obfuscated
#rm -rf "${SRC_PATH}/module/oc_telegram_shop/upload/oc_telegram_shop/framework"
#mv /tmp/obfuscated/yakpro-po/obfuscated "${SRC_PATH}/module/oc_telegram_shop/upload/oc_telegram_shop/framework"
#
#echo "PHP Obfuscation: src"
#php "${GITHUB_WORKSPACE}/yakpro-po-master/yakpro-po.php" --silent --config-file \
#  "${SRC_PATH}/docker/config.php" \
#  "${SRC_PATH}/module/oc_telegram_shop/upload/oc_telegram_shop/src" \
#  -o /tmp/obfuscated
#rm -rf "${SRC_PATH}/module/oc_telegram_shop/upload/oc_telegram_shop/src"
#mv /tmp/obfuscated/yakpro-po/obfuscated "${SRC_PATH}/module/oc_telegram_shop/upload/oc_telegram_shop/src"
#
#echo "PHP Obfuscation: bastion"
#php "${GITHUB_WORKSPACE}/yakpro-po-master/yakpro-po.php" --silent --config-file \
#  "${SRC_PATH}/docker/config.php" \
#  "${SRC_PATH}/module/oc_telegram_shop/upload/oc_telegram_shop/bastion" \
#  -o /tmp/obfuscated
#rm -rf "${SRC_PATH}/module/oc_telegram_shop/upload/oc_telegram_shop/bastion"
#mv /tmp/obfuscated/yakpro-po/obfuscated "${SRC_PATH}/module/oc_telegram_shop/upload/oc_telegram_shop/bastion"

cd "$SRC_PATH"

echo "📜 Creating Phar archive..."
rm -rf \
  "${SRC_PATH}/module/oc_telegram_shop/upload/oc_telegram_shop/tests" \
  "${SRC_PATH}/module/oc_telegram_shop/upload/oc_telegram_shop/phpunit.xml" \
  "${SRC_PATH}/module/oc_telegram_shop/upload/oc_telegram_shop/psalm.xml" \
  "${SRC_PATH}/module/oc_telegram_shop/upload/oc_telegram_shop/psalm-classes.php" \
  "${SRC_PATH}/module/oc_telegram_shop/upload/oc_telegram_shop/psalm-stubs.php"

php -d phar.readonly=0 scripts/ci/create-phar.php "${SRC_PATH}/module/oc_telegram_shop/upload/oc_telegram_shop" "${MODULE_NAME}.phar"

echo "📦 Packaging into .ocmod.zip..."
mkdir -p "${SRC_PATH}/module/oc_telegram_shop/upload/system/library/oc_telegram_shop"
mv "${MODULE_NAME}.phar" "${SRC_PATH}/module/oc_telegram_shop/upload/system/library/oc_telegram_shop"
mv "${SRC_PATH}/module/oc_telegram_shop/upload/cli.php" "${SRC_PATH}/module/oc_telegram_shop/upload/system/library/oc_telegram_shop"
chmod +x "${SRC_PATH}/module/oc_telegram_shop/upload/system/library/oc_telegram_shop/cli.php"

rm -rf \
  "${SRC_PATH}/module/oc_telegram_shop/upload/oc_telegram_shop"

cd "${SRC_PATH}/module/oc_telegram_shop/"
echo "List files"
ls -la ./upload
zip -rqq "${MODULE_NAME}.ocmod.zip" .
mv "${MODULE_NAME}.ocmod.zip" "${BUILD_PATH}"

FINAL_FILE="${BUILD_PATH}/${MODULE_NAME}.ocmod.zip"

if [ -f "$FINAL_FILE" ]; then
  FILE_SIZE=$(du -h "$FINAL_FILE" | cut -f1)
  echo -e "✅ Build completed successfully!"
  echo -e "🎉 Created: $FINAL_FILE ($FILE_SIZE)"
else
  echo "❌ Error: Final build file not found: $FINAL_FILE"
  exit 1
fi

echo -e "🎯 Build artifact ready for installation!"