# Deploying Atlas to adampowell.pro

## Server Details
- **Server:** 198.211.114.12 (root access)
- **Domain:** adampowell.pro
- **Web Root:** /var/www/adampowell.pro/
- **URL:** https://adampowell.pro/atlas

## Deployment Steps

### 1. Connect to Server

```bash
ssh -i "$env:USERPROFILE\.ssh\id_ed25519" root@198.211.114.12
```

### 2. Install Node.js (if not already installed)

```bash
# Check if Node.js is installed
node --version
npm --version

# If not installed, install Node.js 18+
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
apt-get install -y nodejs
```

### 3. Upload Atlas to Server

From your Windows machine:

```bash
# Copy Atlas folder to server
scp -i "$env:USERPROFILE\.ssh\id_ed25519" -r "C:\ADAMANT\ADAMPOWELL PRO\adampowell.pro\Atlas" root@198.211.114.12:/var/www/adampowell.pro/
```

### 4. Configure Atlas on Server

```bash
# SSH into server
ssh -i "$env:USERPROFILE\.ssh\id_ed25519" root@198.211.114.12

# Navigate to Atlas
cd /var/www/adampowell.pro/Atlas/broker

# Create .env file
cp .env.example .env
nano .env
```

Edit `.env` with:
```env
# OpenAI API Key
OPENAI_API_KEY=sk-your-actual-api-key

# JWT Secrets (generate random strings)
JWT_SECRET=your-random-secret-min-32-characters
PAIRING_SECRET=your-random-pairing-secret-min-32-chars

# Server Configuration
PORT=3001

# Data Directory
DATA_DIR=/var/www/adampowell.pro/Atlas/data

# Environment
NODE_ENV=production
```

Install dependencies:
```bash
npm install --production
```

### 5. Create systemd Service

```bash
nano /etc/systemd/system/atlas.service
```

Add this content:

```ini
[Unit]
Description=Atlas Broker Server
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/var/www/adampowell.pro/Atlas/broker
ExecStart=/usr/bin/node /var/www/adampowell.pro/Atlas/broker/src/server.js
Restart=always
RestartSec=10
Environment=NODE_ENV=production

# Logging
StandardOutput=journal
StandardError=journal
SyslogIdentifier=atlas

[Install]
WantedBy=multi-user.target
```

Enable and start the service:

```bash
systemctl daemon-reload
systemctl enable atlas
systemctl start atlas
systemctl status atlas
```

### 6. Configure Nginx Reverse Proxy

```bash
nano /etc/nginx/sites-available/adampowell.pro
```

Add this location block to your existing server config:

```nginx
server {
    listen 80;
    listen [::]:80;
    server_name adampowell.pro www.adampowell.pro;

    # ... your existing config ...

    # Atlas PWA
    location /atlas {
        alias /var/www/adampowell.pro/Atlas/pwa/public;
        try_files $uri $uri/ /atlas/index.html;

        # PWA headers
        add_header Cache-Control "public, max-age=0, must-revalidate";
        add_header Service-Worker-Allowed "/atlas";
    }

    # Atlas API
    location /api/ {
        proxy_pass http://localhost:3001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;
    }

    # WebSocket for PC Agent
    location /agent {
        proxy_pass http://localhost:3001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_read_timeout 86400;
    }
}
```

Test and reload nginx:

```bash
nginx -t
systemctl reload nginx
```

### 7. Enable HTTPS with Let's Encrypt

```bash
# Install certbot if not already installed
apt-get update
apt-get install -y certbot python3-certbot-nginx

# Get SSL certificate (if you don't have one already)
certbot --nginx -d adampowell.pro -d www.adampowell.pro

# Certbot will automatically update your nginx config for HTTPS
```

### 8. Update PWA for Production

On the server, update the service worker and manifest:

```bash
cd /var/www/adampowell.pro/Atlas/pwa/public
nano manifest.json
```

Update `start_url` and `scope`:
```json
{
  "start_url": "/atlas/",
  "scope": "/atlas/",
  ...
}
```

Update `sw.js` to use correct paths:
```javascript
const CACHE_NAME = 'atlas-v6';
const BASE_PATH = '/atlas';
const urlsToCache = [
  `${BASE_PATH}/`,
  `${BASE_PATH}/index.html`,
  `${BASE_PATH}/styles.css`,
  // ... etc
];
```

### 9. Access Atlas

1. **On your iPhone**, open Safari and go to:
   ```
   https://adampowell.pro/atlas
   ```

2. **Install as PWA:**
   - Tap Share button
   - Tap "Add to Home Screen"
   - Tap "Add"

3. **You're done!** Atlas is now available at a permanent URL with HTTPS.

### 10. Update PC Agent Configuration

On your Windows PC, update the PC Agent to connect to your server:

```bash
cd Atlas/pc-agent
nano .env
```

Change `BROKER_URL`:
```env
BROKER_URL=wss://adampowell.pro/agent
```

Note: Use `wss://` (WebSocket Secure) instead of `ws://` for HTTPS.

## Monitoring & Maintenance

### View logs:
```bash
journalctl -u atlas -f
```

### Restart service:
```bash
systemctl restart atlas
```

### Check status:
```bash
systemctl status atlas
```

### Update Atlas:
```bash
cd /var/www/adampowell.pro/Atlas/broker
git pull  # if using git
npm install --production
systemctl restart atlas
```

## Security Considerations

1. **Firewall:** Make sure port 3001 is NOT exposed directly (nginx proxies to it)
2. **Environment Variables:** Keep .env file secure (chmod 600)
3. **Regular Updates:** Keep Node.js and dependencies updated
4. **Backups:** Backup `/var/www/adampowell.pro/Atlas/data` regularly

## Testing

After deployment, test:
- [ ] https://adampowell.pro/atlas loads PWA
- [ ] Device pairing works
- [ ] Voice transcription works
- [ ] Tasks can be created/completed
- [ ] Memory system works
- [ ] PC Agent can connect from your Windows machine
- [ ] PWA installs on iPhone
- [ ] Offline mode works

You're now running Atlas on your own domain! 🚀
