import React, { memo } from "react";

const ICONS = {
  search: (
    <svg className="w-16 h-16 text-gray-300" fill="none" viewBox="0 0 24 24" stroke="currentColor">
      <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
    </svg>
  ),
  heart: (
    <svg className="w-16 h-16 text-gray-300" fill="none" viewBox="0 0 24 24" stroke="currentColor">
      <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z" />
    </svg>
  ),
  history: (
    <svg className="w-16 h-16 text-gray-300" fill="none" viewBox="0 0 24 24" stroke="currentColor">
      <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z" />
    </svg>
  ),
  settings: (
    <svg className="w-16 h-16 text-gray-300" fill="none" viewBox="0 0 24 24" stroke="currentColor">
      <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
      <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
    </svg>
  ),
  image: (
    <svg className="w-16 h-16 text-gray-300" fill="none" viewBox="0 0 24 24" stroke="currentColor">
      <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" />
    </svg>
  ),
};

const VARIANTS = {
  noResults: {
    icon: "search",
    title: "No images found",
    description: (query) =>
      query ? `No images found for "${query}". Try different keywords or clear filters.` : "Try different keywords or clear filters.",
    actionLabel: "Clear filters",
  },
  noFavorites: {
    icon: "heart",
    title: "No favorites yet",
    description: () => "Images you favorite will appear here.",
    actionLabel: null,
  },
  noHistory: {
    icon: "history",
    title: "No search history",
    description: () => "Your recent searches will appear here.",
    actionLabel: null,
  },
  providerDisabled: {
    icon: "settings",
    title: "Provider disabled",
    description: () => "Enable this source in Dashboard → Image sources.",
    actionLabel: "Open Dashboard",
    actionHref: () => (typeof AI_IMAGE_AdminConfig !== "undefined" ? AI_IMAGE_AdminConfig.settings_url : "#"),
  },
};

function EmptyState({
  variant = "noResults",
  query = "",
  onAction,
  className = "",
}) {
  const config = VARIANTS[variant] || VARIANTS.noResults;
  const iconEl = ICONS[config.icon] || ICONS.search;
  const description =
    typeof config.description === "function"
      ? config.description(query)
      : config.description;

  return (
    <div
      className={`flex flex-col items-center justify-center rounded-xl border border-gray-200 bg-gray-50/50 py-12 px-6 text-center ${className}`}
      role="status"
      aria-live="polite"
    >
      <div className="mb-4">{iconEl}</div>
      <h3 className="text-lg font-semibold text-gray-800">{config.title}</h3>
      <p className="mt-1 max-w-sm text-sm text-gray-500">{description}</p>
      {config.actionLabel && (
        <div className="mt-4">
          {config.actionHref ? (
            <a
              href={config.actionHref()}
              className="inline-flex items-center rounded-md bg-gray-800 px-4 py-2 text-sm font-medium text-white hover:bg-gray-900"
            >
              {config.actionLabel}
            </a>
          ) : (
            <button
              type="button"
              onClick={onAction}
              className="inline-flex items-center rounded-md bg-gray-800 px-4 py-2 text-sm font-medium text-white hover:bg-gray-900"
            >
              {config.actionLabel}
            </button>
          )}
        </div>
      )}
    </div>
  );
}

export default memo(EmptyState);
