#!/bin/bash

echo "🚀 Dubai DLD Analytics API Setup"
echo "================================="

# Check if Node.js is installed
if ! command -v node &> /dev/null; then
    echo "❌ Node.js is not installed. Please install Node.js 16+ first."
    exit 1
fi

# Check if MySQL is installed
if ! command -v mysql &> /dev/null; then
    echo "❌ MySQL is not installed. Please install MySQL 8.0+ first."
    exit 1
fi

echo "✅ Prerequisites check passed"

# Install dependencies
echo "📦 Installing dependencies..."
npm install

# Create .env file if it doesn't exist
if [ ! -f .env ]; then
    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=your_password

# NLP Configuration
NLP_LIBRARY=natural

# API Configuration
API_KEY=your_api_key_here
RATE_LIMIT=100
EOF
    echo "⚠️  Please update .env with your database credentials"
fi

# Database setup
echo "🗄️  Setting up database..."
echo "Please enter your MySQL root password:"
read -s MYSQL_PASSWORD

if [ -f "2.sql" ]; then
    echo "📊 Importing database schema..."
    mysql -u root -p$MYSQL_PASSWORD < 2.sql
    if [ $? -eq 0 ]; then
        echo "✅ Database schema imported successfully"
    else
        echo "❌ Database import failed. Please check your credentials and try again."
        exit 1
    fi
else
    echo "⚠️  SQL file (2.sql) not found. Please ensure it's in the project root."
fi

echo ""
echo "🎉 Setup complete!"
echo ""
echo "Next steps:"
echo "1. Update .env with your database credentials"
echo "2. Start the server: npm run dev"
echo "3. Open http://localhost:3000/health to test"
echo "4. Open public/index.html in your browser for the demo"
echo ""
echo "Available endpoints:"
echo "- POST /api/query - Custom queries"
echo "- GET /api/queries/top-areas - Top areas"
echo "- GET /api/queries/project-summary - Project summary"
echo "- GET /health - Health check"

