# Redirect HTTP to HTTPS
server {
    listen 80;
    listen [::]:80;
    server_name mawidi.com www.mawidi.com;

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

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

# Main HTTPS server
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name mawidi.com www.mawidi.com;

    # SSL Configuration
    ssl_certificate /etc/nginx/ssl/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/privkey.pem;
    ssl_session_timeout 1d;
    ssl_session_cache shared:MozSSL:10m;
    ssl_session_tickets off;

    # Modern SSL configuration
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;

    # HSTS
    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;

    # Content Security Policy
    add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://www.googletagmanager.com https://www.google-analytics.com https://unpkg.com https://elevenlabs.io https://*.elevenlabs.io https://js.stripe.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com data:; img-src 'self' data: https: blob: https://js.stripe.com https://storage.googleapis.com; connect-src 'self' https://api.mawidi.com https://www.google-analytics.com https://*.convex.cloud https://*.convex.site https://api.stripe.com https://api.elevenlabs.io https://api.us.elevenlabs.io wss://api.elevenlabs.io wss://api.us.elevenlabs.io https://elevenlabs.io https://*.elevenlabs.io; frame-src 'self' https://www.youtube.com https://www.google.com https://js.stripe.com https://hooks.stripe.com; worker-src 'self' blob: https://unpkg.com https://elevenlabs.io https://*.elevenlabs.io;" always;

    # Security headers
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;

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

    # Root directory
    root /var/www/mawidi;

    # Max upload size
    client_max_body_size 50M;

    # Proxy settings for Next.js
    location / {
        proxy_pass http://mawidi-prod:9000;
        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;

        # Cache settings
        proxy_cache mawidi_cache;
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;
        proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
        proxy_cache_background_update on;
        proxy_cache_lock on;

        # Timeouts
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
    }

    # API routes (no cache)
    location /api {
        proxy_pass http://mawidi-prod:9000;
        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;

        # No cache for API
        proxy_no_cache 1;
        proxy_cache_bypass 1;

        # Rate limiting
        limit_req zone=api_limit burst=20 nodelay;
    }

    # Static files
    location /_next/static {
        proxy_pass http://mawidi-prod:9000;
        proxy_cache mawidi_cache;
        proxy_cache_valid 200 365d;
        expires 365d;
        add_header Cache-Control "public, max-age=31536000, immutable";
    }

    # Images and media
    location ~* \.(jpg|jpeg|png|gif|ico|svg|webp|woff|woff2|ttf|otf|eot)$ {
        proxy_pass http://mawidi-prod:9000;
        proxy_cache mawidi_cache;
        proxy_cache_valid 200 30d;
        expires 30d;
        add_header Cache-Control "public, max-age=2592000";
    }

    # Health check endpoint
    location /health {
        access_log off;
        add_header Content-Type text/plain;
        return 200 'OK';
    }

    # Block common attack vectors
    location ~* \.(asp|aspx|cgi|dll|exe|jsp|msp|phtml|php|pl|py|rb|sh|sql|swf)$ {
        deny all;
        return 404;
    }

    # Block access to hidden files
    location ~ /\. {
        deny all;
        return 404;
    }

    # Block access to backup files
    location ~* \.(bak|backup|old|orig|original|swp|tmp)$ {
        deny all;
        return 404;
    }
}