# Redirect HTTP to HTTPS
server {
    listen 80;
    server_name vp.vish.gg api.vp.vish.gg proxy.vp.vish.gg;

    return 301 https://$host$request_uri;
}

# HTTPS Reverse Proxy for Piped
server {
    listen 443 ssl http2;
    server_name vp.vish.gg;

    # SSL Certificates (managed by Certbot)
    ssl_certificate /etc/letsencrypt/live/vp.vish.gg/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/vp.vish.gg/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    # Proxy requests to Piped Frontend (use Docker service name, NOT 127.0.0.1)
    location / {
        proxy_pass http://127.0.0.1:8080;
        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;
    }
}

# HTTPS Reverse Proxy for Piped API
server {
    listen 443 ssl http2;
    server_name api.vp.vish.gg;

    # SSL Certificates
    ssl_certificate /etc/letsencrypt/live/vp.vish.gg/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/vp.vish.gg/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    # Proxy requests to Piped API backend
    location / {
        proxy_pass http://127.0.0.1:8080;
        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;
    }
}

# HTTPS Reverse Proxy for Piped Proxy (for video streaming)
server {
    listen 443 ssl http2;
    server_name proxy.vp.vish.gg;

    # SSL Certificates
    ssl_certificate /etc/letsencrypt/live/vp.vish.gg/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/vp.vish.gg/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    # Proxy video playback requests through ytproxy
    location ~ (/videoplayback|/api/v4/|/api/manifest/) {
        include snippets/ytproxy.conf;
        add_header Cache-Control private always;
        proxy_hide_header Access-Control-Allow-Origin;
    }

    location / {
        include snippets/ytproxy.conf;
        add_header Cache-Control "public, max-age=604800";
        proxy_hide_header Access-Control-Allow-Origin;
    }
}
