from modules.ssh_manager import execute_command

def setup_nginx_reverse_proxy(server_data, domain, internal_port):
    """
    Creates and enables Nginx reverse proxy configuration for an application domain.
    """
    class DummyServer:
        pass
    srv = DummyServer()
    srv.ip = server_data['ip']
    srv.ssh_port = server_data.get('ssh_port', 22)
    srv.ssh_user = server_data.get('ssh_user', 'root')
    srv.auth_type = server_data.get('auth_type', 'key')
    srv.ssh_key_path = server_data.get('ssh_key_path')
    srv.password = server_data.get('password')

    config_content = f"""server {{
    listen 80;
    server_name {domain};

    location / {{
        proxy_pass http://127.0.0.1:{internal_port};
        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;
    }}
}}
"""

    cmd = f"""
    cat << 'EOF' > /etc/nginx/sites-available/{domain}
{config_content}
EOF
    ln -sf /etc/nginx/sites-available/{domain} /etc/nginx/sites-enabled/
    nginx -t && systemctl reload nginx
    """

    exit_code, out, err = execute_command(srv, cmd)
    return exit_code == 0
