# HTTP Server - Serves both localhost and redirects skyarts.ddns.net to HTTPS
server {
    listen 80;
    listen [::]:80;
    server_name localhost skyarts.ddns.net;
    
    # Redirect HTTPS for skyarts.ddns.net only
    if ($host = skyarts.ddns.net) {
        return 301 https://$server_name$request_uri;
    }
    
    # For localhost, serve the site
    # Logs
    access_log /var/log/nginx/skyartshop-access.log;
    error_log /var/log/nginx/skyartshop-error.log;

    # Root directory
    root /var/www/skyartshop/public;
    index index.html;

    # Admin area - exact matches to redirect
    location = /admin {
        return 302 /admin/login.html;
    }

    location = /admin/ {
        return 302 /admin/login.html;
    }

    # API proxy to Node.js backend
    location /api/ {
        proxy_pass http://localhost:5000;
        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-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;
        
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
    }

    # Static files
    location /assets/ {
        alias /var/www/skyartshop/assets/;
        expires 30d;
        add_header Cache-Control "public, immutable";
    }

    location /uploads/ {
        alias /var/www/skyartshop/uploads/;
        expires 30d;
        add_header Cache-Control "public";
    }

    # Admin static files
    location /admin/ {
        alias /var/www/skyartshop/admin/;
        try_files $uri $uri/ =404;
    }

    # Root redirect handled by backend
    location = / {
        proxy_pass http://localhost:5000;
        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;
    }

    # Health check
    location /health {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
    }
}

# HTTPS - Main Server for skyarts.ddns.net
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name skyarts.ddns.net;

    # SSL Configuration
    ssl_certificate /etc/letsencrypt/live/skyarts.ddns.net/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/skyarts.ddns.net/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    # Security Headers
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;

    # Logs
    access_log /var/log/nginx/skyartshop-access.log;
    error_log /var/log/nginx/skyartshop-error.log;

    # Root directory
    root /var/www/skyartshop/public;
    index index.html;

    # Admin area - exact matches to redirect
    location = /admin {
        return 302 /admin/login.html;
    }

    location = /admin/ {
        return 302 /admin/login.html;
    }

    # API proxy to Node.js backend
    location /api/ {
        proxy_pass http://localhost:5000;
        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-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;
        
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
    }

    # Static files
    location /assets/ {
        alias /var/www/skyartshop/assets/;
        expires 30d;
        add_header Cache-Control "public, immutable";
    }

    location /uploads/ {
        alias /var/www/skyartshop/uploads/;
        expires 30d;
        add_header Cache-Control "public";
    }

    # Admin static files
    location /admin/ {
        alias /var/www/skyartshop/admin/;
        try_files $uri $uri/ =404;
    }

    # Root redirect handled by backend
    location = / {
        proxy_pass http://localhost:5000;
        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;
    }

    # Health check
    location /health {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
    }
}
