# Next.js Frontend Deployment Guide

This guide will help you deploy the Next.js frontend to Ubuntu server.

## Prerequisites

- Ubuntu 20.04 LTS or later
- Root or sudo access
- Backend API running on port 8001
- Node.js 20.x or later

## Quick Deployment

### Option 1: Automated Deployment Script

```bash
# Run the deployment script (as root)
sudo bash scripts/deploy_nextjs.sh
```

This script will:
- Install Node.js and npm if needed
- Install dependencies
- Build the application
- Set up log directories
- Install and enable systemd service

### Option 2: Manual Setup

```bash
# 1. Install Node.js (if not already installed)
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash -
sudo apt-get install -y nodejs

# 2. Navigate to Next.js directory
cd /home/plagis/workspace/plagis_aumentum/plagis-nextjs

# 3. Install dependencies
npm ci

# 4. Build the application
npm run build

# 5. Install systemd service
sudo cp scripts/plagis-nextjs.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable plagis-nextjs
sudo systemctl start plagis-nextjs
```

## Configuration

### Environment Variables

The service uses these default environment variables:
- `NODE_ENV=production`
- `PORT=3000`
- `NEXT_PUBLIC_API_URL=http://localhost:8001`

To override these, create a `.env.production` file:

```bash
cd /home/plagis/workspace/plagis_aumentum/plagis-nextjs
sudo -u plagis nano .env.production
```

Add:
```bash
NODE_ENV=production
PORT=3000
NEXT_PUBLIC_API_URL=http://localhost:8001
```

Then uncomment the `EnvironmentFile` line in the service file:
```bash
sudo nano /etc/systemd/system/plagis-nextjs.service
# Uncomment: EnvironmentFile=/home/plagis/workspace/plagis_aumentum/plagis-nextjs/.env.production
sudo systemctl daemon-reload
sudo systemctl restart plagis-nextjs
```

### Changing the Port

If you need to run on a different port:

1. Update the service file:
   ```bash
   sudo nano /etc/systemd/system/plagis-nextjs.service
   # Change: Environment="PORT=3000"
   ```

2. Reload and restart:
   ```bash
   sudo systemctl daemon-reload
   sudo systemctl restart plagis-nextjs
   ```

## Service Management

### Start/Stop/Restart

```bash
sudo systemctl start plagis-nextjs
sudo systemctl stop plagis-nextjs
sudo systemctl restart plagis-nextjs
```

### Check Status

```bash
sudo systemctl status plagis-nextjs
```

### View Logs

```bash
# View all logs
sudo journalctl -u plagis-nextjs -f

# View application logs
sudo tail -f /var/log/plagis/nextjs.log

# View error logs
sudo tail -f /var/log/plagis/nextjs-error.log
```

## Verification

### Test the Application

```bash
# Check if service is running
curl http://localhost:3000

# Check health (if you have a health endpoint)
curl http://localhost:3000/api/health
```

### Check Service Status

```bash
sudo systemctl status plagis-nextjs
```

Should show:
- `Active: active (running)`
- `Main PID: <process_id>`

## Boot Configuration

The service is configured to:
- ✅ Start automatically on boot (`enabled`)
- ✅ Wait for network to be ready
- ✅ Wait for backend API to be ready
- ✅ Auto-restart on failure

### Verify Boot Configuration

```bash
# Check if enabled
sudo systemctl is-enabled plagis-nextjs

# Should output: enabled
```

## Troubleshooting

### Service Won't Start

1. Check service status:
   ```bash
   sudo systemctl status plagis-nextjs
   ```

2. Check logs:
   ```bash
   sudo journalctl -u plagis-nextjs -n 50
   ```

3. Verify Node.js:
   ```bash
   node --version
   npm --version
   ```

4. Check if port is in use:
   ```bash
   sudo lsof -i :3000
   ```

### Build Errors

If build fails:

```bash
# Clean and rebuild
cd /home/plagis/workspace/plagis_aumentum/plagis-nextjs
rm -rf .next node_modules
npm ci
npm run build
```

### Port Already in Use

If port 3000 is already in use:

```bash
# Find process using port 3000
sudo lsof -i :3000

# Kill the process or change port in service file
```

### Backend API Not Available

The service waits for the backend API. If backend is not running:

```bash
# Check backend status
sudo systemctl status plagis-aumentum-api

# Start backend if needed
sudo systemctl start plagis-aumentum-api
```

## Updating the Application

### Update Code

```bash
cd /home/plagis/workspace/plagis_aumentum/plagis-nextjs

# Pull latest changes (if using git)
git pull

# Rebuild
npm ci
npm run build

# Restart service
sudo systemctl restart plagis-nextjs
```

### Update Dependencies

```bash
cd /home/plagis/workspace/plagis_aumentum/plagis-nextjs

# Update package.json, then:
npm ci
npm run build
sudo systemctl restart plagis-nextjs
```

## Firewall Configuration

If you need to expose the frontend externally:

```bash
# Allow port 3000
sudo ufw allow 3000/tcp

# Or for specific IP
sudo ufw allow from 192.168.1.0/24 to any port 3000
```

## Reverse Proxy (Recommended for Production)

For production, use Nginx as a reverse proxy:

### Install Nginx

```bash
sudo apt-get install -y nginx
```

### Create Nginx Configuration

```bash
sudo nano /etc/nginx/sites-available/plagis-frontend
```

Add:
```nginx
server {
    listen 80;
    server_name your-domain.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;
    }
}
```

Enable site:
```bash
sudo ln -s /etc/nginx/sites-available/plagis-frontend /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
```

## Monitoring

### Health Check

Create a simple health check endpoint or use:
```bash
curl -I http://localhost:3000
```

### Set Up Monitoring

Consider using:
- **PM2** for process management (alternative to systemd)
- **Nginx** for reverse proxy and load balancing
- **Logwatch** for log monitoring
- **Monit** for process monitoring

## Summary

✅ **The Next.js frontend will automatically start on boot**

The configuration ensures:
- Service starts after network is ready
- Service waits for backend API
- Service auto-restarts if it crashes
- Everything is configured for automatic startup

