#!/bin/bash

# Post-install script to fix ele-admin-pro compatibility with AntDv4
# This script replaces colorPalette() calls with hardcoded color values

echo "🔧 Fixing ele-admin-pro for AntDv4 compatibility..."

ELE_ADMIN_PATH="node_modules/ele-admin-pro"

if [ ! -d "$ELE_ADMIN_PATH" ]; then
    echo "⚠️  ele-admin-pro not found in node_modules, skipping fix"
    exit 0
fi

# Find all Less files with colorPalette calls and fix them
find "$ELE_ADMIN_PATH" -name "*.less" -type f | while read file; do
    if grep -q "colorPalette" "$file" 2>/dev/null; then
        echo "  🔨 Fixing: $file"
        
        # Replace primary color palette
        sed -i.bak "s/~colorPalette('@\{primary-color\}',[[:space:]]*1)/#e6f4ff/g" "$file"
        sed -i.bak "s/~colorPalette('@\{primary-color\}',[[:space:]]*2)/#bae0ff/g" "$file"
        sed -i.bak "s/~colorPalette('@\{primary-color\}',[[:space:]]*3)/#91caff/g" "$file"
        sed -i.bak "s/~colorPalette('@\{primary-color\}',[[:space:]]*4)/#69b1ff/g" "$file"
        sed -i.bak "s/~colorPalette('@\{primary-color\}',[[:space:]]*5)/#4096ff/g" "$file"
        sed -i.bak "s/~colorPalette('@\{primary-color\}',[[:space:]]*6)/#1677ff/g" "$file"
        sed -i.bak "s/~colorPalette('@\{primary-color\}',[[:space:]]*7)/#0958d9/g" "$file"
        sed -i.bak "s/~colorPalette('@\{primary-color\}',[[:space:]]*8)/#003eb3/g" "$file"
        sed -i.bak "s/~colorPalette('@\{primary-color\}',[[:space:]]*9)/#002c8c/g" "$file"
        sed -i.bak "s/~colorPalette('@\{primary-color\}',[[:space:]]*10)/#001d66/g" "$file"
        
        # Replace success color palette
        sed -i.bak "s/~colorPalette('@\{success-color\}',[[:space:]]*1)/#f6ffed/g" "$file"
        sed -i.bak "s/~colorPalette('@\{success-color\}',[[:space:]]*2)/#d9f7be/g" "$file"
        sed -i.bak "s/~colorPalette('@\{success-color\}',[[:space:]]*3)/#b7eb8f/g" "$file"
        sed -i.bak "s/~colorPalette('@\{success-color\}',[[:space:]]*4)/#95de64/g" "$file"
        sed -i.bak "s/~colorPalette('@\{success-color\}',[[:space:]]*5)/#73d13d/g" "$file"
        sed -i.bak "s/~colorPalette('@\{success-color\}',[[:space:]]*6)/#52c41a/g" "$file"
        sed -i.bak "s/~colorPalette('@\{success-color\}',[[:space:]]*7)/#389e0d/g" "$file"
        sed -i.bak "s/~colorPalette('@\{success-color\}',[[:space:]]*8)/#237804/g" "$file"
        sed -i.bak "s/~colorPalette('@\{success-color\}',[[:space:]]*9)/#135200/g" "$file"
        sed -i.bak "s/~colorPalette('@\{success-color\}',[[:space:]]*10)/#092b00/g" "$file"
        
        # Replace warning color palette
        sed -i.bak "s/~colorPalette('@\{warning-color\}',[[:space:]]*1)/#fffbe6/g" "$file"
        sed -i.bak "s/~colorPalette('@\{warning-color\}',[[:space:]]*2)/#fff1b8/g" "$file"
        sed -i.bak "s/~colorPalette('@\{warning-color\}',[[:space:]]*3)/#ffe58f/g" "$file"
        sed -i.bak "s/~colorPalette('@\{warning-color\}',[[:space:]]*4)/#ffd666/g" "$file"
        sed -i.bak "s/~colorPalette('@\{warning-color\}',[[:space:]]*5)/#ffc53d/g" "$file"
        sed -i.bak "s/~colorPalette('@\{warning-color\}',[[:space:]]*6)/#faad14/g" "$file"
        sed -i.bak "s/~colorPalette('@\{warning-color\}',[[:space:]]*7)/#d48806/g" "$file"
        sed -i.bak "s/~colorPalette('@\{warning-color\}',[[:space:]]*8)/#ad6800/g" "$file"
        sed -i.bak "s/~colorPalette('@\{warning-color\}',[[:space:]]*9)/#874d00/g" "$file"
        sed -i.bak "s/~colorPalette('@\{warning-color\}',[[:space:]]*10)/#612500/g" "$file"
        
        # Replace error color palette
        sed -i.bak "s/~colorPalette('@\{error-color\}',[[:space:]]*1)/#fff2f0/g" "$file"
        sed -i.bak "s/~colorPalette('@\{error-color\}',[[:space:]]*2)/#ffccc7/g" "$file"
        sed -i.bak "s/~colorPalette('@\{error-color\}',[[:space:]]*3)/#ffa39e/g" "$file"
        sed -i.bak "s/~colorPalette('@\{error-color\}',[[:space:]]*4)/#ff7875/g" "$file"
        sed -i.bak "s/~colorPalette('@\{error-color\}',[[:space:]]*5)/#ff4d4f/g" "$file"
        sed -i.bak "s/~colorPalette('@\{error-color\}',[[:space:]]*6)/#f5222d/g" "$file"
        sed -i.bak "s/~colorPalette('@\{error-color\}',[[:space:]]*7)/#cf1322/g" "$file"
        sed -i.bak "s/~colorPalette('@\{error-color\}',[[:space:]]*8)/#a8071a/g" "$file"
        sed -i.bak "s/~colorPalette('@\{error-color\}',[[:space:]]*9)/#820014/g" "$file"
        sed -i.bak "s/~colorPalette('@\{error-color\}',[[:space:]]*10)/#5c0011/g" "$file"
        
        # Remove backup files
        rm -f "$file.bak"
    fi
done

echo "✅ Fixed ele-admin-pro for AntDv4 compatibility"
