upstream php-fpm {
    server backend:9000;
}

upstream frontend-server {
    server frontend:80;
}

server {
    listen 80;
    server_name _;

    client_max_body_size 25M;

    # API requests -> PHP-FPM backend
    location /api {
        root /var/www/html/public;

        fastcgi_pass php-fpm;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/html/public/index.php;
        include fastcgi_params;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    # Sanctum CSRF cookie
    location /sanctum {
        root /var/www/html/public;

        fastcgi_pass php-fpm;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/html/public/index.php;
        include fastcgi_params;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    # Storage files (uploaded images, etc.)
    location /storage {
        alias /var/www/html/storage/app/public/app/public;
        expires 7d;
        add_header Cache-Control "public";
        try_files $uri =404;
        charset utf-8;
    }

    # Everything else -> Vue SPA frontend
    location / {
        proxy_pass http://frontend-server;
        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;
    }
}
