# 🚀 Deploy ParentFlow to Production - Step by Step

## Quick Deploy Instructions

### Step 1: Connect to Production Server

Open your terminal and connect via SSH:
```bash
ssh root@10.0.0.240
# Password: a3pq5t50yA@#
```

### Step 2: Download and Run Deployment Script

Once connected to the server, run these commands:

```bash
# Download the deployment script
wget https://git.noru1.ro/andrei/maternal-app/raw/branch/main/deploy-to-production.sh

# Make it executable
chmod +x deploy-to-production.sh

# Run the deployment
./deploy-to-production.sh
```

### Step 3: Edit Environment Variables (First Time Only)

The script will pause and ask you to edit `.env.production`.
You need to update these critical values:

```bash
# Edit the file
nano /root/maternal-app/.env.production
```

Key values to update:
- `JWT_SECRET` - Generate with: `openssl rand -base64 64`
- `JWT_REFRESH_SECRET` - Generate with: `openssl rand -base64 64`
- `OPENAI_API_KEY` - Your OpenAI API key for AI features
- Redis, MongoDB, MinIO passwords (keep defaults or change)

Press `Ctrl+X`, then `Y`, then `Enter` to save and exit.

Then press `Enter` in the deployment script to continue.

### Step 4: Verify Deployment

After the script completes, verify everything is running:

```bash
# Check PM2 processes
pm2 status

# Check Docker containers
docker ps

# Check if services are accessible
curl http://localhost:3020/api/health
curl http://localhost:3030
```

## What the Script Does

1. ✅ Clones the repository from Gitea
2. ✅ Installs Node.js 18, PM2, Docker, Docker Compose
3. ✅ Installs all npm dependencies
4. ✅ Builds backend and frontend
5. ✅ Starts Docker containers (Redis, MongoDB, MinIO)
6. ✅ Runs database migrations on 10.0.0.207
7. ✅ Starts PM2 processes for backend (3020) and frontend (3030)
8. ✅ Sets up PM2 to restart on system reboot

## After Deployment

### Test the Application

From your local machine:
1. Backend Health: http://10.0.0.240:3020/api/health
2. Frontend: http://10.0.0.240:3030

### Production URLs (Already configured in Nginx)

- API: https://api.parentflowapp.com
- Web: https://web.parentflowapp.com

### Management Commands

On the production server:

```bash
# View logs
pm2 logs

# Restart services
pm2 restart all

# Stop services
pm2 stop all

# View real-time metrics
pm2 monit

# Docker logs
docker logs parentflow-redis-prod
docker logs parentflow-mongodb-prod
```

## Troubleshooting

### If backend doesn't start:
```bash
pm2 logs parentflow-backend-prod --err
cd /root/maternal-app/maternal-app/maternal-app-backend
npm run build
pm2 restart parentflow-backend-prod
```

### If frontend doesn't start:
```bash
pm2 logs parentflow-frontend-prod --err
cd /root/maternal-app/maternal-web
npm run build
pm2 restart parentflow-frontend-prod
```

### If database connection fails:
```bash
# Test connection
PGPASSWORD=a3ppq psql -h 10.0.0.207 -p 5432 -U postgres -d parentflow -c "SELECT version();"

# Check migrations
cd /root/maternal-app/maternal-app/maternal-app-backend
./scripts/check-migrations.sh
```

### To update after changes:
```bash
cd /root/maternal-app
git pull origin main
cd maternal-app/maternal-app-backend
npm install
npm run build
cd ../../maternal-web
npm install
npm run build
pm2 restart all
```

## Success Indicators

You'll know deployment is successful when:
- ✅ `pm2 status` shows both processes as "online"
- ✅ `docker ps` shows 3 containers running
- ✅ Backend responds at http://10.0.0.240:3020/api/health
- ✅ Frontend loads at http://10.0.0.240:3030
- ✅ You can register/login at https://web.parentflowapp.com

## Support

If you encounter issues:
1. Check logs: `pm2 logs`
2. Check Docker: `docker ps` and `docker logs [container-name]`
3. Verify PostgreSQL connection to 10.0.0.207
4. Ensure ports 3020 and 3030 are not blocked

---

**Ready to deploy! Just SSH to the server and run the 3 commands in Step 2.**