import { useEffect } from 'react';

export default function GoBotWidget() {
  useEffect(() => {
    if (typeof window === 'undefined') return;

    const goBotId = process.env.NEXT_PUBLIC_GOBOT_ID;

    if (!goBotId) {
      console.error('NEXT_PUBLIC_GOBOT_ID no configurado');
      return;
    }

    if (!window.gobot) {
      window.gobot = function() {
        (window.gobot.queue = window.gobot.queue || []).push(arguments);
      };
      window.gobot.queue = [];
    }

    window.gobot('create', goBotId);

    const script = document.createElement('script');
    script.type = 'text/javascript';
    script.async = true;
    script.src = 'https://new.getgobot.com/client/v1/gobot-client.js';
    script.setAttribute('ngrok-skip-browser-warning', 'true');
    document.head.appendChild(script);

    return () => {
      if (script.parentNode) {
        script.parentNode.removeChild(script);
      }
    };
  }, []);

  return null;
}