#!/bin/bash

echo "🐳 Docker MySQL Setup for Dubai DLD Analytics"
echo "============================================="
echo ""

# Check if Docker is installed
if ! command -v docker &> /dev/null; then
    echo "❌ Docker is not installed. Please install Docker first."
    echo "   Visit: https://docs.docker.com/get-docker/"
    exit 1
fi

# Check if Docker Compose is installed
if ! command -v docker-compose &> /dev/null; then
    echo "❌ Docker Compose is not installed. Please install Docker Compose first."
    echo "   Visit: https://docs.docker.com/compose/install/"
    exit 1
fi

echo "✅ Docker and Docker Compose are installed"

# Check if 2.sql exists
if [ ! -f "2.sql" ]; then
    echo "❌ Database schema file (2.sql) not found in current directory"
    exit 1
fi

echo "✅ Database schema file found"

# Stop any existing MySQL container
echo "🛑 Stopping any existing MySQL container..."
docker stop dubai-mysql 2>/dev/null || true
docker rm dubai-mysql 2>/dev/null || true

# Start MySQL with Docker Compose
echo "🚀 Starting MySQL with Docker Compose..."
docker-compose up -d

# Wait for MySQL to be ready
echo "⏳ Waiting for MySQL to be ready..."
sleep 30

# Test connection
echo "🔍 Testing MySQL connection..."
max_attempts=10
attempt=1

while [ $attempt -le $max_attempts ]; do
    if docker exec dubai-mysql mysql -uroot -pAdmin@123 -e "SELECT 1;" >/dev/null 2>&1; then
        echo "✅ MySQL is ready!"
        break
    else
        echo "⏳ Attempt $attempt/$max_attempts - MySQL not ready yet..."
        sleep 5
        attempt=$((attempt + 1))
    fi
done

if [ $attempt -gt $max_attempts ]; then
    echo "❌ MySQL failed to start properly"
    echo "Check logs with: docker logs dubai-mysql"
    exit 1
fi

# Check if database was created
echo "🔍 Checking if database was created..."
DB_EXISTS=$(docker exec dubai-mysql mysql -uroot -pAdmin@123 -e "SHOW DATABASES LIKE 'dubai_dld';" 2>/dev/null | grep dubai_dld)

if [ -z "$DB_EXISTS" ]; then
    echo "⚠️  Database not found, creating it..."
    docker exec dubai-mysql mysql -uroot -pAdmin@123 -e "CREATE DATABASE dubai_dld CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
    
    echo "📊 Importing database schema..."
    docker exec -i dubai-mysql mysql -uroot -pAdmin@123 dubai_dld < 2.sql
else
    echo "✅ Database 'dubai_dld' already exists"
fi

# Check tables
echo "🔍 Checking tables..."
TABLE_COUNT=$(docker exec dubai-mysql mysql -uroot -pAdmin@123 dubai_dld -e "SHOW TABLES;" 2>/dev/null | wc -l)
echo "📊 Found $TABLE_COUNT tables in dubai_dld database"

# Create .env file
echo "📝 Creating .env file..."
cat > .env << EOF
# Server Configuration
PORT=3000
NODE_ENV=development

# Database Configuration
DB_HOST=localhost
DB_PORT=3306
DB_NAME=dubai_dld
DB_USER=root
DB_PASSWORD=Admin@123

# NLP Configuration
NLP_LIBRARY=natural

# API Configuration
API_KEY=your_api_key_here
RATE_LIMIT=100
EOF

echo "✅ Created .env file"

echo ""
echo "🎉 MySQL setup complete!"
echo ""
echo "Database Details:"
echo "- Host: localhost"
echo "- Port: 3306"
echo "- Username: root"
echo "- Password: Admin@123"
echo "- Database: dubai_dld"
echo ""
echo "Next steps:"
echo "1. Install Node.js dependencies: npm install"
echo "2. Start the API server: npm run dev"
echo "3. Test the API: ./test_api.sh"
echo "4. Open public/index.html for the dashboard"
echo ""
echo "To stop MySQL: docker-compose down"
echo "To view logs: docker logs dubai-mysql"

