user nginx;
worker_processes auto;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    sendfile on;
    keepalive_timeout 65;
    client_max_body_size 20M;

    # ──────────────────────────────────────────────────────────────────
    # 1. HTTP GLOBAL REDIRECT TO HTTPS
    # ──────────────────────────────────────────────────────────────────
    server {
        listen 80;
        # Catches both subdomains hitting HTTP port 80
        server_name dev.jhphousing.org api.dev.jhphousing.org;

        location /.well-known/acme-challenge/ {
            alias /var/www/certbot/.well-known/acme-challenge/;
            allow all;
        }

        location / {
            return 301 https://$host$request_uri;
        }
    }

    # ──────────────────────────────────────────────────────────────────
    # 2. HTTPS: FRONTEND (dev.jhphousing.org)
    # ──────────────────────────────────────────────────────────────────
    server {
        listen 443 ssl;
        http2 on;
        server_name dev.jhphousing.org;

        ssl_certificate /etc/letsencrypt/live/dev.jhphousing.org/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/dev.jhphousing.org/privkey.pem;

        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;

        add_header Strict-Transport-Security "max-age=31536000" always;
        add_header X-Frame-Options SAMEORIGIN;
        add_header X-Content-Type-Options nosniff;

        location / {
            proxy_pass http://web:3000;
            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 https;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }

    # ──────────────────────────────────────────────────────────────────
    # 3. HTTPS: BACKEND API (api.dev.jhphousing.org)
    # ──────────────────────────────────────────────────────────────────
    server {
        listen 443 ssl http2;
        server_name api.dev.jhphousing.org;

        ssl_certificate /etc/letsencrypt/live/dev.jhphousing.org/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/dev.jhphousing.org/privkey.pem;

        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;

        add_header Strict-Transport-Security "max-age=31536000" always;
        add_header X-Frame-Options SAMEORIGIN;
        add_header X-Content-Type-Options nosniff;

        # Direct root proxy straight to Django
        location / {
            proxy_pass http://django:8001;
            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 https;
        }

        # Static assets served directly by Nginx
        location /staticfiles/ {
            alias /app/staticfiles/;
            expires 30d;
            access_log off;
        }

        location /media/ {
            alias /app/media/;
            expires 7d;
            access_log off;
        }
    }
}