#!/bin/bash

# Script to transform dashboard views to add draggable widget support
# Usage: ./transform_dashboards.sh

DASHBOARDS=(
    "accounting"
    "crm"
    "inventory"
    "purchases"
    "manufacturing"
    "sales"
)

echo "🚀 Starting dashboard transformation..."
echo ""

for dashboard in "${DASHBOARDS[@]}"; do
    file="views/${dashboard}/dashboard.php"
    
    if [ ! -f "$file" ]; then
        echo "⚠️  Skipping $dashboard - file not found"
        continue
    fi
    
    echo "📝 Transforming $dashboard dashboard..."
    
    # Create backup
    cp "$file" "${file}.backup"
    
    # Add CSS/JS includes at the top (after any existing PHP opening tag)
    sed -i '' '1 a\
<!-- Include Dashboard Customization CSS and JS -->\
<link rel="stylesheet" href="<?= base_url('\''assets/css/dashboard-custom.css'\'') ?>">\
<meta name="base-url" content="<?= base_url() ?>">\
' "$file"
    
    echo "   ✓ Added CSS/JS includes"
    echo "   ℹ️  Manual steps needed for $dashboard:"
    echo "      1. Add 'Reset Layout' button to header"
    echo "      2. Wrap widgets in <div id=\"dashboard-widgets\" data-dashboard=\"$dashboard\">"
    echo "      3. Add widget-id and controls to each card"
    echo "      4. Add Sortable.js and dashboard-manager.js before closing"
    echo ""
done

echo "✅ Transformation complete!"
echo "📋 Backups created with .backup extension"
echo ""
echo "⚠️  Important: Review each dashboard and complete manual steps"
