user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        # Docker DNS resolver
        resolver 127.0.0.11 valid=30s;

        # Health proxy endpoint - forwards to internal python service
        location /healthcheck {
            proxy_pass http://health-proxy:8081$request_uri;
            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_connect_timeout 3s;
            proxy_read_timeout 5s;
        }

        # Order service API - forwards to order-service
        location /api/order {
            proxy_pass http://order-service:8082$request_uri;
            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 Content-Type application/json;
            proxy_connect_timeout 3s;
            proxy_read_timeout 5s;
        }

        # Manifest with proper content type
        location = /manifest.json {
            root   /usr/share/nginx/html;
            add_header Content-Type application/manifest+json;
            add_header Cache-Control "public, max-age=3600";
        }

        # Service worker with proper headers
        location = /sw.js {
            root   /usr/share/nginx/html;
            add_header Content-Type application/javascript;
            add_header Cache-Control "no-cache, no-store, must-revalidate";
            add_header Service-Worker-Allowed "/";
        }

        # Markdown files with proper content type
        location ~ \.md$ {
            root   /usr/share/nginx/html;
            add_header Content-Type text/markdown;
            add_header Cache-Control "no-cache";
        }

        # Serve static files
        location / {
            root   /usr/share/nginx/html;
            try_files $uri $uri/ =404;
        }

        error_page  404 /404.html;
    }
}
