#!/bin/bash

# Build Debug Version - Fixed for Chrome Extension
# Mục đích: Build debug với IIFE format để tránh import errors

echo "🔧 Building OdooFlow Debug Version (Chrome Extension Compatible)..."

# Clean previous builds
echo "🧹 Cleaning previous debug build..."
rm -rf dist-debug

# Step 1: Build main extension files (popup, options, sidepanel, service-worker)
echo "📦 Building main extension files..."
npx vite build --config vite.config.debug.ts

# Check if main build was successful
if [ $? -ne 0 ]; then
    echo "❌ Main build failed!"
    exit 1
fi

# Step 2: Build content script separately with IIFE format
echo "📦 Building content script with IIFE format..."

# Create temporary config for debug content script
cat > vite.config.content-debug.ts << 'EOF'
import { defineConfig } from 'vite'
import { resolve } from 'path'

export default defineConfig({
  build: {
    outDir: 'dist-content-debug',
    minify: false, // No minification for debugging
    sourcemap: true, // Enable sourcemaps for debugging
    lib: {
      entry: resolve(process.cwd(), 'src/content/core/content-script-core.ts'),
      name: 'OdooFlowContentScript',
      fileName: 'content-script',
      formats: ['iife']
    },
    rollupOptions: {
      output: {
        extend: true,
        globals: {
          chrome: 'chrome'
        }
      },
      external: ['chrome']
    }
  },
  resolve: {
    alias: {
      '@': resolve(process.cwd(), './src'),
    },
  },
  define: {
    __BUILD_TYPE__: JSON.stringify('debug'),
    __PREMIUM_FEATURES__: false,
    __DEV__: true
  }
})
EOF

npx vite build --config vite.config.content-debug.ts

# Check if content script build was successful
if [ $? -eq 0 ]; then
    echo "✅ Content script built successfully!"

    # Copy content script to debug folder
    echo "📋 Copying content script to debug folder..."
    cp dist-content-debug/content-script.iife.js dist-debug/content-script.js
    cp dist-content-debug/content-script.iife.js.map dist-debug/content-script.js.map 2>/dev/null || true

    # Clean temporary files
    rm -rf dist-content-debug
    rm vite.config.content-debug.ts

else
    echo "❌ Content script build failed!"
    rm -f vite.config.content-debug.ts
    exit 1
fi

# Step 3: Copy manifest and icons
echo "📋 Copying manifest and assets..."
cp public/manifest.json dist-debug/
cp -r public/icons dist-debug/

# Show build info
echo ""
echo "📊 Build Information:"
echo "  - Type: Debug (Unminified + IIFE)"
echo "  - Output: dist-debug/"
echo "  - Minification: Disabled"
echo "  - Source Maps: Enabled"
echo "  - Content Script: IIFE format (Chrome compatible)"

# Show file sizes
echo ""
echo "📁 Generated Files:"
ls -la dist-debug/*.js | awk '{print "  - " $9 ": " $5 " bytes"}'

echo ""
echo "🎉 Debug build ready for Chrome Extension!"
echo ""
echo "📋 To load in Chrome:"
echo "  1. Open chrome://extensions/"
echo "  2. Enable 'Developer mode'"
echo "  3. Click 'Load unpacked'"
echo "  4. Select the 'dist-debug' folder"
echo ""
echo "🔍 Debug Features:"
echo "  - Readable variable names"
echo "  - Source maps for debugging"
echo "  - IIFE format (no import errors)"
echo "  - Single bundled content script"
