events {
    worker_connections 1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    # 로그 설정
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    error_log   /var/log/nginx/error.log warn;

    sendfile        on;
    keepalive_timeout  65;

    # 업로드 파일 크기 제한 (10GB)
    client_max_body_size 10G;

    # Upstream 설정
    upstream api_backend {
        server kamco-train-api:8080;
    }

    upstream web_backend {
        server kamco-train-web:3002;
    }

    # HTTP → HTTPS 리다이렉트 서버
    server {
        listen 80;
        server_name api.train-kamco.com train-kamco.com;

        # 모든 HTTP 요청을 HTTPS로 리다이렉트
        return 301 https://$server_name$request_uri;
    }

    # HTTPS 서버 설정
    server {
        listen 443 ssl http2;
        server_name api.train-kamco.com;

        # SSL 인증서 설정 (사설 인증서 - 멀티 도메인)
        ssl_certificate     /etc/nginx/ssl/train-kamco.com.crt;
        ssl_certificate_key /etc/nginx/ssl/train-kamco.com.key;

        # SSL 프로토콜 및 암호화 설정
        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;

        # SSL 세션 캐시
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;

        # HSTS (HTTP Strict Transport Security)
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" 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;

        # 프록시 설정
        location / {
            proxy_pass http://api_backend;
            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;
            proxy_set_header X-Forwarded-Host $server_name;

            # 인증 헤더 및 쿠키 전달 (JWT 토큰 전달 보장)
            proxy_pass_request_headers on;
            proxy_set_header Cookie $http_cookie;
            proxy_set_header Authorization $http_authorization;

            # 타임아웃 설정 (대용량 파일 업로드 지원)
            proxy_connect_timeout 300s;
            proxy_send_timeout 300s;
            proxy_read_timeout 300s;

            # 버퍼 설정
            proxy_buffering on;
            proxy_buffer_size 4k;
            proxy_buffers 8 4k;
            proxy_busy_buffers_size 8k;
        }

        # 헬스체크 엔드포인트
        location /monitor/health {
            proxy_pass http://api_backend/monitor/health;
            access_log off;
        }
    }

    # HTTPS 서버 설정 - Next.js Web Application
    server {
        listen 443 ssl http2;
        server_name train-kamco.com;

        # SSL 인증서 설정 (사설 인증서 - 멀티 도메인)
        ssl_certificate     /etc/nginx/ssl/train-kamco.com.crt;
        ssl_certificate_key /etc/nginx/ssl/train-kamco.com.key;

        # SSL 프로토콜 및 암호화 설정
        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;

        # SSL 세션 캐시
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;

        # HSTS (HTTP Strict Transport Security)
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" 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;

        # API 프록시 설정 (Web에서 API 호출 시)
        location /api/ {
            proxy_pass http://api_backend/api/;
            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;
            proxy_set_header X-Forwarded-Host $server_name;

            # 인증 헤더 및 쿠키 전달
            proxy_pass_request_headers on;
            proxy_set_header Cookie $http_cookie;

            # 타임아웃 설정
            proxy_connect_timeout 300s;
            proxy_send_timeout 300s;
            proxy_read_timeout 300s;
        }

        # 프록시 설정
        location / {
            proxy_pass http://web_backend;
            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;
            proxy_set_header X-Forwarded-Host $server_name;

            # Next.js WebSocket 지원을 위한 Upgrade 헤더
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";

            # 타임아웃 설정
            proxy_connect_timeout 600s;
            proxy_send_timeout 600s;
            proxy_read_timeout 600s;

            # 버퍼 설정
            proxy_buffering on;
            proxy_buffer_size 4k;
            proxy_buffers 8 4k;
            proxy_busy_buffers_size 8k;
        }
    }
}
