#!/bin/bash
# ========================================================================
# Script to run Organization 2 Migration
# This script creates Bangladesh Army (org_id=2) with units, departments, and users
# ========================================================================

echo "========================================================================"
echo "Organization 2 Migration - Bangladesh Army"
echo "========================================================================"
echo ""
echo "This script will create:"
echo "- 1 Organization: Bangladesh Army (ID: 2)"
echo "- 2 Units: Army Headquarters, Dhaka Cantonment"
echo "- 3 Departments: Operations, Administration, Logistics"
echo "- 3 Users: Army Super Admin, Army Admin, Army User"
echo ""
echo "Default password for all users: admin123"
echo ""
echo "========================================================================"
echo ""

# Get MySQL credentials
read -p "Enter MySQL username (default: root): " MYSQL_USER
MYSQL_USER=${MYSQL_USER:-root}

read -sp "Enter MySQL password: " MYSQL_PASSWORD
echo ""

# Check if migration file exists
if [ ! -f "migration_add_org2_data.sql" ]; then
    echo "ERROR: migration_add_org2_data.sql not found!"
    echo "Please ensure you are running this from the backend/database directory."
    exit 1
fi

echo ""
echo "Running migration..."
echo ""

# Run the migration
mysql -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" cms_db < migration_add_org2_data.sql

# Check if successful
if [ $? -eq 0 ]; then
    echo ""
    echo "========================================================================"
    echo "Migration completed successfully!"
    echo "========================================================================"
    echo ""
    echo "Created users for Bangladesh Army:"
    echo "- Email: army_admin@army.mil.bd | Password: admin123 | Role: Army Super Admin"
    echo "- Email: admin@army.mil.bd      | Password: admin123 | Role: Army Admin"
    echo "- Email: user@army.mil.bd       | Password: admin123 | Role: Army User"
    echo ""
    echo "To verify the migration, run:"
    echo "  mysql -u $MYSQL_USER -p cms_db"
    echo "  SELECT * FROM organizations WHERE id = 2;"
    echo "  SELECT * FROM users WHERE organization_id = 2;"
    echo ""
    echo "See MIGRATION_ORG2_README.md for more details."
    echo "========================================================================"
else
    echo ""
    echo "========================================================================"
    echo "ERROR: Migration failed!"
    echo "========================================================================"
    echo ""
    echo "Please check:"
    echo "1. MySQL credentials are correct"
    echo "2. Database 'cms_db' exists"
    echo "3. You have necessary permissions"
    echo ""
    echo "Check the error message above for details."
    echo "========================================================================"
    exit 1
fi

