server {
    listen 80;
    server_name cdn.mozartdigital.docker;

    # Redirection HTTP vers HTTPS
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2;
    server_name cdn.mozartdigital.docker;

    # Configuration SSL
    ssl_certificate /usr/local/share/ca-certificates/cdn.mozartdigital.docker.crt;
    ssl_certificate_key /usr/local/share/ca-certificates/cdn.mozartdigital.docker.key;

    # Paramètres SSL sécurisés
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    root /var/www/html/apps/apis/cdn/public;
    index index.php;

    # Configuration pour les assets - toujours passer par Laravel
    location /assets/ {
        # Passer directement par Laravel sans essayer de servir les fichiers statiques
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php8.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_read_timeout 30s;

        # Laisser Laravel gérer les headers de cache

        # Compression gzip
        gzip on;
        gzip_vary on;
        gzip_min_length 1024;
        gzip_types
            image/svg+xml
            image/x-icon
            image/vnd.microsoft.icon
            image/webp
            image/jpeg
            image/png
            text/css
            text/javascript
            application/javascript
            application/x-javascript
            application/json;
    }

    location /storage/ {
        # Passer directement par Laravel sans essayer de servir les fichiers statiques
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php8.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_read_timeout 30s;

        # Laisser Laravel gérer les headers de cache

        # Compression gzip
        gzip on;
        gzip_vary on;
        gzip_min_length 1024;
        gzip_types
            image/svg+xml
            image/x-icon
            image/vnd.microsoft.icon
            image/webp
            image/jpeg
            image/png
            text/css
            text/javascript
            application/javascript
            application/x-javascript
            application/json;
    }

    # Redirection vers PHP pour le traitement des images
    location @php {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php8.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_read_timeout 30s;
    }

    # Configuration générale pour Laravel - rediriger tout vers Laravel
    location / {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php8.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_read_timeout 30s;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php8.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_read_timeout 30s;
    }

    # Sécurité - masquer les fichiers sensibles
    location ~ /\. {
        deny all;
    }

    # Bloquer l'accès direct aux dossiers storage et bootstrap/cache du CDN
    # Mais permettre les chemins qui contiennent ces mots dans un sous-chemin (ex: domaine.com/storage/...)
    location ~ ^/(storage|bootstrap/cache)(/|$) {
        deny all;
    }

    # Headers de sécurité
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
}
