'use client'

import { useEffect, useState } from 'react'
import Link from 'next/link'
import { useTranslations } from 'next-intl'
import { Search, Menu, Sparkles, Phone } from 'lucide-react'
import Container from '@/components/frontend/Container'
import LanguageSwitcher from '@/components/frontend/LanguageSwitcher'
import { useSiteInfo } from '@/components/frontend/SiteInfoProvider'
import { HeaderActions, HeaderLogo, HeaderNav } from './HeaderParts'
import { MobileMenu, SearchModal, useHeaderState } from './useHeaderState'
import type { HeaderProps } from './Header'

// Design 3 — "Premium": a thin dark strip (offer + language), then a glassy
// row — logo, the menu centered, a search pill and the account/cart icons.
// The strip folds away on scroll so the sticky row stays slim.
export default function HeaderPremium({ mainMenu = [], languages = [], currentLocale = 'en', logoUrl = null, logoAlt }: HeaderProps) {
  const t = useTranslations('Header')
  const state = useHeaderState({ logoUrl, logoAlt })
  const { phone } = useSiteInfo()
  const [scrolled, setScrolled] = useState(false)

  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 24)
    onScroll()
    window.addEventListener('scroll', onScroll, { passive: true })
    return () => window.removeEventListener('scroll', onScroll)
  }, [])

  return (
    <>
      <header
        className={`sticky top-0 z-50 transition-all duration-300 ${
          scrolled ? 'bg-white/85 backdrop-blur-xl shadow-lg shadow-black/5' : 'bg-white'
        }`}
      >
        {/* Strip */}
        <div
          className={`relative z-10 bg-dark-bg text-white/80 text-xs transition-[max-height,opacity] duration-300 ${
            scrolled ? 'max-h-0 opacity-0 overflow-hidden' : 'max-h-12 opacity-100 overflow-visible'
          }`}
        >
          <Container>
            <div className="flex items-center justify-between gap-4 py-2">
              <Link href="/offers" className="group flex min-w-0 items-center gap-2 hover:text-white transition-colors">
                <Sparkles size={14} className="shrink-0 text-primary-light" />
                <span className="truncate">
                  {state.freeDeliveryText ? t('freeDelivery', { amount: state.freeDeliveryText }) : t('deals')}
                </span>
              </Link>
              <div className="flex shrink-0 items-center gap-5">
                {phone && (
                  <a href={`tel:${phone.replace(/[^0-9+]/g, '')}`} className="hidden sm:inline-flex items-center gap-1.5 hover:text-white transition-colors">
                    <Phone size={13} />
                    <span dir="ltr">{phone}</span>
                  </a>
                )}
                <LanguageSwitcher languages={languages} currentLocale={currentLocale} tone="dark" />
              </div>
            </div>
          </Container>
        </div>

        {/* Main row */}
        <Container>
          <div className="flex h-16 sm:h-[4.5rem] items-center gap-4 lg:gap-8">
            <button
              onClick={state.openMobileMenu}
              className="lg:hidden text-dark hover:text-primary transition-colors"
              aria-label={t('menu')}
            >
              <Menu size={24} />
            </button>

            <HeaderLogo logo={state.logo} className="text-2xl md:text-[1.7rem] font-black tracking-tight" maxWidth={170} />

            <nav className="hidden lg:flex flex-1 items-center justify-center gap-7 text-[15px] font-medium">
              <HeaderNav items={mainMenu} pathname={state.pathname} />
            </nav>

            <div className="ms-auto lg:ms-0 flex items-center gap-3 sm:gap-5">
              <button
                type="button"
                onClick={state.openSearch}
                className="hidden md:flex w-56 xl:w-64 items-center gap-2.5 rounded-full bg-gray-100 px-4 py-2.5 text-start text-sm text-gray-custom transition-colors hover:bg-gray-200/70"
              >
                <Search size={17} className="shrink-0" />
                <span className="flex-1 truncate">{t('searchPlaceholder')}</span>
              </button>
              <button onClick={state.openSearch} className="md:hidden text-gray-custom hover:text-primary" aria-label={t('search')}>
                <Search size={22} />
              </button>
              <HeaderActions state={state} iconSize={21} />
            </div>
          </div>
        </Container>
      </header>

      <SearchModal isOpen={state.isSearchOpen} onClose={state.closeSearch} />
      <MobileMenu
        isOpen={state.isMobileMenuOpen}
        onClose={state.closeMobileMenu}
        menuItems={mainMenu}
        logo={state.logo}
        isCustomerLoggedIn={state.isCustomerLoggedIn}
      />
    </>
  )
}
