#!/usr/bin/env bash

# Build and package the Flutter web app for PRODUCTION delivery with optimizations.

set -euo pipefail

PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TIMESTAMP="$(date +%Y%m%d_%H%M%S)"
OUT_DIR="web_prod_export_${TIMESTAMP}"
ZIP_FILE="${OUT_DIR}.zip"

cd "${PROJECT_ROOT}"

write_prod_env() {
  cat > assets/config/env.json <<'EOF'
{
  "flavor":"pro",
  "baseUrl":"https://api.mypoint.com.vn/8854/gup2start/rest",
  "t3Token":"runner-env-flavor-pro",
  "enableLogging":false
}
EOF
  echo "📋 Production config written to assets/config/env.json"
}

copy_x_app_sdk() {
  local src="node_modules/x-app-sdk/dist/index.es.js"
  local dest="build/web/js/x_app_sdk_bundle.js"
  if [ ! -f "${src}" ]; then
    echo "❌ x-app-sdk bundle not found at ${src}" >&2
    exit 1
  fi
  mkdir -p "$(dirname "${dest}")"
  cp "${src}" "${dest}"
  echo "✅ Copied x-app-sdk bundle."
}

compress_assets() {
  local dir="$1"
  echo "🗜️  Compressing assets with maximum compression..."
  
  # Gzip compression (level 9 = maximum)
  if command -v gzip >/dev/null 2>&1; then
    echo "   → Creating .gz files (gzip -9)..."
    find "${dir}" -type f \( -name '*.js' -o -name '*.css' -o -name '*.json' -o -name '*.wasm' -o -name '*.svg' -o -name '*.html' -o -name '*.woff2' -o -name '*.otf' -o -name '*.ttf' \) ! -name '*.gz' ! -name '*.br' -exec gzip -9 -kf {} \;
    echo "   ✅ Gzip compression completed"
  else
    echo "   ⚠️  gzip not available, skipping .gz artifacts"
  fi
  
  # Brotli compression (quality 11 = maximum)
  if command -v brotli >/dev/null 2>&1; then
    echo "   → Creating .br files (brotli -q 11)..."
    find "${dir}" -type f \( -name '*.js' -o -name '*.css' -o -name '*.json' -o -name '*.wasm' -o -name '*.svg' -o -name '*.html' -o -name '*.woff2' -o -name '*.otf' -o -name '*.ttf' \) ! -name '*.gz' ! -name '*.br' -exec brotli -f -k -q 11 {} \;
    echo "   ✅ Brotli compression completed"
  else
    echo "   ⚠️  brotli not available, skipping .br artifacts"
  fi
  
  # Calculate compression stats
  local original_size=$(du -sk "${dir}" | cut -f1)
  echo "   📊 Total size: ${original_size}KB (includes compressed files)"
}

echo "🚀 Building PRODUCTION export (optimized)..."
write_prod_env

echo "🧹 Cleaning previous artifacts..."
flutter clean || true

echo "📦 Getting Flutter packages..."
flutter pub get

echo "🔨 Flutter build web (release, MAXIMUM optimization for T3 delivery)..."
# --release: Build ở chế độ release (minified, optimized)
# --pwa-strategy=none: Tắt PWA service worker (giảm kích thước)
# --no-source-maps: Không tạo source maps (giảm kích thước build)
# --tree-shake-icons: Chỉ include icons được sử dụng (giảm kích thước fonts)
# --dart-define=FLUTTER_WEB_USE_SKIA=false: Force HTML renderer (không dùng CanvasKit)
flutter build web \
  --release \
  --pwa-strategy=none \
  --no-source-maps \
  --tree-shake-icons \
  --dart-define=FLUTTER_WEB_USE_SKIA=false

copy_x_app_sdk

echo "📁 Preparing export directory: ${OUT_DIR}"
mkdir -p "${OUT_DIR}"
cp -r build/web/* "${OUT_DIR}/"

# Post-process index.html to fix duplicate loading issues and remove Branch SDK
echo "🔧 Post-processing index.html to prevent duplicate script loading and remove Branch SDK..."
if [ -f "${OUT_DIR}/index.html" ]; then
  python3 - "${OUT_DIR}/index.html" <<'PYTHON_SCRIPT'
import re
import sys

file_path = sys.argv[1]
with open(file_path, 'r', encoding='utf-8') as f:
    content = f.read()

# Remove preload for main.dart.js if exists
content = re.sub(
    r'<link\s+rel="preload"\s+href="main\.dart\.js"[^>]*>',
    '<!-- Removed: main.dart.js preload (flutter_bootstrap.js will load it) -->',
    content,
    flags=re.IGNORECASE
)

# Ensure flutterConfiguration.renderer is set
if 'window.flutterConfiguration.renderer' not in content:
    # Add renderer config before flutter_bootstrap.js loads
    content = re.sub(
        r'(window\.flutterConfiguration\s*=\s*window\.flutterConfiguration\s*\|\|\s*\{\};)',
        r"\1\n    window.flutterConfiguration.renderer = 'html';",
        content
    )

# Remove duplicate script tags for flutter_bootstrap.js (keep only the last one)
script_tags = list(re.finditer(r'<script[^>]*src=["\']flutter_bootstrap\.js["\'][^>]*>', content, re.IGNORECASE))
if len(script_tags) > 1:
    # Keep only the last one
    for match in script_tags[:-1]:
        content = content[:match.start()] + '<!-- Removed duplicate -->' + content[match.end():]

# Ensure flutter_bootstrap.js has async defer
content = re.sub(
    r'(<script[^>]*src=["\']flutter_bootstrap\.js["\'][^>]*)(>)',
    r'\1 async defer\2',
    content,
    flags=re.IGNORECASE
)

# Remove Branch SDK for web (only needed for mobile)
content = re.sub(
    r'<link[^>]*cdn\.branch\.io[^>]*>',
    '<!-- Removed: Branch SDK preconnect (not needed for web) -->',
    content,
    flags=re.IGNORECASE
)
content = re.sub(
    r'<script[^>]*branch[^>]*>.*?</script>',
    '<!-- Removed: Branch SDK (not needed for web) -->',
    content,
    flags=re.IGNORECASE | re.DOTALL
)

with open(file_path, 'w', encoding='utf-8') as f:
    f.write(content)
PYTHON_SCRIPT
  echo "✅ Post-processed index.html"
fi

if [ -f "web/firebase-messaging-sw.js" ] && [ ! -f "${OUT_DIR}/firebase-messaging-sw.js" ]; then
  cp web/firebase-messaging-sw.js "${OUT_DIR}/"
fi

# Helper script for local preview with SPA fallback
cat > "${OUT_DIR}/serve_local.sh" <<'EOF'
#!/usr/bin/env bash

PORT="${1:-8080}"

python3 - "$PORT" <<'PY'
import http.server
import socketserver
import os
import sys

ROOT = os.getcwd()
PORT = int(sys.argv[1])

class SPAHandler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        resolved = self.translate_path(self.path)
        if not os.path.exists(resolved):
            self.path = '/index.html'
        return super().do_GET()

    def log_message(self, fmt, *args):
        print(f"🌐 {fmt % args}")

with socketserver.TCPServer(('', PORT), SPAHandler) as httpd:
    print(f"🚀 Serving {ROOT} at http://localhost:{PORT}")
    print("💡 SPA fallback enabled. Ctrl+C to stop.")
    httpd.serve_forever()
PY
EOF
chmod +x "${OUT_DIR}/serve_local.sh"

if [ "${KEEP_CANVASKIT:-0}" != "1" ]; then
  echo "🧹 Removing CanvasKit bundle to shrink export (set KEEP_CANVASKIT=1 to keep)..."
  rm -rf "${OUT_DIR}/canvaskit"
else
  echo "ℹ️ KEEP_CANVASKIT=1 → giữ nguyên thư mục canvaskit."
fi

echo "🗜️ Precompressing assets with maximum compression..."
compress_assets "${OUT_DIR}"

# Calculate final sizes
echo ""
echo "📊 Build size summary:"
echo "   Original files:"
du -sh "${OUT_DIR}" --exclude="*.gz" --exclude="*.br" 2>/dev/null || du -sh "${OUT_DIR}"
echo "   With compression:"
du -sh "${OUT_DIR}"

echo ""
echo "📦 Creating zip archive ${ZIP_FILE} (maximum compression)..."
zip -rq -9 "${ZIP_FILE}" "${OUT_DIR}"

echo ""
echo "🎉 PRODUCTION export ready (MAXIMUM OPTIMIZATION for T3 delivery)!"
echo "   📁 Folder : ${OUT_DIR}"
echo "   📦 Zip    : ${ZIP_FILE}"
echo ""
echo "📊 Final sizes:"
zip_size=$(du -sh "${ZIP_FILE}" | cut -f1)
folder_size=$(du -sh "${OUT_DIR}" | cut -f1)
echo "   📦 Zip file: ${zip_size}"
echo "   📁 Folder: ${folder_size}"
echo ""
echo "📌 Delivery notes for T3:"
echo "   ✅ All assets are pre-compressed (gzip + brotli)"
echo "   ✅ CanvasKit removed (saves ~1.6MB)"
echo "   ✅ Source maps removed (saves space)"
echo "   ✅ PWA disabled (simpler deployment)"
echo "   ✅ Tree-shaken icons (only used icons included)"
echo ""
echo "   💡 Server should serve with Content-Encoding: gzip or br"
echo "   💡 Check .gz and .br files are available for optimal performance"
echo ""
echo "▶️ Quick preview command:"
echo "   cd ${OUT_DIR} && ./serve_local.sh 8080"
echo "⚠️  Avoid using 'python3 -m http.server' directly; it does not handle SPA routes and will 404 on deep links."
