# ──────────────────────────────────────────────────────────────────────────────
# crm.beezifi.com  — frontend SPA (static files + tenant path routing)
# ──────────────────────────────────────────────────────────────────────────────
# Deploy:
#   sudo cp nginx-crm.conf /etc/nginx/sites-available/crm.beezifi.com
#   sudo ln -s /etc/nginx/sites-available/crm.beezifi.com /etc/nginx/sites-enabled/
#   sudo nginx -t && sudo systemctl reload nginx
# ──────────────────────────────────────────────────────────────────────────────

server {
    listen 80;
    server_name crm.beezifi.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name crm.beezifi.com;

    # ── SSL (Let's Encrypt / Certbot fills these in automatically) ──────────
    ssl_certificate     /etc/letsencrypt/live/crm.beezifi.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/crm.beezifi.com/privkey.pem;
    include             /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam         /etc/letsencrypt/ssl-dhparams.pem;

    # ── Frontend root ───────────────────────────────────────────────────────
    # Adjust this path to wherever your frontend/ directory is on the server
    root /var/www/html/PM2ManagerProduction/apps/BeezifiCRM/frontend;
    index index.html;

    # ── Cache static assets ─────────────────────────────────────────────────
    location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
        expires 7d;
        add_header Cache-Control "public, immutable";
        try_files $uri =404;
    }

    # ── SPA fallback ────────────────────────────────────────────────────────
    # All unknown paths (e.g. /phamholdings, /acme, etc.) get index.html.
    # index.html reads window.location.pathname to extract the tenant slug.
    location / {
        try_files $uri $uri/ /index.html;
    }
}
