"use client"

import type { ReactNode } from "react"
import {
  CheckCircle2,
  Copy,
  Download,
  FileText,
  FolderInput,
  FolderOpen,
  Globe2,
  HardDrive,
  History,
  Info,
  Mail,
  Pencil,
  Printer,
  Share2,
  Shield,
  Trash2,
  UserPlus,
} from "lucide-react"
import {
  MenubarContent,
  MenubarItem,
  MenubarMenu,
  MenubarSeparator,
  MenubarSubContent,
  MenubarSubTrigger,
  MenubarTrigger,
} from "@/components/ui/menubar"
import {
  DocsExclusiveMenuSub,
  DocsExclusiveMenuSubRoot,
} from "@/components/drive/richtext/docs-exclusive-menu-sub"
import { DOCS_MENUBAR_CONTENT_PROPS } from "@/components/drive/richtext/docs-menubar-props"
import { DocsLogoIcon } from "@/components/drive/richtext/docs-logo-icon"
import { DocsMenuShortcut } from "@/components/drive/richtext/docs-menu-shortcut"
import { DOCS_DOWNLOAD_FORMATS, type DocsDownloadFormat } from "@/lib/drive/docs-file-menu-export"
import { cn } from "@/lib/utils"

export type DocsFileMenuActions = {
  onNewDocument: () => void
  onNewFromTemplate: () => void
  onOpen: () => void
  onMakeCopy: () => void
  onShareWithUsers: () => void
  onPublishToWeb: () => void
  onEmailFile: () => void
  onEmailCollaborators: () => void
  onEmailDraft: () => void
  onDownload: (format: DocsDownloadFormat) => void
  onRename: () => void
  onMove: () => void
  onAddShortcut: () => void
  onMoveToTrash: () => void
  onNameCurrentVersion: () => void
  onShowVersionHistory: () => void
  onToggleOffline: () => void
  onDetails: () => void
  onSecurityLimits: () => void
  onPageSetup: () => void
  onPrint: () => void
  /** Dev-only: purge .ultidoc.json sidecar and force DOCX reimport. */
  onPurgeSidecarAndReimport?: () => void
}

function MenuIcon({ children }: { children: ReactNode }) {
  return <span className="docs-menu-item-icon">{children}</span>
}

export function DocsFileMenu({
  actions,
  disabled,
}: {
  actions: DocsFileMenuActions
  disabled?: boolean
}) {
  return (
    <MenubarMenu>
      <MenubarTrigger className="docs-menu-trigger">Fichier</MenubarTrigger>
      <MenubarContent
        {...DOCS_MENUBAR_CONTENT_PROPS}
        className="docs-menu-content min-w-[280px] overflow-visible"
        data-docs-menu-surface
      >
        <DocsExclusiveMenuSubRoot>
        <DocsExclusiveMenuSub id="new">
          <MenubarSubTrigger className="docs-menu-item" disabled={disabled}>
            <MenuIcon>
              <FileText className="size-4" />
            </MenuIcon>
            Nouveau
          </MenubarSubTrigger>
          <MenubarSubContent className="docs-menu-content docs-menu-sub-content min-w-[240px] overflow-visible" data-docs-menu-surface>
            <MenubarItem className="docs-menu-item" disabled={disabled} onClick={actions.onNewDocument}>
              <MenuIcon>
                <DocsLogoIcon className="size-4" />
              </MenuIcon>
              Document
            </MenubarItem>
            <MenubarItem
              className="docs-menu-item"
              disabled={disabled}
              onClick={actions.onNewFromTemplate}
            >
              <MenuIcon>
                <Pencil className="size-4" />
              </MenuIcon>
              À partir de la galerie de modèles
            </MenubarItem>
          </MenubarSubContent>
        </DocsExclusiveMenuSub>

        <MenubarItem className="docs-menu-item" disabled={disabled} onClick={actions.onOpen}>
          <MenuIcon>
            <FolderOpen className="size-4" />
          </MenuIcon>
          Ouvrir
          <DocsMenuShortcut shortcutId="file.open" />
        </MenubarItem>

        <MenubarItem className="docs-menu-item" disabled={disabled} onClick={actions.onMakeCopy}>
          <MenuIcon>
            <Copy className="size-4" />
          </MenuIcon>
          Créer une copie
        </MenubarItem>

        <MenubarSeparator />

        <DocsExclusiveMenuSub id="share">
          <MenubarSubTrigger className="docs-menu-item" disabled={disabled}>
            <MenuIcon>
              <UserPlus className="size-4" />
            </MenuIcon>
            Partager
          </MenubarSubTrigger>
          <MenubarSubContent className="docs-menu-content docs-menu-sub-content min-w-[260px] overflow-visible" data-docs-menu-surface>
            <MenubarItem className="docs-menu-item" disabled={disabled} onClick={actions.onShareWithUsers}>
              <MenuIcon>
                <Share2 className="size-4" />
              </MenuIcon>
              Partager avec d&apos;autres utilisateurs
            </MenubarItem>
            <MenubarItem className="docs-menu-item" disabled={disabled} onClick={actions.onPublishToWeb}>
              <MenuIcon>
                <Globe2 className="size-4" />
              </MenuIcon>
              Publier sur le Web
            </MenubarItem>
          </MenubarSubContent>
        </DocsExclusiveMenuSub>

        <DocsExclusiveMenuSub id="email">
          <MenubarSubTrigger className="docs-menu-item" disabled={disabled}>
            <MenuIcon>
              <Mail className="size-4" />
            </MenuIcon>
            Envoyer par e-mail
          </MenubarSubTrigger>
          <MenubarSubContent className="docs-menu-content docs-menu-sub-content min-w-[280px] overflow-visible" data-docs-menu-surface>
            <MenubarItem className="docs-menu-item" disabled={disabled} onClick={actions.onEmailFile}>
              Envoyer ce fichier par e-mail
            </MenubarItem>
            <MenubarItem
              className="docs-menu-item"
              disabled={disabled}
              onClick={actions.onEmailCollaborators}
            >
              Envoyer par e-mail aux collaborateurs
            </MenubarItem>
            <MenubarItem className="docs-menu-item" disabled={disabled} onClick={actions.onEmailDraft}>
              Brouillon d&apos;e-mail
            </MenubarItem>
          </MenubarSubContent>
        </DocsExclusiveMenuSub>

        <DocsExclusiveMenuSub id="download">
          <MenubarSubTrigger className="docs-menu-item" disabled={disabled}>
            <MenuIcon>
              <Download className="size-4" />
            </MenuIcon>
            Télécharger
          </MenubarSubTrigger>
          <MenubarSubContent className="docs-menu-content docs-menu-sub-content min-w-[280px] overflow-visible" data-docs-menu-surface>
            {DOCS_DOWNLOAD_FORMATS.map((format) => (
              <MenubarItem
                key={format.id}
                className="docs-menu-item"
                disabled={disabled}
                onClick={() => actions.onDownload(format.id)}
              >
                {format.label}
              </MenubarItem>
            ))}
          </MenubarSubContent>
        </DocsExclusiveMenuSub>

        <MenubarSeparator />

        <MenubarItem className="docs-menu-item" disabled={disabled} onClick={actions.onRename}>
          <MenuIcon>
            <Pencil className="size-4" />
          </MenuIcon>
          Renommer
        </MenubarItem>

        <MenubarItem className="docs-menu-item" disabled={disabled} onClick={actions.onMove}>
          <MenuIcon>
            <FolderInput className="size-4" />
          </MenuIcon>
          Déplacer
        </MenubarItem>

        <MenubarItem className="docs-menu-item" disabled={disabled} onClick={actions.onAddShortcut}>
          <MenuIcon>
            <HardDrive className="size-4" />
          </MenuIcon>
          Ajouter un raccourci dans Drive
        </MenubarItem>

        <MenubarItem
          className={cn("docs-menu-item", "focus:text-destructive")}
          disabled={disabled}
          onClick={actions.onMoveToTrash}
        >
          <MenuIcon>
            <Trash2 className="size-4" />
          </MenuIcon>
          Placer dans la corbeille
        </MenubarItem>

        <MenubarSeparator />

        <DocsExclusiveMenuSub id="version-history">
          <MenubarSubTrigger className="docs-menu-item" disabled={disabled}>
            <MenuIcon>
              <History className="size-4" />
            </MenuIcon>
            Historique des versions
          </MenubarSubTrigger>
          <MenubarSubContent className="docs-menu-content docs-menu-sub-content min-w-[260px] overflow-visible" data-docs-menu-surface>
            <MenubarItem
              className="docs-menu-item"
              disabled={disabled}
              onClick={actions.onNameCurrentVersion}
            >
              Nommer la version actuelle
            </MenubarItem>
            <MenubarItem
              className="docs-menu-item"
              disabled={disabled}
              onClick={actions.onShowVersionHistory}
            >
              Afficher l&apos;historique des versions
            </MenubarItem>
          </MenubarSubContent>
        </DocsExclusiveMenuSub>

        <MenubarItem className="docs-menu-item" disabled={disabled} onClick={actions.onToggleOffline}>
          <MenuIcon>
            <CheckCircle2 className="size-4" />
          </MenuIcon>
          Rendre disponible hors connexion
        </MenubarItem>

        <MenubarSeparator />

        <MenubarItem className="docs-menu-item" disabled={disabled} onClick={actions.onDetails}>
          <MenuIcon>
            <Info className="size-4" />
          </MenuIcon>
          Détails
        </MenubarItem>

        <MenubarItem className="docs-menu-item" disabled={disabled} onClick={actions.onSecurityLimits}>
          <MenuIcon>
            <Shield className="size-4" />
          </MenuIcon>
          Limites de sécurité
        </MenubarItem>

        <MenubarItem className="docs-menu-item" disabled={disabled} onClick={actions.onPageSetup}>
          <MenuIcon>
            <FileText className="size-4" />
          </MenuIcon>
          Configuration de la page
        </MenubarItem>

        {actions.onPurgeSidecarAndReimport ? (
          <>
            <MenubarSeparator />
            <MenubarItem
              className={cn("docs-menu-item", "text-amber-700 focus:text-amber-800 dark:text-amber-400")}
              disabled={disabled}
              onClick={actions.onPurgeSidecarAndReimport}
            >
              <MenuIcon>
                <Trash2 className="size-4" />
              </MenuIcon>
              Purger sidecar et réimporter doc
            </MenubarItem>
          </>
        ) : null}

        <MenubarItem className="docs-menu-item" disabled={disabled} onClick={actions.onPrint}>
          <MenuIcon>
            <Printer className="size-4" />
          </MenuIcon>
          Imprimer
          <DocsMenuShortcut shortcutId="file.print" />
        </MenubarItem>
        </DocsExclusiveMenuSubRoot>
      </MenubarContent>
    </MenubarMenu>
  )
}
