"use client";

import { useEffect, useState } from "react";

const PHONE_NUMBERS = [
  "+91 87626 80082",
  "+91 91411 81219",
  "+91 94485 78152",
];

export default function UnderMaintenanceDialog() {
  const [open, setOpen] = useState(false);

  useEffect(() => {
    function handleClick(e: MouseEvent) {
      const target = e.target as HTMLElement;

      // ── Never intercept clicks INSIDE the dialog itself ──────────────────
      // Walk up and bail out if we're inside our own dialog card
      let el: HTMLElement | null = target;
      while (el) {
        if (el.dataset?.maintenanceDialog === "true") return;
        el = el.parentElement;
      }

      // ── Block everything else and show the dialog ─────────────────────────
      e.preventDefault();
      e.stopPropagation();
      setOpen(true);
    }

    // Capture phase — fires before React/Next.js router.push handlers
    document.addEventListener("click", handleClick, true);
    return () => document.removeEventListener("click", handleClick, true);
  }, []);

  if (!open) return null;

  return (
    <>
      {/* Backdrop */}
      <div
        className="fixed inset-0 z-[9998] bg-black/40 backdrop-blur-sm"
        aria-hidden="true"
        onClick={() => setOpen(false)}
      />

      {/* Dialog card — data-maintenance-dialog marks it as "safe zone" */}
      <div
        role="dialog"
        aria-modal="true"
        aria-labelledby="maintenance-title"
        className="fixed inset-0 z-[9999] flex items-center justify-center p-4 pointer-events-none"
      >
        <div
          data-maintenance-dialog="true"
          className="bg-white rounded-2xl shadow-2xl w-full max-w-sm p-8 flex flex-col items-center gap-5 pointer-events-auto"
        >
          {/* Icon */}
          <div className="w-20 h-20 rounded-full bg-orange-100 flex items-center justify-center">
            <svg
              xmlns="http://www.w3.org/2000/svg"
              viewBox="0 0 64 64"
              fill="none"
              stroke="currentColor"
              strokeWidth={3}
              strokeLinecap="round"
              strokeLinejoin="round"
              className="w-10 h-10 text-orange-500"
              aria-hidden="true"
            >
              <rect x="8"  y="20" width="48" height="10" rx="2" />
              <rect x="8"  y="34" width="48" height="10" rx="2" />
              <line x1="16" y1="20" x2="16" y2="44" />
              <line x1="48" y1="20" x2="48" y2="44" />
              <line x1="8"  y1="44" x2="20" y2="54" />
              <line x1="56" y1="44" x2="44" y2="54" />
              <line x1="18" y1="20" x2="24" y2="30" />
              <line x1="28" y1="20" x2="34" y2="30" />
              <line x1="38" y1="20" x2="44" y2="30" />
              <line x1="18" y1="34" x2="24" y2="44" />
              <line x1="28" y1="34" x2="34" y2="44" />
              <line x1="38" y1="34" x2="44" y2="44" />
            </svg>
          </div>

          {/* Heading */}
          <div className="text-center space-y-1">
            <h2
              id="maintenance-title"
              className="text-xl font-bold text-gray-900"
            >
              Website Under Maintenance
            </h2>
            <p className="text-sm text-gray-500">
              We'll be back soon. For bookings, please call us:
            </p>
          </div>

          {/* Phone buttons — these must work, so they live inside the safe zone */}
          <div className="w-full flex flex-col gap-3">
            {PHONE_NUMBERS.map((phone) => (
              <a
                key={phone}
                href={`tel:${phone.replace(/\s/g, "")}`}
                className="flex items-center justify-center gap-2 w-full py-3 px-4 rounded-full bg-orange-500 hover:bg-orange-600 active:scale-95 transition-all text-white font-semibold text-sm"
              >
                <svg
                  xmlns="http://www.w3.org/2000/svg"
                  className="w-4 h-4 flex-shrink-0"
                  fill="none"
                  viewBox="0 0 24 24"
                  stroke="currentColor"
                  strokeWidth={2}
                  aria-hidden="true"
                >
                  <path
                    strokeLinecap="round"
                    strokeLinejoin="round"
                    d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 8V5z"
                  />
                </svg>
                {phone}
              </a>
            ))}
          </div>

          {/* Dismiss */}
          <button
            onClick={() => setOpen(false)}
            className="text-xs text-gray-400 underline underline-offset-2 hover:text-gray-600 transition-colors mt-1"
          >
            Dismiss
          </button>
        </div>
      </div>
    </>
  );
}
