server {
    listen 80;
    server_name official.inkreach.cc;

    client_max_body_size 10m;

    # ACME challenge for cert renewals
    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

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

server {
    listen 443 ssl;
    http2 on;
    server_name official.inkreach.cc;

    ssl_certificate     /etc/nginx/certs/official.inkreach.cc_ecc/fullchain.cer;
    ssl_certificate_key /etc/nginx/certs/official.inkreach.cc_ecc/official.inkreach.cc.key;
    ssl_protocols TLSv1.2 TLSv1.3;

    client_max_body_size 10m;

    root /usr/share/nginx/html;

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

    # Admin SPA
    location /admin/ {
        try_files $uri $uri/ /admin/index.html;
    }
    location = /admin {
        return 301 /admin/;
    }

    # API: strip the /api prefix before proxying to the NestJS container
    location /api/ {
        proxy_pass http://api:3001/;
        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;
    }

    location = /h5 {
        return 301 /h5/;
    }

    location /h5/{
        try_files $uri $uri/ /h5/index.html;
    }

    # Uploaded files served by the API
    location /uploads/ {
        proxy_pass http://api:3001/uploads/;
        proxy_set_header Host $host;
    }

    # Static product assets served by the API
    location /assets/ {
        proxy_pass http://api:3001/assets/;
        proxy_set_header Host $host;
    }

    # Website placeholder until the public site is deployed
    location / {
        return 302 /admin/;
    }
}
