upstream auth-app {
    server auth-service:8000;
}
 
upstream account-app {
    server account-service:8000;
} 

upstream notif-app {
    server notification-service:8000;
} 

server {
    listen 80;
    server_name localhost;
 
    # Shared proxy settings
    location / {
        # Default configuration or index.html
        root /usr/share/nginx/html;
        index index.html;

        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;
    }

    # WebSocket proxy (if needed by clients)
    location /ws {
        proxy_pass http://auth-app/;
        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-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    # Simple CORS for APIs (no credentials)
    set $CORS_ORIGIN "*";

    location /auth/ {
        if ($request_method = OPTIONS) {
            add_header 'Access-Control-Allow-Origin' $CORS_ORIGIN always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, PATCH, DELETE, OPTIONS' always;
            add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type' always;
            return 204;
        }
        add_header 'Access-Control-Allow-Origin' $CORS_ORIGIN always;
        proxy_pass http://auth-app/api/v1/auth/; 
        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;
    } 

    location /auth/swagger { 
        proxy_pass http://auth-app/swagger;  
        alias /usr/share/nginx/html/swaggerui/;
    }
    
    # Token management endpoints
    location /tokens/ {
        if ($request_method = OPTIONS) {
            add_header 'Access-Control-Allow-Origin' $CORS_ORIGIN always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, PATCH, DELETE, OPTIONS' always;
            add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type' always;
            return 204;
        }
        add_header 'Access-Control-Allow-Origin' $CORS_ORIGIN always;
        proxy_pass http://auth-app/api/v1/tokens/; 
        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;
    }

    location /accounts/ {
        if ($request_method = OPTIONS) {
            add_header 'Access-Control-Allow-Origin' $CORS_ORIGIN always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, PATCH, DELETE, OPTIONS' always;
            add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type' always;
            return 204;
        }
        add_header 'Access-Control-Allow-Origin' $CORS_ORIGIN always;
        proxy_pass http://account-app/api/v1/accounts/; 
        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;
    }     
    
    location /account/swagger {
        proxy_pass http://account-app/swagger; 
    } 
 
    location /notification/ {
        if ($request_method = OPTIONS) {
            add_header 'Access-Control-Allow-Origin' $CORS_ORIGIN always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, PATCH, DELETE, OPTIONS' always;
            add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type' always;
            return 204;
        }
        add_header 'Access-Control-Allow-Origin' $CORS_ORIGIN always;
        proxy_pass http://notif-app/api/v1/notification/; 
        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;
    }     
    
    location /notification/swagger {
        proxy_pass http://notif-app/swagger; 
    } 
 
}