"use client"

import dynamic from "next/dynamic"
import type { ComponentType } from "react"
import {
  resolveAdminSettingsSection,
  type AdminSettingsSectionId,
} from "@/lib/admin-settings/settings-nav"
import { AdminAccessGuard } from "@/components/admin/settings/admin-access-guard"

function loadSection<P = object>(
  loader: () => Promise<{ default: ComponentType<P> }>
) {
  return dynamic(loader, { ssr: false })
}

const SECTIONS: Record<AdminSettingsSectionId, ComponentType> = {
  overview: loadSection(() =>
    import("@/components/admin/settings/sections/overview-section").then((m) => ({
      default: m.OverviewSection,
    }))
  ),
  users: loadSection(() =>
    import("@/components/admin/settings/sections/users-section").then((m) => ({
      default: m.UsersSection,
    }))
  ),
  authentication: loadSection(() =>
    import("@/components/admin/settings/sections/authentication-section").then((m) => ({
      default: m.AuthenticationSection,
    }))
  ),
  security: loadSection(() =>
    import("@/components/admin/settings/sections/security-section").then((m) => ({
      default: m.SecuritySection,
    }))
  ),
  quotas: loadSection(() =>
    import("@/components/admin/settings/sections/quotas-section").then((m) => ({
      default: m.QuotasSection,
    }))
  ),
  "file-policies": loadSection(() =>
    import("@/components/admin/settings/sections/file-policies-section").then((m) => ({
      default: m.FilePoliciesSection,
    }))
  ),
  "public-shares": loadSection(() =>
    import("@/components/admin/settings/sections/public-shares-section").then((m) => ({
      default: m.PublicSharesSection,
    }))
  ),
  llm: loadSection(() =>
    import("@/components/admin/settings/sections/ai-assistant-section").then((m) => ({
      default: m.AiAssistantSection,
    }))
  ),
  search: loadSection(() =>
    import("@/components/admin/settings/sections/search-section").then((m) => ({
      default: m.SearchSection,
    }))
  ),
  plugins: loadSection(() =>
    import("@/components/admin/settings/sections/plugins-section").then((m) => ({
      default: m.PluginsSection,
    }))
  ),
  agenda: loadSection(() =>
    import("@/components/admin/settings/sections/agenda-section").then((m) => ({
      default: m.AgendaSection,
    }))
  ),
  ultimeet: loadSection(() =>
    import("@/components/admin/settings/sections/ultimeet-section").then((m) => ({
      default: m.UltimeetSection,
    }))
  ),
  "mail-domains": loadSection(() =>
    import("@/components/admin/settings/sections/mail-domains-section").then((m) => ({
      default: m.MailDomainsSection,
    }))
  ),
  "ai-assistant": loadSection(() =>
    import("@/components/admin/settings/sections/ai-assistant-section").then((m) => ({
      default: m.AiAssistantSection,
    }))
  ),
  audit: loadSection(() =>
    import("@/components/admin/settings/sections/audit-section").then((m) => ({
      default: m.AuditSection,
    }))
  ),
}

export function AdminSettingsSectionView({
  sectionId,
}: {
  sectionId: AdminSettingsSectionId
}) {
  const Section = SECTIONS[sectionId]
  return (
    <AdminAccessGuard>
      <Section />
    </AdminAccessGuard>
  )
}

export function AdminSettingsSectionFromSegments({
  segments,
}: {
  segments?: string[]
}) {
  const sectionId = resolveAdminSettingsSection(segments)
  return <AdminSettingsSectionView sectionId={sectionId} />
}
