'use client'

import Image from 'next/image'
import Link from 'next/link'
import { useTranslations } from 'next-intl'
import { Heart, ShoppingCart, User, LayoutDashboard } from 'lucide-react'
import SiteWordmark from '@/components/frontend/SiteWordmark'
import CartBadge from '@/components/frontend/cart/CartBadge'
import { useCartUI } from '@/components/frontend/cart/CartUIContext'
import { MenuRenderer, type MenuItem } from '@/components/frontend/MenuRenderer'
import type { HeaderState } from './useHeaderState'

// Small pieces the three header designs share.

export function HeaderLogo({ logo, className = 'text-2xl md:text-3xl', maxWidth = 180 }: { logo: HeaderState['logo']; className?: string; maxWidth?: number }) {
  return (
    <Link href="/" className="block shrink-0" aria-label={logo.text}>
      {logo.type === 'image' ? (
        <span className="relative block" style={{ width: maxWidth, height: Math.round(maxWidth / 3.75) }}>
          <Image src={logo.src} alt={logo.alt} fill sizes={`${maxWidth}px`} className="object-contain" priority />
        </span>
      ) : (
        <SiteWordmark name={logo.text} className={className} />
      )}
    </Link>
  )
}

// Admin menu ("header-complete") or, while none is set up, four basic links.
export function HeaderNav({ items, pathname, className = '' }: { items: MenuItem[]; pathname: string; className?: string }) {
  const t = useTranslations('Header')
  if (items.length > 0) return <MenuRenderer items={items} className={className} />

  const links = [
    { href: '/', label: t('home') },
    { href: '/products', label: t('shop') },
    { href: '/about', label: t('about') },
    { href: '/contact', label: t('contact') },
  ]
  return (
    <>
      {links.map((l) => (
        <Link
          key={l.href}
          href={l.href}
          className={`font-semibold transition-colors duration-300 ${pathname === l.href ? 'text-primary' : 'text-dark hover:text-primary'}`}
        >
          {l.label}
        </Link>
      ))}
    </>
  )
}

interface ActionProps {
  state: HeaderState
  // Show text labels under the icons (Modern header)
  withLabels?: boolean
  iconSize?: number
  className?: string
}

// Account · wishlist · cart. The cart link carries cartIconRef so the
// add-to-cart "flying image" lands on it (CartUIContext).
export function HeaderActions({ state, withLabels = false, iconSize = 20, className = '' }: ActionProps) {
  const t = useTranslations('Header')
  const { cartIconRef } = useCartUI()
  const item = `relative flex items-center text-gray-custom hover:text-primary transition-colors duration-300 ${
    withLabels ? 'flex-col gap-1 text-[11px] font-semibold' : ''
  }`
  const badge = 'absolute -top-2 -end-3 min-w-[1.25rem] rounded-full px-1.5 py-0.5 text-center text-[10px] font-bold leading-none text-white'

  return (
    <div className={`flex items-center ${withLabels ? 'gap-6' : 'gap-5'} ${className}`}>
      <Link
        href={state.isCustomerLoggedIn ? '/account' : '/login'}
        className={item}
        aria-label={state.isCustomerLoggedIn ? t('account') : t('signIn')}
      >
        {state.isCustomerLoggedIn ? <LayoutDashboard size={iconSize} /> : <User size={iconSize} />}
        {withLabels && <span className="hidden lg:block">{state.isCustomerLoggedIn ? t('account') : t('signIn')}</span>}
      </Link>

      <Link href="/account/wishlist" className={item} aria-label={t('wishlist')}>
        <span className="relative">
          <Heart size={iconSize} />
          {state.wishlistCount > 0 && (
            <span className={`${badge} bg-red-500`}>{state.wishlistCount > 9 ? '9+' : state.wishlistCount}</span>
          )}
        </span>
        {withLabels && <span className="hidden lg:block">{t('wishlist')}</span>}
      </Link>

      <Link
        href="/cart"
        ref={cartIconRef as React.Ref<HTMLAnchorElement>}
        className={item}
        aria-label={t('cart')}
      >
        <span className="relative">
          <ShoppingCart size={iconSize} />
          <CartBadge count={state.cartCount} className={`${badge} bg-primary`} />
        </span>
        {withLabels && <span className="hidden lg:block">{t('cart')}</span>}
      </Link>
    </div>
  )
}
