===============================================================================
PRODUCTION DEPLOYMENT INSTRUCTIONS - PostgreSQL Migration
===============================================================================

Git changes have been pushed. Since you have auto-pull set up, follow these
steps on your PRODUCTION SERVER to complete the migration:

===============================================================================
STEP 1: After Git Auto-Pull Completes
===============================================================================

SSH into your production server and navigate to the Connect-5 directory.

===============================================================================
STEP 2: Install New Dependencies
===============================================================================

npm install

This will:
- Remove @supabase/supabase-js
- Install pg (PostgreSQL driver)

===============================================================================
STEP 3: Create db.config.js with PostgreSQL Credentials
===============================================================================

IMPORTANT: You need to create db.config.js on the production server.

Option A - Using the example file:
-----------------------------------
cp db.config.example.js db.config.js
nano db.config.js

Then edit with your PostgreSQL credentials:
{
    HOST: '202.171.184.108',
    USER: 'postgres',
    PASSWORD: 'X@gon2005!#$',
    DB: 'connect5',
    dialect: 'postgres',
    pool: { max: 5, min: 0, acquire: 30000, idle: 10000 }
}

Option B - Quick create (copy/paste this entire block):
--------------------------------------------------------
cat > db.config.js << 'EOF'
module.exports = {
    HOST: '202.171.184.108',
    USER: 'postgres',
    PASSWORD: 'X@gon2005!#$',
    DB: 'connect5',
    dialect: 'postgres',
    pool: {
        max: 5,
        min: 0,
        acquire: 30000,
        idle: 10000
    }
};
EOF

===============================================================================
STEP 4: Apply Database Schema (if not already done)
===============================================================================

Run the schema application script:

node apply-schema.js

Expected output:
✅ Schema applied successfully!
✅ Found 4 tables: active_sessions, game_moves, games, players

If you see errors about tables already existing, that's fine - skip this step.

===============================================================================
STEP 5: Restart the Node.js Service
===============================================================================

Depending on how you're running the server:

Option A - If using PM2:
------------------------
pm2 restart connect5
# OR
pm2 restart server.js
# OR
pm2 restart all

Option B - If using nohup:
--------------------------
pkill -f "node server.js"
sleep 2
nohup node server.js > server.log 2>&1 &

Option C - If using systemd service:
------------------------------------
sudo systemctl restart connect5
# OR
sudo systemctl restart connect5.service

Option D - Not sure what's running it?
--------------------------------------
# Check what's running
ps aux | grep "node server.js"

# Kill the process (replace PID with actual process ID)
kill -9 PID

# Start it again
nohup node server.js > server.log 2>&1 &

===============================================================================
STEP 6: Verify the Service is Running
===============================================================================

Check if the server started successfully:

# View the logs
tail -f server.log

Expected output:
🔄 Initializing PostgreSQL database schema...
✅ Database schema verified successfully
🌐 Server running on port 3000
📡 WebSocket server ready
🗄️  Database connected

# Test the API endpoint
curl http://localhost:3000/api/db-status

Expected response:
{
  "connected": true,
  "latency": 10,
  "writeCapable": true,
  "connectionType": "PostgreSQL Direct Connection",
  "host": "202.171.184.108",
  "database": "connect5"
}

===============================================================================
STEP 7: Test the Production URL
===============================================================================

Visit your production URL (e.g., https://connect5.beyondcloud.technology/)

Verify:
✅ Page loads correctly
✅ Status bar shows "Connected" in green
✅ Latency is displayed
✅ "Write: Enabled" shows in green
✅ Multiplayer functionality works

===============================================================================
SUMMARY OF SERVICES TO RESTART
===============================================================================

You need to restart:
✅ Node.js service (server.js) - This is the ONLY service that needs restarting

You DO NOT need to restart:
❌ Nginx/Apache (web server) - No changes to proxy config
❌ PostgreSQL - Database server continues running
❌ The production server itself

===============================================================================
TROUBLESHOOTING
===============================================================================

If the service won't start:
1. Check db.config.js exists and has correct credentials
2. Check logs: tail -f server.log
3. Test database connection: node apply-schema.js
4. Ensure PostgreSQL is accessible from production server

Common issues:
- "Cannot find module './db.config.js'" → Create db.config.js (Step 3)
- "Table 'players' does not exist" → Run apply-schema.js (Step 4)
- "ECONNREFUSED" → Check PostgreSQL firewall/network access

===============================================================================
