# Production Deployment Guide

## Pre-Deployment Checklist

### 1. Environment Configuration
- [ ] Copy `.env.production` to `.env` on production server
- [ ] Generate secure JWT_SECRET: `openssl rand -base64 64`
- [ ] Update MONGODB_URI with production database credentials
- [ ] Set DOMAIN to your production domain
- [ ] Configure ALLOWED_ORIGINS for CORS
- [ ] Set NODE_ENV=production

### 2. Security
- [ ] Change default admin password (aa1@hotel.com)
- [ ] Enable HTTPS/SSL certificate
- [ ] Configure firewall rules
- [ ] Set up MongoDB IP whitelist
- [ ] Review and update rate limiting settings
- [ ] Enable MongoDB authentication

### 3. Database
- [ ] Create production MongoDB database
- [ ] Set up database backups
- [ ] Create database indexes (run: `node scripts/createIndexes.js`)
- [ ] Remove seed data if not needed

### 4. Application
```bash
# Install dependencies
npm ci --production

# Build/prepare application
npm run build  # if you have a build step

# Start with PM2
pm2 start ecosystem.config.js --env production
pm2 save
pm2 startup
```

### 5. Monitoring
- [ ] Set up PM2 monitoring: `pm2 monitor`
- [ ] Configure log rotation
- [ ] Set up error tracking (e.g., Sentry)
- [ ] Configure uptime monitoring

### 6. Performance
- [ ] Enable gzip compression
- [ ] Set up CDN for static assets
- [ ] Configure caching headers
- [ ] Optimize database queries

## Deployment Commands

### Using PM2 (Recommended)
```bash
# Start application
pm2 start ecosystem.config.js --env production

# View logs
pm2 logs anna-stockroom

# Restart application
pm2 restart anna-stockroom

# Stop application
pm2 stop anna-stockroom
```

### Using Docker (Alternative)
```bash
# Build image
docker build -t anna-stockroom .

# Run container
docker run -d -p 3000:3000 --env-file .env anna-stockroom
```

## Post-Deployment

### 1. Verify Deployment
- [ ] Check health endpoint: `curl https://your-domain.com/api/health`
- [ ] Test login functionality
- [ ] Verify database connectivity
- [ ] Test API endpoints
- [ ] Check error logs

### 2. Create Admin User
```bash
# If needed, create initial admin user
node scripts/createAdmin.js
```

### 3. Monitor
```bash
# Check application status
pm2 status

# Monitor logs
pm2 logs --lines 100

# Check system resources
pm2 monit
```

## Backup & Recovery

### Database Backup
```bash
# Backup MongoDB
mongodump --uri="your-mongodb-uri" --out=/backup/$(date +%Y%m%d)

# Restore MongoDB
mongorestore --uri="your-mongodb-uri" /backup/20240122
```

### Application Backup
```bash
# Backup uploads directory
tar -czf uploads-backup-$(date +%Y%m%d).tar.gz uploads/

# Backup environment config
cp .env .env.backup
```

## Troubleshooting

### Application won't start
1. Check logs: `pm2 logs anna-stockroom --err`
2. Verify environment variables: `pm2 env 0`
3. Check MongoDB connection
4. Verify port availability: `lsof -i :3000`

### Database connection issues
1. Verify MONGODB_URI in .env
2. Check MongoDB server status
3. Verify IP whitelist in MongoDB Atlas
4. Test connection: `node scripts/testConnection.js`

### Performance issues
1. Check PM2 metrics: `pm2 monit`
2. Review database indexes
3. Check rate limiting logs
4. Monitor memory usage

## Security Best Practices

1. **Never commit .env files to git**
2. **Use strong passwords** for all accounts
3. **Enable 2FA** for admin accounts
4. **Regular security updates**: `npm audit fix`
5. **Monitor access logs** for suspicious activity
6. **Implement IP whitelisting** for admin routes
7. **Regular database backups**
8. **Use HTTPS only** in production

## Scaling

### Horizontal Scaling
- Use load balancer (nginx, AWS ALB)
- Run multiple PM2 instances: `pm2 start ecosystem.config.js -i max`
- Use Redis for session management

### Database Scaling
- Enable MongoDB replica sets
- Implement read replicas
- Use connection pooling

## Support

For issues or questions:
- Check logs: `pm2 logs`
- Review documentation
- Contact support team
