# NextAV Deployment Guide

This guide will help you deploy NextAV using Docker for a production-ready setup.

## Quick Start

### Prerequisites
- Docker & Docker Compose
- At least 2GB RAM and 10GB disk space
- FFmpeg (for thumbnail generation)

### 1. Clone and Setup
```bash
git clone <your-repo-url> nextav
cd nextav
```

### 2. Configure Environment
```bash
# Copy environment file
cp .env.example .env

# Edit .env with your settings
nano .env
```

### 3. Deploy with Docker
```bash
# Make deploy script executable
chmod +x deploy.sh

# Run deployment
./deploy.sh
```

### 4. Access NextAV
Open your browser to `http://localhost:3000`

## Manual Deployment

### Using Docker Compose
```bash
# Build and start
docker-compose up -d --build

# View logs
docker-compose logs -f nextav

# Stop services
docker-compose down
```

### Production with SSL (Optional)
```bash
# For production with SSL
docker-compose --profile production up -d
```

## Configuration

### Environment Variables

| Variable | Description | Default |
|----------|-------------|---------|
| `DATABASE_URL` | SQLite database path | `file:./data/nextav.db` |
| `NODE_ENV` | Environment mode | `production` |
| `NEXT_PUBLIC_BASE_URL` | Base URL for the app | `http://localhost:3000` |
| `NEXT_PUBLIC_MEDIA_ROOT` | Media library root | `/app/media` |

### Directory Structure
```
nextav/
├── data/          # Database and app data
├── media/         # Media libraries
├── ssl/           # SSL certificates (optional)
├── docker-compose.yml
├── Dockerfile
├── .env
└── deploy.sh
```

## Media Libraries Setup

### Adding Media Libraries
1. Create directories in the `media/` folder:
   ```bash
   mkdir -p media/videos media/photos
   ```

2. Mount your existing media:
   ```bash
   # Example: mount existing photo library
   docker-compose down
   # Edit docker-compose.yml to add your volume
   docker-compose up -d
   ```

### Volume Mapping Examples
In `docker-compose.yml`:
```yaml
volumes:
  - /path/to/your/media:/app/media/yourlibrary
  - /path/to/another/library:/app/media/another
```

## Production Deployment

### SSL Certificates
```bash
# Create SSL directory
mkdir ssl

# Generate self-signed certificates (for testing)
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
  -keyout ssl/key.pem -out ssl/cert.pem

# For production, use Let's Encrypt certificates
```

### Reverse Proxy (Nginx)
The production profile includes Nginx with:
- SSL termination
- Gzip compression
- Security headers
- WebSocket support

### Health Checks
The application includes health checks at `/api/health` which verify:
- Database connectivity
- Media directory accessibility
- Application responsiveness

## Monitoring

### View Logs
```bash
# All services
docker-compose logs -f

# Specific service
docker-compose logs -f nextav
```

### Service Status
```bash
docker-compose ps
docker stats
```

## Troubleshooting

### Common Issues

**Port 3000 already in use:**
```bash
# Check what's using port 3000
lsof -i :3000

# Use different port
docker-compose up -d --scale nextav=1 --build
```

**Database permission issues:**
```bash
# Fix permissions
sudo chown -R 1001:1001 ./data
```

**FFmpeg not found:**
```bash
# Ensure FFmpeg is available
which ffmpeg
# Or use Docker container
```

### Reset Everything
```bash
# Stop and remove containers
docker-compose down

# Remove volumes (WARNING: deletes data)
docker-compose down -v

# Rebuild and start
docker-compose up -d --build
```

## Updates

### Updating NextAV
```bash
# Pull latest changes
git pull origin main

# Rebuild and restart
docker-compose down
docker-compose up -d --build
```

### Database Updates
For database schema changes:
```bash
# Backup database
cp data/nextav.db data/nextav.db.backup

# Apply updates (if needed)
docker-compose down
docker-compose up -d
```

## Security

### Basic Security
- Uses non-root user in container
- Runs with minimal privileges
- Includes security headers in production

### Production Hardening
- Use proper SSL certificates
- Configure firewall rules
- Set up log rotation
- Use secrets management for sensitive data

## Performance Tuning

### Resource Limits
Edit `docker-compose.yml` to set limits:
```yaml
services:
  nextav:
    deploy:
      resources:
        limits:
          cpus: '2.0'
          memory: 2G
        reservations:
          cpus: '0.5'
          memory: 512M
```

### Database Optimization
- SQLite is optimized for read-heavy workloads
- Consider PostgreSQL for high-traffic sites
- Regular database maintenance recommended

## Support

For issues and feature requests, please check:
1. Docker logs: `docker-compose logs`
2. Health endpoint: `curl http://localhost:3000/api/health`
3. System resources: `docker stats`


## Build/Push Docker image to private repo
Usage:
# Build & push to private registry
docker build -t 192.168.2.212:3000/tigeren/nextav:latest .
docker push 192.168.2.212:3000/tigeren/nextav:latest
