"use client";

import { useState } from "react";
import { usePathname } from "next/navigation";
import { MAINTENANCE_ENABLED } from "@/lib/maintenance";

const PHONE_NUMBERS = [
  { number: "+91 87626 80082", href: "tel:+918762680082" },
  { number: "+91 91411 81219", href: "tel:+919141181219" },
  { number: "+91 94485 78152", href: "tel:+919448578152" },
];

export default function MaintenanceDialog() {
  const pathname = usePathname();
  // Dismiss only applies to the current page: navigating to a different
  // pathname re-shows the dialog (temporary under-maintenance behavior).
  const [dismissedPath, setDismissedPath] = useState<string | null>(null);

  const open = dismissedPath !== pathname;

  const handleDismiss = () => setDismissedPath(pathname);

  if (!MAINTENANCE_ENABLED || !open) return null;

  return (
    // Backdrop
    <div
      className="fixed inset-0 z-[9999] flex items-center justify-center bg-black/50 px-4"
      onClick={handleDismiss}
    >
      {/* Dialog card */}
      <div
        className="bg-white rounded-2xl shadow-2xl w-full max-w-sm p-6 flex flex-col items-center text-center"
        onClick={(e) => e.stopPropagation()}
      >
        {/* Icon */}
        <div className="w-16 h-16 rounded-full bg-orange-100 flex items-center justify-center mb-4">
          <svg
            xmlns="http://www.w3.org/2000/svg"
            className="w-8 h-8 text-orange-500"
            fill="none"
            viewBox="0 0 24 24"
            stroke="currentColor"
            strokeWidth={2}
          >
            <path
              strokeLinecap="round"
              strokeLinejoin="round"
              d="M12 9v2m0 4h.01M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z"
            />
          </svg>
        </div>

        {/* Title */}
        <h2 className="text-xl font-bold text-gray-900 mb-2">
          Website Under Maintenance
        </h2>

        {/* Subtitle */}
        <p className="text-gray-500 text-sm mb-6">
          We&apos;ll be back soon. For bookings, please call us:
        </p>

        {/* Phone buttons */}
        <div className="w-full flex flex-col gap-3 mb-6">
          {PHONE_NUMBERS.map((phone) => (
            <a
              key={phone.href}
              href={phone.href}
              className="flex items-center justify-center gap-2 w-full py-3 px-4 rounded-full bg-orange-500 hover:bg-orange-600 active:bg-orange-700 text-white font-semibold text-sm transition-colors"
            >
              {/* Phone icon */}
              <svg
                xmlns="http://www.w3.org/2000/svg"
                className="w-4 h-4"
                fill="currentColor"
                viewBox="0 0 24 24"
              >
                <path d="M6.62 10.79a15.053 15.053 0 006.59 6.59l2.2-2.2a1 1 0 011.01-.24c1.12.37 2.33.57 3.58.57a1 1 0 011 1V20a1 1 0 01-1 1C10.61 21 3 13.39 3 4a1 1 0 011-1h3.5a1 1 0 011 1c0 1.25.2 2.46.57 3.58a1 1 0 01-.24 1.01l-2.21 2.2z" />
              </svg>
              {phone.number}
            </a>
          ))}
        </div>

        {/* Dismiss */}
        <button
          onClick={handleDismiss}
          className="text-gray-400 hover:text-gray-600 text-sm underline underline-offset-2 transition-colors"
        >
          Dismiss
        </button>
      </div>
    </div>
  );
}
