"use client";

import { useHospitalityCapabilities } from "@/modules/hospitality/hooks/useHospitalityCapabilities";
import { EmptyState } from "@/components/ds";
import { HospitalityPageLoader } from "@/modules/hospitality/pages/shared";

const FEATURE_TO_BUNDLE: Record<string, string> = {
  digital_menu: "digital_menu",
  digital_menu_catalog: "digital_menu",
  qr_menu: "qr_menu",
  qr_ordering: "qr_ordering",
  pos_lite: "pos_lite",
  pos_pro: "pos_pro",
  kitchen: "kitchen",
  kitchen_engine: "kitchen",
  reservation: "reservation",
  table_service: "table_service",
  delivery_integration: "delivery_connector",
  delivery_connector: "delivery_connector",
  accounting_integration: "accounting_connector",
  crm_integration: "crm_connector",
  crm_connector: "crm_connector",
  loyalty_integration: "loyalty_connector",
  loyalty_connector: "loyalty_connector",
  communication_integration: "communication_connector",
  communication_connector: "communication_connector",
  website_integration: "website_connector",
  website_connector: "website_connector",
  analytics: "analytics",
};

export function HospitalityBundleGate({
  feature,
  capabilities,
  children,
}: {
  feature: string;
  capabilities?: { features: Record<string, boolean> };
  children: React.ReactNode;
}) {
  const capsQ = useHospitalityCapabilities();
  const caps = capabilities ?? capsQ.data;
  if (capsQ.isLoading && !capabilities) return <HospitalityPageLoader />;

  const enabled = caps?.features?.[feature] ?? caps?.features?.[FEATURE_TO_BUNDLE[feature] ?? feature];
  if (enabled === false) {
    return (
      <EmptyState
        title="ماژول غیرفعال"
        description="این بخش در بسته فعال شما موجود نیست. از تنظیمات بسته‌ها فعال کنید."
      />
    );
  }
  return <>{children}</>;
}
