server {
    listen 80;
    server_name dev.geosector.fr;

    root /var/www/geosector/web;
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }

    # Configuration pour les assets statiques (optionnel)
    location /assets/ {
        expires 1y;
        add_header Cache-Control "public";
    }
}

server {
    listen 80;
    server_name dapp.geosector.fr;
    
    # Logs globales
    access_log /var/log/nginx/geosector-app_access.log;
    error_log /var/log/nginx/geosector-app_error.log;
    
    set $current_host $host;
    
    # Application Flutter (contenu statique)
    location / {
        root /var/www/geosector/app;
        index index.html;
        try_files $uri $uri/ /index.html;
        
        # Configuration pour les assets Flutter
        location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
            expires off;
            add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate" always;
            add_header Pragma "no-cache" always;
        }
    }
    
    # API PHP
    location /api/ {
        # alias /var/www/geosector/api/public/;
        
        add_header X-Debug-Host $current_host;
        
        # Gestion CORS pour les requêtes OPTIONS
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' $http_origin always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
            add_header 'Access-Control-Allow-Credentials' 'true' always;
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain; charset=utf-8';
            add_header 'Content-Length' 0;
            return 204;
        }
        
        # En-têtes CORS pour les requêtes normales
        add_header 'Access-Control-Allow-Origin' $http_origin always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization' always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        
        # Désactiver le cache pour le développement
        add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate" always;
        add_header Pragma "no-cache" always;
        add_header Expires "0" always;
        
        # Configuration conditionnelle pour l'origine
        if ($http_origin ~ '^http://(dapp\.geosector\.fr)$') {
            set $cors "true";
        }
        
        if ($cors = "false") {
            return 403;
        }
        
        # Traitement normal pour les autres méthodes
        try_files $uri $uri/ /api/index.php$is_args$args;
        
        # Gestion PHP pour l'API (ajusté pour fonctionner avec alias)
        location ~ ^/api/(.+\.php)$ {
            # alias /var/www/geosector/api/public/$1;
	        fastcgi_pass unix:/run/php-fpm83/php-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            
            # Variable d'environnement
            fastcgi_param APP_ENV "dev";  # À ajuster selon l'environnement
           
            # Transmission des headers personnalisés à PHP
            fastcgi_param HTTP_X_APP_IDENTIFIER "dapp.geosector.fr";
            fastcgi_param HTTP_X_REAL_IP $remote_addr;
            
            # Augmenter les timeouts pour les opérations de synchronisation
            fastcgi_read_timeout 300;
            fastcgi_send_timeout 300;
            
            # En-têtes CORS pour les réponses PHP
            add_header 'Access-Control-Allow-Origin' $http_origin always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS' always;
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization' always;
            add_header 'Access-Control-Allow-Credentials' 'true' always;
        }
    }
    
    # Protection des fichiers système
    location ~ /\.(?!well-known) {
        deny all;
    }
}