'use client'
import React, { useState, useEffect, useRef, useCallback } from 'react'
import type { Header as HeaderType, Page } from '@/payload-types'
import Link from 'next/link'
import { usePathname } from 'next/navigation'
import { MenuIcon, XIcon, ChevronDownIcon, ChevronRightIcon } from 'lucide-react'
import { SocialIconsRow } from '@/components/SocialIcons'
import type { SocialLinkData } from '@/components/SocialIcons'

type NavChild = NonNullable<NonNullable<NonNullable<HeaderType['navItems']>[number]['children']>[number]>
type NavItem = NonNullable<HeaderType['navItems']>[number]

function resolveHref(item: {
  type?: string | null
  url?: string | null
  reference?: { relationTo?: string; value?: number | Page | null } | null
}): string {
  if (item.type === 'reference' && item.reference?.value) {
    const page = item.reference.value
    if (typeof page === 'object' && page !== null) {
      return page.slug === 'home' || page.slug === 'startsida' ? '/' : `/${(page as Page).slug}`
    }
    return '#'
  }
  return item.url || '#'
}

function groupChildren(children: NavChild[]): { heading: string | null; links: NavChild[] }[] {
  const main: NavChild[] = []
  const groups: Record<string, NavChild[]> = {}
  const groupOrder: string[] = []

  for (const child of children) {
    if (!child.group) {
      main.push(child)
    } else {
      if (!groups[child.group]) {
        groups[child.group] = []
        groupOrder.push(child.group)
      }
      groups[child.group].push(child)
    }
  }

  const result: { heading: string | null; links: NavChild[] }[] = []
  if (main.length > 0) result.push({ heading: null, links: main })
  for (const g of groupOrder) result.push({ heading: g, links: groups[g] })
  return result
}

function useFocusTrap(containerRef: React.RefObject<HTMLElement | null>, active: boolean) {
  useEffect(() => {
    if (!active || !containerRef.current) return
    const container = containerRef.current
    const sel = 'a[href], button:not([disabled]), input:not([disabled]), [tabindex]:not([tabindex="-1"])'
    const getEls = () => Array.from(container.querySelectorAll<HTMLElement>(sel))
    const els = getEls()
    if (els.length > 0) setTimeout(() => els[0]?.focus(), 100)
    const handleKey = (e: KeyboardEvent) => {
      if (e.key !== 'Tab') return
      const focusable = getEls()
      if (!focusable.length) return
      const first = focusable[0]
      const last = focusable[focusable.length - 1]
      if (e.shiftKey) {
        if (document.activeElement === first) { e.preventDefault(); last.focus() }
      } else {
        if (document.activeElement === last) { e.preventDefault(); first.focus() }
      }
    }
    document.addEventListener('keydown', handleKey)
    return () => document.removeEventListener('keydown', handleKey)
  }, [active, containerRef])
}

function isActiveSection(item: NavItem, pathname: string | null): boolean {
  if (!pathname || !item.children?.length) {
    const href = resolveHref(item)
    if (href === '/') return pathname === '/'
    return pathname?.startsWith(href) || false
  }
  return item.children.some((child) => {
    const childHref = resolveHref(child)
    if (childHref === '/' || childHref === '#') return false
    return pathname?.startsWith(childHref) || false
  })
}

const MegaMenuPanel: React.FC<{
  item: NavItem
  headerRef: React.RefObject<HTMLElement | null>
  panelRef: React.RefObject<HTMLDivElement | null>
  onClose: () => void
}> = ({ item, headerRef, panelRef, onClose }) => {
  const [top, setTop] = useState(64)

  useEffect(() => {
    const update = () => {
      if (headerRef.current) {
        setTop(headerRef.current.getBoundingClientRect().bottom)
      }
    }
    update()
    window.addEventListener('scroll', update, { passive: true })
    window.addEventListener('resize', update)
    return () => {
      window.removeEventListener('scroll', update)
      window.removeEventListener('resize', update)
    }
  }, [headerRef])

  const columns = groupChildren(item.children || [])

  return (
    <>
      {/* Backdrop */}
      <div
        className="fixed inset-0 z-30"
        style={{ top, backdropFilter: 'blur(4px)', backgroundColor: 'rgba(0,0,0,0.5)' }}
        onClick={onClose}
      />
      {/* Panel — dark, sharp corners */}
      <div
        ref={panelRef}
        className="fixed left-0 right-0 z-40 bg-lm-dark border-b border-white/10"
        style={{ top }}
      >
        <div className="max-w-[1200px] mx-auto px-6 md:px-8 py-8 md:py-12">
          <div className="grid grid-cols-1 md:grid-cols-[180px_1fr] gap-8 md:gap-12">
            <div>
              <h2 className="text-white font-bold" style={{ fontSize: '1.375rem' }}>
                {item.label}
              </h2>
            </div>
            <div className={`grid gap-6 ${
              columns.length === 1 ? 'grid-cols-1' :
              columns.length === 2 ? 'grid-cols-2' : 'grid-cols-3'
            }`}>
              {columns.map((col, ci) => (
                <div key={ci}>
                  {col.heading && (
                    <p className="text-white/40 text-xs font-semibold uppercase tracking-widest mb-3">
                      {col.heading}
                    </p>
                  )}
                  <ul className="flex flex-col gap-0">
                    {col.links.map((link, li) => (
                      <li key={li}>
                        <Link
                          href={resolveHref(link)}
                          onClick={onClose}
                          className={`block py-1.5 transition-colors duration-150 ${
                            col.heading
                              ? 'text-white/70 hover:text-lm-teal text-base'
                              : 'text-white font-semibold text-lg hover:text-lm-teal'
                          }`}
                        >
                          {link.label}
                        </Link>
                      </li>
                    ))}
                  </ul>
                </div>
              ))}
            </div>
          </div>
        </div>
      </div>
    </>
  )
}

export const HeaderNav: React.FC<{ data: HeaderType; socialLinks?: SocialLinkData[] }> = ({
  data,
  socialLinks = [],
}) => {
  const navItems = (data?.navItems || []) as NavItem[]
  const [mobileOpen, setMobileOpen] = useState(false)
  const [openDropdown, setOpenDropdown] = useState<string | null>(null)
  const [mobileOpenSection, setMobileOpenSection] = useState<string | null>(null)
  const pathname = usePathname()
  const navRef = useRef<HTMLDivElement>(null)
  const headerRef = useRef<HTMLElement | null>(null)
  const megaMenuRef = useRef<HTMLDivElement | null>(null)
  const mobileMenuRef = useRef<HTMLDivElement | null>(null)

  useFocusTrap(mobileMenuRef, mobileOpen)

  useEffect(() => {
    if (navRef.current) headerRef.current = navRef.current.closest('header')
  }, [])

  useEffect(() => {
    setMobileOpen(false)
    setMobileOpenSection(null)
    setOpenDropdown(null)
  }, [pathname])

  useEffect(() => {
    document.body.style.overflow = mobileOpen ? 'hidden' : ''
    return () => { document.body.style.overflow = '' }
  }, [mobileOpen])

  useEffect(() => {
    const handler = (e: MouseEvent) => {
      if (
        navRef.current && !navRef.current.contains(e.target as Node) &&
        megaMenuRef.current && !megaMenuRef.current.contains(e.target as Node)
      ) setOpenDropdown(null)
    }
    document.addEventListener('mousedown', handler)
    return () => document.removeEventListener('mousedown', handler)
  }, [])

  useEffect(() => {
    const handler = (e: KeyboardEvent) => {
      if (e.key === 'Escape') { setOpenDropdown(null); setMobileOpen(false) }
    }
    document.addEventListener('keydown', handler)
    return () => document.removeEventListener('keydown', handler)
  }, [])

  const closeMobile = () => { setMobileOpen(false); setMobileOpenSection(null) }
  const activeItem = navItems.find(item => item.label === openDropdown)

  return (
    <>
      {/* ── Desktop nav ── */}
      <nav ref={navRef} className="hidden lg:flex gap-7 items-center">
        {navItems.map((item, i) => {
          const hasChildren = item.children && item.children.length > 0
          const isOpen = openDropdown === item.label
          const isMega = item.megaMenu && hasChildren
          const isActive = isActiveSection(item, pathname)

          return hasChildren ? (
            <div key={i} className="relative">
              <button
                onClick={() => setOpenDropdown(isOpen ? null : item.label ?? null)}
                className={`flex items-center gap-1 font-medium text-sm transition-colors ${
                  isOpen || isActive ? 'text-lm-teal' : 'text-white hover:text-lm-teal'
                }`}
                aria-haspopup="true"
                aria-expanded={isOpen}
              >
                {item.label}
                <ChevronDownIcon
                  className={`w-3.5 h-3.5 transition-transform duration-200 ${isOpen ? 'rotate-180' : ''}`}
                />
              </button>

              {/* Small dropdown — dark, sharp */}
              {!isMega && (
                <div
                  className={`absolute top-full left-0 mt-2 min-w-[200px] bg-lm-dark-3 border border-white/10 py-1 z-50 transition-all duration-150 origin-top shadow-xl ${
                    isOpen
                      ? 'opacity-100 scale-y-100 pointer-events-auto'
                      : 'opacity-0 scale-y-95 pointer-events-none'
                  }`}
                >
                  {item.children!.map((child, j) => (
                    <Link
                      key={j}
                      href={resolveHref(child)}
                      onClick={() => setOpenDropdown(null)}
                      className="block px-4 py-2.5 text-white/80 hover:text-white hover:bg-white/5 transition-colors text-sm"
                    >
                      {child.label}
                    </Link>
                  ))}
                </div>
              )}
            </div>
          ) : (
            <Link
              key={i}
              href={resolveHref(item)}
              className={`relative font-medium text-sm transition-colors ${
                isActive ? 'text-lm-teal' : 'text-white hover:text-lm-teal'
              }`}
            >
              {item.label}
              {isActive && (
                <span className="absolute -bottom-1 left-0 right-0 h-px bg-lm-teal" />
              )}
            </Link>
          )
        })}
      </nav>

      {/* ── Mega menu panel ── */}
      {activeItem?.megaMenu && activeItem.children?.length && (
        <MegaMenuPanel
          item={activeItem}
          headerRef={headerRef}
          panelRef={megaMenuRef}
          onClose={() => setOpenDropdown(null)}
        />
      )}

      {/* ── Hamburger ── */}
      <button
        className="lg:hidden text-white p-2 hover:text-lm-teal transition-colors"
        onClick={() => setMobileOpen(!mobileOpen)}
        aria-label={mobileOpen ? 'Stäng meny' : 'Öppna meny'}
      >
        {mobileOpen ? <XIcon className="w-6 h-6" /> : <MenuIcon className="w-6 h-6" />}
      </button>

      {/* ── Mobile backdrop ── */}
      <div
        className={`fixed inset-0 z-40 lg:hidden transition-opacity duration-300 ${
          mobileOpen ? 'opacity-100 pointer-events-auto' : 'opacity-0 pointer-events-none'
        }`}
        style={{ backdropFilter: 'blur(4px)', backgroundColor: 'rgba(0,0,0,0.6)' }}
        onClick={closeMobile}
        aria-hidden="true"
      />

      {/* ── Mobile slide-in — dark ── */}
      <div
        ref={mobileMenuRef}
        className={`fixed inset-y-0 right-0 z-50 flex flex-col bg-lm-dark lg:hidden
          w-full md:w-[400px]
          transition-transform duration-300
          ${mobileOpen ? 'translate-x-0 shadow-[-24px_0_60px_rgba(0,0,0,0.4)]' : 'translate-x-full'}
        `}
        role="dialog"
        aria-modal="true"
        aria-label="Navigeringsmeny"
      >
        {/* Header */}
        <div className="flex items-center justify-between px-6 py-4 border-b border-white/10">
          <Link href="/" onClick={closeMobile} className="text-white font-bold text-lg">
            Layer &amp; Mesh
          </Link>
          <button
            onClick={closeMobile}
            className="text-white/60 p-2 hover:text-white transition-colors"
            aria-label="Stäng meny"
          >
            <XIcon className="w-5 h-5" />
          </button>
        </div>

        {/* Links */}
        <nav className="flex-1 overflow-y-auto px-6 py-4 flex flex-col">
          {navItems.map((item, i) => {
            const hasChildren = item.children && item.children.length > 0
            const isExpanded = mobileOpenSection === item.label
            return hasChildren ? (
              <div key={i} className="border-b border-white/8">
                <button
                  onClick={() => setMobileOpenSection(isExpanded ? null : item.label ?? null)}
                  className="w-full flex justify-between items-center py-3.5 text-white font-medium text-base hover:text-lm-teal transition-colors"
                >
                  {item.label}
                  <ChevronRightIcon
                    className={`w-4 h-4 transition-transform duration-200 ${isExpanded ? 'rotate-90' : ''}`}
                  />
                </button>
                <div
                  className={`overflow-hidden transition-all duration-300 ${
                    isExpanded ? 'max-h-[400px] pb-2' : 'max-h-0'
                  }`}
                >
                  {item.children!.map((child, j) => (
                    <Link
                      key={j}
                      href={resolveHref(child)}
                      onClick={closeMobile}
                      className="block pl-4 py-2.5 text-lm-teal text-sm border-b border-white/5 hover:text-white transition-colors"
                    >
                      {child.label}
                    </Link>
                  ))}
                </div>
              </div>
            ) : (
              <Link
                key={i}
                href={resolveHref(item)}
                onClick={closeMobile}
                className="py-3.5 text-white font-medium text-base border-b border-white/8 hover:text-lm-teal transition-colors block"
              >
                {item.label}
              </Link>
            )
          })}
        </nav>

        {/* Footer */}
        <div className="px-6 py-4 border-t border-white/10 flex items-center justify-between">
          <span className="text-white/40 text-xs tracking-widest uppercase">Layer &amp; Mesh</span>
          {socialLinks.length > 0 && (
            <SocialIconsRow
              links={socialLinks}
              linkClassName="text-white/40 hover:text-white"
              iconSize="w-5 h-5"
            />
          )}
        </div>
      </div>
    </>
  )
}
