# Production Deployment Instructions

## Prerequisites
- Ubuntu/Debian server with root access
- Server IP: 10.0.0.240
- PostgreSQL server at: 10.0.0.207
- Domains configured:
  - api.parentflowapp.com → 10.0.0.240:3020
  - web.parentflowapp.com → 10.0.0.240:3030
  - adminpf.parentflowapp.com → 10.0.0.240:3335

## Quick Installation (One Command)

SSH into your production server and run:

```bash
cd /root && \
git clone https://andrei:33edc%40%40NHY%5E%5E@git.noru1.ro/andrei/maternal-app.git parentflow-production && \
cd parentflow-production && \
git checkout main && \
chmod +x deploy-production.sh && \
./deploy-production.sh
```

This will automatically:
1. Install Node.js 22 and all dependencies
2. Install Docker and Docker Compose
3. Start all required services (Redis, MongoDB, MinIO)
4. Run database migrations
5. Build all applications
6. Start PM2 services

## Manual Step-by-Step Installation

### 1. Clone Repository
```bash
cd /root
git clone https://andrei:33edc%40%40NHY%5E%5E@git.noru1.ro/andrei/maternal-app.git parentflow-production
cd parentflow-production
git checkout main
```

### 2. Make Scripts Executable
```bash
chmod +x deploy-production.sh
chmod +x migrate-production.sh
chmod +x start-production.sh
chmod +x stop-production.sh
```

### 3. Run Initial Deployment
```bash
./deploy-production.sh
```

This script will:
- Install Node.js 22
- Install Docker and Docker Compose
- Clone the latest code from main branch
- Install all npm dependencies
- Start Docker services (Redis, MongoDB, MinIO)
- Run database migrations
- Build production apps
- Start PM2 services

### 4. Verify Installation
After deployment completes, verify all services are running:

```bash
# Check PM2 services
pm2 list

# Check service health
curl http://localhost:3020/health      # Backend API
curl http://localhost:3030              # Frontend
curl http://localhost:3335              # Admin Dashboard

# Check Docker services
docker ps
```

## Service Management

### Start All Services
```bash
cd /root/parentflow-production
./start-production.sh
```

### Stop All Services
```bash
cd /root/parentflow-production
./stop-production.sh
```

### Run Database Migrations Only
```bash
cd /root/parentflow-production
./migrate-production.sh
```

### View Logs
```bash
# PM2 logs
pm2 logs parentflow-backend-prod
pm2 logs parentflow-frontend-prod
pm2 logs parentflow-admin-prod

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

## Update Production

To update production with the latest code:

```bash
cd /root/parentflow-production
git pull origin main
./deploy-production.sh
```

## Service Ports

| Service | Port | URL |
|---------|------|-----|
| Backend API | 3020 | https://api.parentflowapp.com |
| Frontend | 3030 | https://web.parentflowapp.com |
| Admin Dashboard | 3335 | https://adminpf.parentflowapp.com |
| Redis | 6379 | Internal only |
| MongoDB | 27017 | Internal only |
| MinIO | 9000 | Internal only |
| MinIO Console | 9001 | http://10.0.0.240:9001 |

## Database Access

Production database is hosted on dedicated server:
- Host: 10.0.0.207
- Port: 5432
- Database: parentflow
- User: postgres
- Password: a3ppq

To access database directly:
```bash
PGPASSWORD=a3ppq psql -h 10.0.0.207 -p 5432 -U postgres -d parentflow
```

## Admin Dashboard

Access the admin dashboard at: https://adminpf.parentflowapp.com

Default admin credentials:
- Email: admin@parentflowapp.com
- Password: admin123

**IMPORTANT**: Change the admin password after first login!

## Troubleshooting

### Services Not Starting
```bash
# Check PM2 status
pm2 status

# Check error logs
pm2 logs --err

# Restart specific service
pm2 restart parentflow-backend-prod
pm2 restart parentflow-frontend-prod
pm2 restart parentflow-admin-prod
```

### Port Conflicts
```bash
# Check what's using a port
lsof -i :3020  # Backend
lsof -i :3030  # Frontend
lsof -i :3335  # Admin

# Kill process on port (if needed)
kill -9 $(lsof -t -i:3020)
```

### Database Connection Issues
```bash
# Test database connection
PGPASSWORD=a3ppq psql -h 10.0.0.207 -p 5432 -U postgres -d parentflow -c "\dt"

# Check migrations status
cd /root/parentflow-production
./migrate-production.sh
```

### Docker Issues
```bash
# Restart Docker services
docker-compose -f docker-compose.production.yml down
docker-compose -f docker-compose.production.yml up -d

# Check Docker logs
docker logs parentflow-redis -f
docker logs parentflow-mongodb -f
```

## Monitoring

### PM2 Monitoring
```bash
# Enable PM2 web monitoring
pm2 web

# Save PM2 configuration
pm2 save
pm2 startup
```

### System Resources
```bash
# Check memory usage
free -h

# Check disk space
df -h

# Check CPU usage
htop
```

## Backup

### Database Backup
```bash
# Create backup
PGPASSWORD=a3ppq pg_dump -h 10.0.0.207 -p 5432 -U postgres -d parentflow > backup_$(date +%Y%m%d).sql

# Restore backup
PGPASSWORD=a3ppq psql -h 10.0.0.207 -p 5432 -U postgres -d parentflow < backup_20240101.sql
```

### Application Backup
```bash
# Backup uploads and data
tar -czf parentflow_data_$(date +%Y%m%d).tar.gz /root/parentflow-production/uploads
```

## Security Notes

1. **Change default passwords** immediately after installation:
   - Admin dashboard password
   - Database passwords (if using defaults)
   - Redis password
   - MongoDB password
   - MinIO credentials

2. **Configure firewall** to only allow necessary ports:
   - 80, 443 (HTTP/HTTPS)
   - 22 (SSH - consider changing default port)
   - Block direct access to service ports from outside

3. **Enable SSL/TLS** using Let's Encrypt:
   ```bash
   apt-get install certbot python3-certbot-nginx
   certbot --nginx -d api.parentflowapp.com -d web.parentflowapp.com -d adminpf.parentflowapp.com
   ```

4. **Regular Updates**:
   ```bash
   # Update system packages
   apt update && apt upgrade -y

   # Update Node.js packages (with caution)
   cd /root/parentflow-production
   npm audit fix
   ```

## Support

For issues or questions:
1. Check PM2 logs: `pm2 logs`
2. Check application logs in `/root/parentflow-production/logs/`
3. Review error logs: `pm2 logs --err`
4. Check system logs: `journalctl -xe`