import type { LucideIcon } from "lucide-react";
import {
  LayoutDashboard,
  Globe,
  FileText,
  Layers,
  Paintbrush,
  FormInput,
  Send,
  BarChart3,
  Settings,
  Building2,
  Puzzle,
  Palette,
} from "lucide-react";

export type ExperiencePortalId = "studio";

export type PortalNavItem = {
  href: string;
  label: string;
  feature?: string | null;
};

export type PortalNavGroup = {
  id: string;
  label: string;
  icon: LucideIcon;
  href?: string;
  feature?: string | null;
  items?: PortalNavItem[];
};

export const EXPERIENCE_PORTAL = {
  id: "studio" as const,
  label: "تربت پیجز",
  description: "سایت، صفحه، قالب، فرم و انتشار",
  basePath: "/experience",
  icon: Globe,
};

export const STUDIO_NAV: PortalNavGroup[] = [
  {
    id: "dashboard",
    label: "داشبورد",
    icon: LayoutDashboard,
    href: "/experience",
  },
  {
    id: "workspaces",
    label: "فضاهای کاری / سایت‌ها",
    icon: Building2,
    items: [
      { href: "/experience/workspaces", label: "فضاهای کاری" },
      { href: "/experience/sites", label: "سایت‌ها" },
      { href: "/experience/site-domains", label: "دامنه‌ها" },
    ],
  },
  {
    id: "pages",
    label: "صفحات / قالب‌ها",
    icon: FileText,
    items: [
      { href: "/experience/pages", label: "صفحات" },
      { href: "/experience/templates", label: "قالب‌ها" },
      { href: "/experience/template-versions", label: "نسخه‌های قالب" },
      { href: "/experience/template-instantiations", label: "نمونه‌سازی" },
      { href: "/experience/publish-targets", label: "اهداف انتشار" },
    ],
  },
  {
    id: "builder",
    label: "سازنده",
    icon: Paintbrush,
    items: [
      { href: "/experience/components", label: "کامپوننت‌ها" },
      { href: "/experience/component-versions", label: "نسخه کامپوننت" },
      { href: "/experience/themes", label: "تم‌ها" },
      { href: "/experience/theme-versions", label: "نسخه تم" },
      { href: "/experience/layouts", label: "چیدمان‌ها" },
      { href: "/experience/layout-versions", label: "نسخه چیدمان" },
      { href: "/experience/site-theme-bindings", label: "اتصال تم" },
      { href: "/experience/page-layout-bindings", label: "اتصال چیدمان" },
      { href: "/experience/localizations", label: "بومی‌سازی" },
      { href: "/experience/media-refs", label: "رسانه‌ها" },
    ],
  },
  {
    id: "forms",
    label: "فرم‌ها / نظرسنجی",
    icon: FormInput,
    items: [
      { href: "/experience/forms", label: "فرم‌ها" },
      { href: "/experience/form-submissions", label: "ارسال‌ها" },
      { href: "/experience/surveys", label: "نظرسنجی‌ها" },
      { href: "/experience/survey-responses", label: "پاسخ‌ها" },
    ],
  },
  {
    id: "publishing",
    label: "انتشار / SEO",
    icon: Send,
    items: [
      { href: "/experience/publish-releases", label: "انتشارها" },
      { href: "/experience/domain-edge-bindings", label: "Edge دامنه" },
      { href: "/experience/seo-metadata", label: "SEO" },
      { href: "/experience/sitemap-shells", label: "Sitemap" },
      { href: "/experience/robots-shells", label: "Robots" },
      { href: "/experience/pwa-manifests", label: "PWA" },
      { href: "/experience/published-actions", label: "اکشن‌های منتشر شده" },
    ],
  },
  {
    id: "analytics",
    label: "آنالیتیکس / Embed",
    icon: BarChart3,
    items: [
      { href: "/experience/analytics", label: "داشبورد آنالیتیکس" },
      { href: "/experience/analytics-report-definitions", label: "تعریف گزارش" },
      { href: "/experience/analytics-snapshots", label: "اسنپ‌شات‌ها" },
      { href: "/experience/embed", label: "پیش‌نمایش Embed" },
      { href: "/experience/widget-definitions", label: "ویجت‌ها" },
      { href: "/experience/widget-instances", label: "نمونه ویجت" },
    ],
  },
  {
    id: "design-system",
    label: "سیستم طراحی",
    icon: Palette,
    href: "/experience/design-system",
  },
  {
    id: "admin",
    label: "مدیریت",
    icon: Settings,
    items: [
      { href: "/experience/locale-profiles", label: "پروفایل زبان" },
      { href: "/experience/external-providers", label: "ارائه‌دهندگان" },
      { href: "/experience/render-engines", label: "موتور رندر" },
      { href: "/experience/configurations", label: "پیکربندی" },
      { href: "/experience/settings", label: "تنظیمات" },
      { href: "/experience/audit", label: "ممیزی" },
      { href: "/experience/health", label: "سلامت سرویس" },
      { href: "/experience/capabilities", label: "قابلیت‌ها" },
    ],
  },
];

export function navForExperience(): PortalNavGroup[] {
  return STUDIO_NAV;
}

export function filterNavByCapabilities(
  nav: PortalNavGroup[],
  features?: Record<string, boolean>
): PortalNavGroup[] {
  if (!features) return nav;

  const isEnabled = (feature?: string | null) => {
    if (!feature) return true;
    return features[feature] === true;
  };

  return nav
    .map((group) => {
      if (group.feature && !isEnabled(group.feature)) return null;
      if (group.items) {
        const items = group.items.filter((i) => isEnabled(i.feature));
        if (items.length === 0 && !group.href) return null;
        return { ...group, items };
      }
      return group;
    })
    .filter(Boolean) as PortalNavGroup[];
}
