# Nginx Config Template for Docker Local Development
# Uses environment variables from .env files
# Variables: DEPLOYMENT_NAME, ROOM_NAME, MANAGED_DOMAIN, SOLEPRINT_DOMAIN

# =============================================================================
# MANAGED APP WITH WRAPPER - amar.room.local.com
# =============================================================================
server {
    listen 80;
    server_name ${MANAGED_DOMAIN};

    # Wrapper static files
    location /wrapper/ {
        alias /app/wrapper/;
        add_header Cache-Control "no-cache";
    }

    # Backend API
    location /api/ {
        proxy_pass http://${DEPLOYMENT_NAME}_backend:8000/api/;
        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_read_timeout 300;
    }

    # Django admin
    location /admin/ {
        proxy_pass http://${DEPLOYMENT_NAME}_backend:8000/admin/;
        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;
    }

    # Django static files
    location /static/ {
        proxy_pass http://${DEPLOYMENT_NAME}_backend:8000/static/;
    }

    # Frontend with wrapper injection
    location / {
        proxy_pass http://${DEPLOYMENT_NAME}_frontend:3000;
        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_read_timeout 300;

        # WebSocket support for Next.js hot reload
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        # Inject wrapper scripts into HTML
        sub_filter '</head>' '<link rel="stylesheet" href="/wrapper/sidebar.css"><script src="/wrapper/sidebar.js"></script></head>';
        sub_filter_once on;
        proxy_set_header Accept-Encoding "";
    }
}

# =============================================================================
# MANAGED APP WITHOUT WRAPPER - amar.local.com
# =============================================================================
server {
    listen 80;
    server_name amar.local.com;

    # Backend API
    location /api/ {
        proxy_pass http://${DEPLOYMENT_NAME}_backend:8000/api/;
        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_read_timeout 300;
    }

    # Django admin
    location /admin/ {
        proxy_pass http://${DEPLOYMENT_NAME}_backend:8000/admin/;
        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;
    }

    # Django static files
    location /static/ {
        proxy_pass http://${DEPLOYMENT_NAME}_backend:8000/static/;
    }

    # Frontend (clean, no wrapper)
    location / {
        proxy_pass http://${DEPLOYMENT_NAME}_frontend:3000;
        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_read_timeout 300;

        # WebSocket support for Next.js hot reload
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

# =============================================================================
# SOLEPRINT - Main Service + Ecosystem
# =============================================================================
server {
    listen 80;
    server_name ${SOLEPRINT_DOMAIN};

    # Artery - API Gateway
    location /artery/ {
        proxy_pass http://${ROOM_NAME}_artery:8000/;
        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;
    }

    # Album - Media Service
    location /album/ {
        proxy_pass http://${ROOM_NAME}_album:8000/;
        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;
    }

    # Ward - Admin Interface
    location /ward/ {
        proxy_pass http://${ROOM_NAME}_ward:8000/;
        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;
    }

    # Soleprint - Main Service (default)
    location / {
        proxy_pass http://${ROOM_NAME}_soleprint:8000;
        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;
    }
}
