import { useState } from 'react';
import { MessageCircle, X, Phone } from 'lucide-react';
import { WHATSAPP_CONFIG } from '../config/site';

const WhatsAppWidget = () => {
  const [isOpen, setIsOpen] = useState(false);

  // Se o widget estiver desabilitado na config, nao renderiza
  if (!WHATSAPP_CONFIG.enabled) return null;

  const phoneNumber = WHATSAPP_CONFIG.phoneNumber;
  const message = WHATSAPP_CONFIG.defaultMessage;
  const whatsappUrl = `https://wa.me/${phoneNumber}?text=${encodeURIComponent(message)}`;

  return (
    <div className="fixed bottom-6 right-6 z-50">
      {/* Chat window */}
      {isOpen && (
        <div className="mb-4 bg-[#1a1a1a] rounded-2xl shadow-2xl border border-white/10 overflow-hidden w-80 animate-in slide-in-from-bottom-4 fade-in duration-300">
          {/* Header */}
          <div className="bg-gradient-to-r from-[#25D366] to-[#128C7E] p-4">
            <div className="flex items-center gap-3">
              <div className="w-10 h-10 rounded-full bg-white/20 flex items-center justify-center">
                <Phone className="w-5 h-5 text-white" />
              </div>
              <div>
                <p className="text-white font-semibold">Avanzato Tecnologia</p>
                <p className="text-white/80 text-xs">Resposta em ate 24h</p>
              </div>
              <button
                onClick={() => setIsOpen(false)}
                className="ml-auto text-white/80 hover:text-white transition-colors"
              >
                <X className="w-5 h-5" />
              </button>
            </div>
          </div>

          {/* Body */}
          <div className="p-4 bg-[#0e0e0e]">
            <div className="bg-[#1a1a1a] rounded-xl p-4 mb-4 border border-white/5">
              <p className="text-gray-300 text-sm">
                {WHATSAPP_CONFIG.greetingText}
              </p>
              <p className="text-gray-500 text-xs mt-2">
                Avanzato Tecnologia
              </p>
            </div>

            {/* Quick options - importadas de config/site.ts */}
            <div className="space-y-2 mb-4">
              {WHATSAPP_CONFIG.options.map((option, i) => (
                <a
                  key={i}
                  href={`https://wa.me/${phoneNumber}?text=${encodeURIComponent(option.message)}`}
                  target="_blank"
                  rel="noopener noreferrer"
                  className="block w-full text-left p-3 bg-[#1a1a1a] rounded-xl border border-white/5 hover:border-[#25D366]/50 transition-colors text-gray-300 text-sm hover:text-white"
                >
                  {option.label}
                </a>
              ))}
            </div>

            {/* CTA Button */}
            <a
              href={whatsappUrl}
              target="_blank"
              rel="noopener noreferrer"
              className="flex items-center justify-center gap-2 w-full bg-[#25D366] hover:bg-[#128C7E] text-white font-semibold py-3 rounded-xl transition-colors"
            >
              <MessageCircle className="w-5 h-5" />
              Iniciar conversa no WhatsApp
            </a>
          </div>
        </div>
      )}

      {/* Toggle button */}
      <button
        onClick={() => setIsOpen(!isOpen)}
        className={`w-14 h-14 rounded-full shadow-lg flex items-center justify-center transition-all duration-300 hover:scale-110 ${
          isOpen
            ? 'bg-gray-600 hover:bg-gray-700'
            : 'bg-[#25D366] hover:bg-[#128C7E]'
        }`}
        aria-label="WhatsApp"
      >
        {isOpen ? (
          <X className="w-6 h-6 text-white" />
        ) : (
          <MessageCircle className="w-6 h-6 text-white" />
        )}
      </button>
    </div>
  );
};

export default WhatsAppWidget;
