"use client";

import { useState, useEffect } from "react";
import Image from "next/image";

const WHATSAPP_CONFIG = {
  phoneNumber: "919999999999",
  companyName: "Real Switchgears",
  replyTimeText: "Typically replies within an hour",
  avatarText: "RS",
  logoSrc: "",
  welcomeMessage: "Hello! I am interested in Lauritz Knudsen & Polycab products. Please share the catalogue.",
  chatAgentRole: "Sales Support",
  agentGreeting: "Hi there! 👋 How can we help you today with your switchgear or cable requirements?",
};

const WhatsAppWidget = () => {
  const [isOpen, setIsOpen] = useState(false);
  const [shouldAnimate, setShouldAnimate] = useState(false);

  useEffect(() => {
    const timer = setTimeout(() => {
      setShouldAnimate(true);
    }, 3000);
    return () => clearTimeout(timer);
  }, []);

  const whatsappUrl = `https://api.whatsapp.com/send?phone=${WHATSAPP_CONFIG.phoneNumber}&text=${encodeURIComponent(WHATSAPP_CONFIG.welcomeMessage)}`;

  return (
    /* 🌟 फिक्स 1: 'pointer-events-none' जोड़ा ताकि बंद होने पर यह पीछे के एलिमेंट्स के क्लिक को न रोके */
    /* 🌟 फिक्स 2: 'left-15' को बदलकर स्टैंडर्ड 'left-5' या 'right-5' (अपनी सुविधानुसार) करें, मैंने यहाँ left-5 किया है */
    <div className="fixed bottom-20 right-7 z-50 font-sans text-left flex flex-col items-end pointer-events-none">

      <div
        /* 🌟 फिक्स 3: जब चैट बॉक्स खुला हो तभी उसमें क्लिक काम करे (pointer-events-auto) */
        className={`mb-4 bg-white rounded-2xl shadow-2xl border border-gray-100 overflow-hidden transition-all duration-300 ease-in-out transform ${isOpen
          ? "opacity-100 translate-y-0 scale-100 pointer-events-auto"
          : "opacity-0 translate-y-10 scale-95 pointer-events-none"
          } 
        w-[calc(100vw-2rem)] sm:w-[360px] max-w-[360px]`}
      >
        <div className="bg-[#075e54] p-4 flex items-center justify-between gap-3">
          <div className="flex-1">
            <h4 className="text-white font-bold text-sm sm:text-base">{WHATSAPP_CONFIG.companyName}</h4>
            <p className="text-white/80 text-xs flex items-center gap-1 mt-0.5">
              <span className="inline-block h-1.5 w-1.5 rounded-full bg-[#25d366] animate-pulse" />
              {WHATSAPP_CONFIG.replyTimeText}
            </p>
          </div>

          <button
            onClick={() => setIsOpen(false)}
            className="text-white/60 hover:text-white text-2xl font-light px-2 transition-colors focus:outline-none order-2"
            aria-label="Close chat"
          >
            ×
          </button>

          <div className="relative h-10 w-10 shrink-0 rounded-full bg-white/20 flex items-center justify-center text-white font-bold border border-white/40 overflow-hidden order-3">
            {WHATSAPP_CONFIG.logoSrc ? (
              <Image
                src={WHATSAPP_CONFIG.logoSrc}
                alt={WHATSAPP_CONFIG.companyName}
                fill
                className="object-cover"
              />
            ) : (
              WHATSAPP_CONFIG.avatarText
            )}
            <span className="absolute bottom-0 right-0 h-2.5 w-2.5 rounded-full bg-[#25d366] border border-[#075e54]" />
          </div>
        </div>

        <div className="p-4 bg-[#e5ddd5] min-h-[120px] relative">
          <div className="absolute inset-0 opacity-[0.06] bg-[url('https://user-images.githubusercontent.com/15075759/28719144-86dc0f70-73b1-11e7-911d-60d70fcded21.png')] bg-repeat" />
          <div className="relative bg-white p-3 rounded-xl rounded-tl-none shadow-sm max-w-[85%] text-gray-800 text-sm leading-relaxed">
            <p className="font-medium text-[#075e54] text-[11px] mb-0.5">{WHATSAPP_CONFIG.chatAgentRole}</p>
            {WHATSAPP_CONFIG.agentGreeting}
          </div>
        </div>

        <div className="p-3 bg-gray-50 border-t border-gray-100">
          <a
            href={whatsappUrl}
            target="_blank"
            rel="noopener noreferrer"
            className="flex items-center justify-center gap-2 w-full bg-[#25d366] hover:bg-[#20ba56] text-white font-bold py-2.5 px-4 rounded-xl shadow-md hover:shadow-lg transition-all duration-300 transform hover:-translate-y-0.5 text-sm"
          >
            <svg
              className="h-8 w-8 drop-shadow-md transition-transform duration-300 group-hover:scale-105"
              viewBox="0 0 24 24"
              xmlns="http://www.w3.org/2000/svg"
            >
              <path fill="#ffffff" d="M12.004 2c-5.51 0-9.993 4.483-9.993 9.993 0 1.758.455 3.41 1.254 4.858L2 22l5.311-1.393a9.932 9.932 0 004.693 1.177c5.51 0 9.993-4.483 9.993-9.993S17.514 2 12.004 2z" />
              <path fill="#25d366" d="M12.004 3.504c-4.682 0-8.489 3.807-8.489 8.489 0 1.606.447 3.109 1.226 4.401l-.799 2.922 2.992-.785a8.441 8.441 0 004.07 1.051c4.682 0 8.49-3.808 8.49-8.49 0-4.681-3.808-8.488-8.49-8.488z" />
              <path fill="#ffffff" d="M15.93 13.52c-.215-.108-1.272-.627-1.469-.699-.196-.072-.34-.108-.483.108-.143.216-.556.699-.681.843-.125.144-.25.162-.465.054-.215-.108-.909-.335-1.731-1.068-.64-.57-1.072-1.275-1.198-1.491-.125-.215-.013-.332.094-.439.097-.097.215-.251.323-.377.108-.126.144-.215.215-.359.072-.144.036-.269-.018-.377-.054-.108-.483-1.164-.662-1.595-.175-.421-.349-.364-.482-.371-.125-.006-.269-.007-.413-.007-.143 0-.377.054-.574.269-.197.215-.753.736-.753 1.795s.771 2.081.879 2.224c.108.144 1.516 2.314 3.673 3.246.513.222.914.355 1.226.454.516.164.985.141 1.356.086.414-.061 1.272-.52 1.451-1.022.18-.502.18-.932.126-1.022-.054-.09-.197-.144-.413-.252z" />
            </svg>
            Start Chat
          </a>
        </div>
      </div>

      {/* 🌟 फिक्स 4: मुख्य गोल बटन पर हमेशा क्लिक काम करना चाहिए (pointer-events-auto) */}
      <button
        onClick={() => setIsOpen(!isOpen)}
        className={`flex h-14 w-14 items-center justify-center rounded-full bg-[#005285] text-white shadow-xl hover:bg-[#005285] transition-all duration-300 transform hover:scale-110 active:scale-95 focus:outline-none pointer-events-auto ${shouldAnimate && !isOpen ? "animate-bounce" : ""
          }`}
        aria-label="Contact us on WhatsApp"
      >
        {isOpen ? (
          <span className="text-2xl font-light text-white">✕</span>
        ) : (
          <svg
            className="h-8 w-8 drop-shadow-md transition-transform duration-300 group-hover:scale-105"
            viewBox="0 0 24 24"
            xmlns="http://www.w3.org/2000/svg"
          >
            <path fill="#ffffff" d="M12.004 2c-5.51 0-9.993 4.483-9.993 9.993 0 1.758.455 3.41 1.254 4.858L2 22l5.311-1.393a9.932 9.932 0 004.693 1.177c5.51 0 9.993-4.483 9.993-9.993S17.514 2 12.004 2z" />
            <path fill="#006db1" d="M12.004 3.504c-4.682 0-8.489 3.807-8.489 8.489 0 1.606.447 3.109 1.226 4.401l-.799 2.922 2.992-.785a8.441 8.441 0 004.07 1.051c4.682 0 8.49-3.808 8.49-8.49 0-4.681-3.808-8.488-8.49-8.488z" />
            <path fill="#ffffff" d="M15.93 13.52c-.215-.108-1.272-.627-1.469-.699-.196-.072-.34-.108-.483.108-.143.216-.556.699-.681.843-.125.144-.25.162-.465.054-.215-.108-.909-.335-1.731-1.068-.64-.57-1.072-1.275-1.198-1.491-.125-.215-.013-.332.094-.439.097-.097.215-.251.323-.377.108-.126.144-.215.215-.359.072-.144.036-.269-.018-.377-.054-.108-.483-1.164-.662-1.595-.175-.421-.349-.364-.482-.371-.125-.006-.269-.007-.413-.007-.143 0-.377.054-.574.269-.197.215-.753.736-.753 1.795s.771 2.081.879 2.224c.108.144 1.516 2.314 3.673 3.246.513.222.914.355 1.226.454.516.164.985.141 1.356.086.414-.061 1.272-.52 1.451-1.022.18-.502.18-.932.126-1.022-.054-.09-.197-.144-.413-.252z" />
          </svg>
        )}
      </button>

    </div>
  );
};

export default WhatsAppWidget;