# Backend Decoupling - Deployment Ready Summary

## What's Been Done ✓

The backend has been successfully decoupled from the translate-press-zone plugin and is ready for standalone deployment.

### Repository Structure
```
wp-content/
├── press-zone-backend/          # NEW: Standalone backend
│   ├── api/                     # Node.js API server
│   ├── admin-panel/             # React admin dashboard (planned)
│   ├── backup/                  # Backup/restore scripts
│   ├── nginx/                   # Nginx + SSL configuration
│   ├── systemd/                 # Systemd service files
│   ├── deploy.sh                # Full deployment script
│   ├── update.sh                # Quick update script
│   ├── podman-compose.yml       # Container orchestration
│   ├── SERVER-SETUP.md          # Complete setup guide
│   └── MIGRATION.md             # Migration instructions
│
└── plugins/translate-press-zone/
    └── press-zone-backend/             # OLD: Will be removed after migration
```

### Files Committed & Pushed to GitHub
- ✅ All 176 files committed
- ✅ Pushed to github.com:avi-ezra/Press.Zone-Works.git
- ✅ Commit: 8ab366a8

---

## Next Steps - Server Deployment

### Option 1: Automated Migration (Recommended)

Follow the detailed guide in **MIGRATION.md**:

```bash
# On server as user 'press'
cd ~
git clone git@github.com:avi-ezra/Press.Zone-Works.git
cd Press.Zone-Works/press-zone-backend

# Follow MIGRATION.md steps
```

### Option 2: Quick Manual Steps

#### 1. Pull Latest Code on Server
```bash
# SSH as user 'press'
ssh press@100.109.41.8

cd ~/Press.Zone-Works
git pull origin master
cd press-zone-backend
```

#### 2. Copy Environment Configuration
```bash
# Copy existing .env from old location
cp ~/domains/dev3.press.zone/public_html/wp-content/plugins/translate-press-zone/press-zone-backend/api/.env \
   ~/Press.Zone-Works/press-zone-backend/api/.env
```

#### 3. Install Dependencies & Build
```bash
cd ~/Press.Zone-Works/press-zone-backend/api
npm ci --production
npx prisma generate
npm run build
```

#### 4. Install Systemd Service
```bash
cd ~/Press.Zone-Works/press-zone-backend/systemd
./install-systemd.sh
```

#### 5. Install Nginx + SSL (as root)
```bash
sudo su
cd /home/press/Press.Zone-Works/press-zone-backend/nginx
./install-nginx.sh
```

#### 6. Start New Backend
```bash
# As user 'press'
systemctl --user start presszone-backend.service
systemctl --user status presszone-backend.service

# Verify health
curl http://localhost:3000/health
```

#### 7. Stop Old Backend
```bash
# Find and kill old processes
ps aux | grep -E "tsx.*src/(index|worker)" | grep -v grep
kill <PID>  # Replace with actual PIDs

# Verify new backend via HTTPS
curl https://api.press.zone/health
```

#### 8. Clean Up Old Directory (Wait 24-48 hours)
```bash
# Only after confirming everything works
rm -rf ~/domains/dev3.press.zone/public_html/wp-content/plugins/translate-press-zone/press-zone-backend/
```

---

## Key Configuration Files

### 1. Environment Variables (`api/.env`)
Already exists on server. Copy from old location:
```
DATABASE_URL=postgresql://translate_user:password@localhost:5432/translate_db
REDIS_HOST=localhost
REDIS_PORT=6379
GEMINI_API_KEY=...
GEMINI_MODEL=gemini-3-flash-preview
# ... etc
```

### 2. Systemd Service
Location: `~/.config/systemd/user/presszone-backend.service`

Commands:
```bash
systemctl --user start presszone-backend
systemctl --user stop presszone-backend
systemctl --user restart presszone-backend
systemctl --user status presszone-backend
journalctl --user -u presszone-backend -f
```

### 3. Nginx Configuration
Location: `/etc/nginx/conf.d/api.press.zone.conf`

- Reverse proxy to localhost:3000
- SSL via Let's Encrypt
- Auto-renewal configured

---

## Verification Checklist

After deployment, verify:

- [ ] API health: `curl https://api.press.zone/health`
- [ ] SSL certificate valid (check in browser)
- [ ] WordPress plugin connects successfully
- [ ] Translations work end-to-end
- [ ] Background workers processing jobs
- [ ] Systemd service survives reboot
- [ ] Logs are clean: `journalctl --user -u presszone-backend -n 50`
- [ ] Database backups scheduled: `crontab -l`

---

## Important Notes

### Current Server State
- **Running on**: dev3.press.zone (not dev1!)
- **Old backend**: `~/domains/dev3.press.zone/.../press-zone-backend/`
- **Services**: Native npm processes (not containers yet)
- **Database**: PostgreSQL + Redis on host
- **Podman**: Installed (v5.6.0)

### DNS Configuration
- Ensure `api.press.zone` points to server IP
- SSL will be obtained automatically by certbot

### Zero-Downtime Strategy
1. New backend starts on port 3000 (same as old)
2. Old backend must be stopped first
3. Brief downtime: ~30 seconds during cutover
4. Nginx provides HTTPS layer

---

## Rollback Plan

If issues occur:

```bash
# Stop new backend
systemctl --user stop presszone-backend.service

# Start old backend
cd ~/domains/dev3.press.zone/public_html/wp-content/plugins/translate-press-zone/press-zone-backend/api
npm run dev &
npm run worker &

# Verify
curl http://localhost:3000/health
```

---

## Support Resources

- **Setup Guide**: `press-zone-backend/SERVER-SETUP.md`
- **Migration Guide**: `press-zone-backend/MIGRATION.md`
- **API README**: `press-zone-backend/README.md`
- **Connect Script**: `./connect.sh` (connects as user press)

---

## Backup Strategy

Automated daily backups at 2 AM:

```bash
# Setup cron
crontab -e

# Add:
0 2 * * * $HOME/press-zone-backend/backup/backup.sh >> $HOME/press-zone-backend/backup/backup.log 2>&1
```

Manual backup:
```bash
cd ~/press-zone-backend/backup
./backup.sh
```

Restore:
```bash
cd ~/press-zone-backend/backup
./restore.sh backup/postgres_20240126_020000.dump.gz
```

---

## Estimated Migration Time

- **Preparation**: 30 minutes
- **Deployment**: 45 minutes
- **Cutover**: 5 minutes (downtime: ~30 seconds)
- **Verification**: 30 minutes
- **Total**: ~2 hours

---

## Ready to Deploy?

1. Read `press-zone-backend/MIGRATION.md` completely
2. Schedule maintenance window (brief downtime needed)
3. Notify users of planned maintenance
4. Follow migration steps carefully
5. Keep old backend directory for 24-48 hours before deleting

**Questions?** Check the troubleshooting sections in MIGRATION.md and SERVER-SETUP.md
