# WineBox nginx configuration
# Place in /etc/nginx/sites-available/winebox and symlink to sites-enabled
# Handles two domains: winebox.app (landing) and booze.winebox.app (app)

upstream winebox_app {
    server 127.0.0.1:8000;
    keepalive 32;
}

# =============================================================================
# winebox.app - Landing Page Only
# =============================================================================

# Redirect HTTP to HTTPS for landing page
server {
    listen 80;
    listen [::]:80;
    server_name winebox.app www.winebox.app;

    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        return 301 https://winebox.app$request_uri;
    }
}

# Redirect www to non-www
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.winebox.app;

    ssl_certificate /etc/letsencrypt/live/winebox.app/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/winebox.app/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/winebox.app/chain.pem;

    return 301 https://winebox.app$request_uri;
}

# HTTPS server for landing page
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name winebox.app;

    # SSL certificates (managed by certbot)
    ssl_certificate /etc/letsencrypt/live/winebox.app/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/winebox.app/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/winebox.app/chain.pem;

    # SSL configuration
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;

    # HSTS
    add_header Strict-Transport-Security "max-age=63072000" always;

    # Logging
    access_log /var/log/nginx/winebox-landing.access.log;
    error_log /var/log/nginx/winebox-landing.error.log;

    # Root for landing page
    root /opt/winebox/static;

    # Gzip compression
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;

    # Landing page
    location = / {
        try_files /landing.html =404;
    }

    # Static assets for landing page
    location /static/ {
        alias /opt/winebox/static/;
        expires 30d;
        add_header Cache-Control "public, immutable";
    }

    # CSS files
    location /css/ {
        alias /opt/winebox/static/css/;
        expires 30d;
        add_header Cache-Control "public, immutable";
    }

    # Favicon (SVG)
    location = /favicon.svg {
        alias /opt/winebox/static/favicon.svg;
        expires 30d;
        add_header Content-Type "image/svg+xml";
    }

    # Legacy favicon.ico redirect to SVG
    location = /favicon.ico {
        return 301 /favicon.svg;
    }
}

# =============================================================================
# booze.winebox.app - Full Application
# =============================================================================

# Redirect HTTP to HTTPS for app
server {
    listen 80;
    listen [::]:80;
    server_name booze.winebox.app;

    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        return 301 https://booze.winebox.app$request_uri;
    }
}

# HTTPS server for app
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name booze.winebox.app;

    # SSL certificates (managed by certbot)
    ssl_certificate /etc/letsencrypt/live/booze.winebox.app/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/booze.winebox.app/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/booze.winebox.app/chain.pem;

    # SSL configuration
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;

    # HSTS
    add_header Strict-Transport-Security "max-age=63072000" always;

    # Logging
    access_log /var/log/nginx/winebox.access.log;
    error_log /var/log/nginx/winebox.error.log;

    # Max upload size (for wine label images)
    client_max_body_size 10M;

    # Gzip compression
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;

    # Static files (served via symlink to package location)
    location /static/ {
        alias /opt/winebox/static/;
        expires 30d;
        add_header Cache-Control "public, immutable";
    }

    # API and application
    location / {
        proxy_pass http://winebox_app;
        proxy_http_version 1.1;
        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_set_header Connection "";
        proxy_buffering off;
        proxy_read_timeout 300s;
        proxy_connect_timeout 75s;
    }

    # Health check endpoint (no logging)
    location /health {
        proxy_pass http://winebox_app/health;
        access_log off;
    }
}
