#!/bin/bash

# Variables - replace these with your desired values
ADMIN_USER="admin"
ADMIN_PASSWORD=$(openssl rand -base64 32)
DB_PASS="ZSI8R1LFJVOqX65EmBsBA1OQPO2arA=="
CONTAINER_NAME="mailserver_db"

echo "Creating MySQL admin user with root-like privileges..."

# Execute MySQL commands inside the container
docker exec -it $CONTAINER_NAME mysql -uroot -p${DB_PASS} -e "
DROP USER IF EXISTS '$ADMIN_USER'@'%';
CREATE USER '$ADMIN_USER'@'%' IDENTIFIED BY '$ADMIN_PASSWORD';
GRANT ALL PRIVILEGES ON *.* TO '$ADMIN_USER'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
SELECT User, Host FROM mysql.user WHERE User = '$ADMIN_USER';
"

# Check if the command was successful
if [ $? -eq 0 ]; then
    echo "✅ MySQL admin user '$ADMIN_USER' created successfully!"
    echo "Connection details:"
    echo "  - Username: $ADMIN_USER"
    echo "  - Password: $ADMIN_PASSWORD"
    echo "  - Host: localhost"
    echo "  - Port: 3806 (mapped port)"
else
    echo "❌ Failed to create MySQL admin user."
    echo "Please check your container status and credentials."
fi