"use client";

import Image from "next/image";
import Link from "next/link";
import { useEffect, useState } from "react";
import { useCartTotals } from "@/store/cart-store";
import { ROUTES } from "@/constants/routes";
import { Pill } from "@/components/ui/Pill";
import { IconButton } from "@/components/ui/IconButton";
import { CategoryMegaMenu } from "@/components/layout/CategoryMegaMenu";

// Top-bar icons are 24x24 with a 1.5 stroke, matching the Figma spec
// (call-02 / mail-at-sign-01 / delivery-tracking-02 / arrow-down-01).
const strokeProps = {
  stroke: "currentColor",
  strokeWidth: 1.5,
  strokeLinecap: "round",
  strokeLinejoin: "round",
} as const;

function PhoneIcon() {
  return (
    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <path
        d="M6.6 10.8c1.4 2.8 3.8 5.2 6.6 6.6l2.2-2.2c.3-.3.7-.4 1-.2 1.1.4 2.3.6 3.6.6.6 0 1 .4 1 1V20c0 .6-.4 1-1 1C10.9 21 3 13.1 3 3.9c0-.6.4-1 1-1h3.4c.6 0 1 .4 1 1 0 1.2.2 2.4.6 3.5.1.4 0 .8-.2 1L6.6 10.8Z"
        {...strokeProps}
      />
    </svg>
  );
}

function MailIcon() {
  return (
    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <path d="M3 7.5a2.5 2.5 0 0 1 2.5-2.5h13A2.5 2.5 0 0 1 21 7.5v6.2" {...strokeProps} />
      <path d="M3 7.5v9A2.5 2.5 0 0 0 5.5 19h8.2" {...strokeProps} />
      <path d="m3.5 7.8 7.3 5.2c.7.5 1.7.5 2.4 0l7.3-5.2" {...strokeProps} />
      <circle cx="18" cy="18" r="3.2" {...strokeProps} />
    </svg>
  );
}

function TrackOrderIcon() {
  return (
    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <path d="M3 8.5h18" {...strokeProps} />
      <path d="M3 6.5A1.5 1.5 0 0 1 4.5 5h15A1.5 1.5 0 0 1 21 6.5v2H3v-2Z" {...strokeProps} />
      <path d="M21 8.5v5.2M3 8.5v9A1.5 1.5 0 0 0 4.5 19h8.6" {...strokeProps} />
      <circle cx="17.5" cy="17" r="3" {...strokeProps} />
      <path d="m21 20.5-1.4-1.4" {...strokeProps} />
    </svg>
  );
}

function ChevronDownIcon() {
  return (
    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <path d="m6 9.5 6 6 6-6" {...strokeProps} />
    </svg>
  );
}

function SearchButtonIcon() {
  return (
    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <path d="M16.0001 16.5L20 20.5" stroke="white" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
      <path
        d="M18 11.5C18 15.366 14.866 18.5 11 18.5C7.13401 18.5 4 15.366 4 11.5C4 7.63404 7.13401 4.50003 11 4.50003"
        stroke="white"
        strokeWidth="1.5"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
      <path
        d="M15.5 3.50003L15.7579 4.19706C16.0961 5.11105 16.2652 5.56805 16.5986 5.90142C16.932 6.2348 17.389 6.4039 18.303 6.74211L19 7.00003L18.303 7.25795C17.389 7.59616 16.932 7.76527 16.5986 8.09864C16.2652 8.43201 16.0961 8.88901 15.7579 9.803L15.5 10.5L15.2421 9.803C14.9039 8.88901 14.7348 8.43201 14.4014 8.09864C14.068 7.76527 13.611 7.59616 12.697 7.25795L12 7.00003L12.697 6.74211C13.611 6.4039 14.068 6.2348 14.4014 5.90142C14.7348 5.56805 14.9039 5.11105 15.2421 4.19706L15.5 3.50003Z"
        fill="white"
        stroke="white"
        strokeWidth="1.5"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
    </svg>
  );
}

function LocationIcon() {
  return (
    <svg width="12" height="15" viewBox="0 0 12 15" fill="none" aria-hidden="true">
      <path
        d="M5.99967 0.333008C2.686 0.333008 0 3.01901 0 6.33268C0 10.8327 5.99967 14.3327 5.99967 14.3327C5.99967 14.3327 11.9993 10.8327 11.9993 6.33268C11.9993 3.01901 9.31334 0.333008 5.99967 0.333008ZM5.99967 8.16601C4.98734 8.16601 4.16634 7.34501 4.16634 6.33268C4.16634 5.32034 4.98734 4.49934 5.99967 4.49934C7.01201 4.49934 7.83301 5.32034 7.83301 6.33268C7.83301 7.34501 7.01201 8.16601 5.99967 8.16601Z"
        fill="currentColor"
      />
    </svg>
  );
}

function MessageBubbleIcon() {
  return (
    <svg width="21" height="21" viewBox="0 0 21 21" fill="none" aria-hidden="true">
      <path
        d="M16.625 3.5H15.6616C15.2548 1.50587 13.4873 0 11.375 0H4.375C1.96263 0 0 1.96263 0 4.375V15.6222C0 16.317 0.380625 16.9522 0.99225 17.2795C1.2705 17.4291 1.57587 17.5026 1.88037 17.5026C2.24437 17.5026 2.6075 17.3967 2.92162 17.1876L5.50287 15.4665C6.1075 17.1596 7.72625 18.375 9.625 18.375H14.6099L18.0784 20.6876C18.3934 20.8976 18.7556 21.0035 19.1196 21.0035C19.4241 21.0035 19.7286 20.9291 20.0077 20.7795C20.6202 20.4522 21 19.817 21 19.1222V7.875C21 5.46263 19.0374 3.5 16.625 3.5Z"
        fill="#394149"
      />
    </svg>
  );
}

function BellIcon() {
  return (
    <svg width="21" height="21" viewBox="0 0 21 21" fill="none" aria-hidden="true">
      <path
        d="M20.3516 14.175L18.0766 5.97537C17.5908 4.22136 16.5323 2.68025 15.0695 1.5973C13.6067 0.514351 11.8237 -0.0481935 10.0043 -0.000803778C8.18485 0.0465859 6.43356 0.701187 5.02912 1.85882C3.62469 3.01645 2.64782 4.61058 2.25399 6.3875L0.494362 14.3062C0.409218 14.6899 0.411295 15.0877 0.500439 15.4705C0.589584 15.8532 0.76352 16.211 1.00942 16.5176C1.25531 16.8241 1.5669 17.0715 1.92118 17.2415C2.27545 17.4116 2.66339 17.4999 3.05636 17.5H6.21249C6.41331 18.489 6.94988 19.3782 7.73126 20.0168C8.51265 20.6555 9.4908 21.0044 10.5 21.0044C11.5092 21.0044 12.4873 20.6555 13.2687 20.0168C14.0501 19.3782 14.5867 18.489 14.7875 17.5H17.8237C18.2281 17.4998 18.6269 17.4062 18.9891 17.2265C19.3514 17.0468 19.6672 16.7859 19.9119 16.464C20.1567 16.1422 20.3238 15.7682 20.4002 15.3711C20.4767 14.974 20.4594 14.5647 20.3516 14.175Z"
        fill="#394149"
      />
    </svg>
  );
}

function AlertIcon() {
  return (
    <svg width="21" height="21" viewBox="0 0 21 21" fill="none" aria-hidden="true">
      <path
        d="M20.125 17.5C19.8929 17.5 19.6704 17.5922 19.5063 17.7563C19.3422 17.9204 19.25 18.1429 19.25 18.375C19.25 18.676 18.6532 19.25 17.7188 19.25C17.4562 19.2592 17.195 19.2099 16.9539 19.1056C16.7128 19.0014 16.4979 18.8447 16.3249 18.6471C16.3591 18.5913 16.3965 18.5376 16.4369 18.4861C17.8638 16.9896 18.8235 15.1095 19.1984 13.076C19.3093 12.4642 19.1995 11.8329 18.8885 11.2944C18.5775 10.756 18.0854 10.3455 17.5 10.136V7.875C17.5 6.94674 17.1313 6.0565 16.4749 5.40013C15.8185 4.74375 14.9283 4.375 14 4.375V3.5C14 2.57174 13.6313 1.6815 12.9749 1.02513C12.3185 0.368749 11.4283 0 10.5 0C9.57174 0 8.6815 0.368749 8.02513 1.02513C7.36875 1.6815 7 2.57174 7 3.5V4.375C6.07174 4.375 5.1815 4.74375 4.52513 5.40013C3.86875 6.0565 3.5 6.94674 3.5 7.875V10.1246C2.90727 10.3294 2.40742 10.7395 2.09091 11.2809C1.77441 11.8223 1.66217 12.459 1.7745 13.076C2.15876 15.112 3.12777 16.9919 4.56313 18.4861C4.60511 18.5389 4.64397 18.5941 4.6795 18.6515C4.50628 18.8498 4.29066 19.0065 4.04861 19.1102C3.80655 19.2138 3.54429 19.2616 3.28125 19.25C2.32838 19.25 1.75 18.6427 1.75 18.375C1.75 18.1429 1.65781 17.9204 1.49372 17.7563C1.32962 17.5922 1.10706 17.5 0.875 17.5C0.642936 17.5 0.420376 17.5922 0.256282 17.7563C0.0921872 17.9204 0 18.1429 0 18.375C0 19.7977 1.50325 21 3.28125 21C4.16603 21.008 5.02416 20.6974 5.69887 20.125C6.37184 20.687 7.22013 20.9961 8.09691 20.9986C8.97369 21.0012 9.82377 20.6971 10.5 20.139C11.1787 20.6965 12.0304 21.0001 12.9088 20.9976C13.7871 20.995 14.6371 20.6865 15.3125 20.125C15.9832 20.6943 16.8355 21.0047 17.7153 21C19.4967 21 21 19.7977 21 18.375C21 18.1429 20.9078 17.9204 20.7437 17.7563C20.5796 17.5922 20.3571 17.5 20.125 17.5ZM8.75 3.5C8.75 3.03587 8.93437 2.59075 9.26256 2.26256C9.59075 1.93437 10.0359 1.75 10.5 1.75C10.9641 1.75 11.4092 1.93437 11.7374 2.26256C12.0656 2.59075 12.25 3.03587 12.25 3.5V4.375H8.75V3.5ZM7 6.125H14C14.4641 6.125 14.9092 6.30937 15.2374 6.63756C15.5656 6.96575 15.75 7.41087 15.75 7.875V9.555L11.3295 8.10425C10.7921 7.92749 10.2123 7.92749 9.67488 8.10425L5.25 9.54887V7.875C5.25 7.41087 5.43437 6.96575 5.76256 6.63756C6.09075 6.30937 6.53587 6.125 7 6.125ZM8.09375 19.25C7.8013 19.266 7.50968 19.206 7.24731 19.0758C6.98494 18.9456 6.76077 18.7497 6.59662 18.5071C6.41436 18.0702 6.16392 17.665 5.85462 17.3066C4.64836 16.0578 3.83041 14.4857 3.5 12.7811C3.4596 12.5715 3.49586 12.3542 3.60218 12.1691C3.7085 11.9839 3.87781 11.8431 4.07925 11.7722L9.625 9.96012V18.375C9.625 18.676 9.02825 19.25 8.09375 19.25ZM12.9062 19.25C11.9534 19.25 11.375 18.6427 11.375 18.375V9.961L16.8919 11.7714C17.094 11.842 17.264 11.9829 17.371 12.1684C17.4779 12.3539 17.5147 12.5717 17.4746 12.782C17.1538 14.4843 16.3444 16.0564 15.1454 17.3066C14.8341 17.6655 14.5835 18.0728 14.4034 18.5124C14.2392 18.7545 14.0148 18.9499 13.7524 19.0792C13.4899 19.2085 13.1983 19.2674 12.9062 19.25Z"
        fill="#394149"
      />
    </svg>
  );
}

function CartIcon() {
  return (
    <svg width="21" height="21" viewBox="0 0 21 21" fill="none" aria-hidden="true">
      <path
        d="M6.125 21C7.0915 21 7.875 20.2165 7.875 19.25C7.875 18.2835 7.0915 17.5 6.125 17.5C5.1585 17.5 4.375 18.2835 4.375 19.25C4.375 20.2165 5.1585 21 6.125 21Z"
        fill="#394149"
      />
      <path
        d="M14.875 21C15.8415 21 16.625 20.2165 16.625 19.25C16.625 18.2835 15.8415 17.5 14.875 17.5C13.9085 17.5 13.125 18.2835 13.125 19.25C13.125 20.2165 13.9085 21 14.875 21Z"
        fill="#394149"
      />
      <path
        d="M19.0496 8.51025C18.9365 8.48908 18.8203 8.49058 18.7077 8.51465C18.5952 8.53873 18.4885 8.5849 18.3939 8.6505C18.2994 8.7161 18.2188 8.79982 18.1568 8.89681C18.0948 8.99379 18.0527 9.10212 18.0329 9.2155C17.9237 9.82113 17.6053 10.3692 17.1332 10.7639C16.6611 11.1586 16.0653 11.3749 15.4499 11.375H4.74075L3.91825 4.375H11.375C11.6071 4.375 11.8296 4.28281 11.9937 4.11872C12.1578 3.95462 12.25 3.73206 12.25 3.5C12.25 3.26794 12.1578 3.04538 11.9937 2.88128C11.8296 2.71719 11.6071 2.625 11.375 2.625H3.71175L3.675 2.317C3.59959 1.67877 3.29268 1.09038 2.81241 0.663341C2.33215 0.236297 1.71192 0.000273732 1.06925 0L0.875 0C0.642936 0 0.420376 0.0921872 0.256282 0.256282C0.0921872 0.420376 0 0.642936 0 0.875C0 1.10706 0.0921872 1.32962 0.256282 1.49372C0.420376 1.65781 0.642936 1.75 0.875 1.75H1.06925C1.28357 1.75003 1.49042 1.82871 1.65058 1.97113C1.81073 2.11354 1.91305 2.30978 1.93812 2.52262L3.14213 12.7601C3.26712 13.8248 3.77867 14.8066 4.57967 15.519C5.38067 16.2315 6.41537 16.625 7.48738 16.625H16.625C16.8571 16.625 17.0796 16.5328 17.2437 16.3687C17.4078 16.2046 17.5 15.9821 17.5 15.75C17.5 15.5179 17.4078 15.2954 17.2437 15.1313C17.0796 14.9672 16.8571 14.875 16.625 14.875H7.48738C6.94445 14.8751 6.41486 14.7069 5.97153 14.3934C5.52821 14.08 5.19297 13.6369 5.012 13.125H15.4499C16.4755 13.1251 17.4686 12.7649 18.2556 12.1072C19.0426 11.4496 19.5736 10.5363 19.7557 9.527C19.7762 9.41387 19.7741 9.29782 19.7497 9.18549C19.7253 9.07315 19.679 8.96673 19.6134 8.8723C19.5478 8.77788 19.4643 8.6973 19.3675 8.63518C19.2708 8.57306 19.1628 8.53061 19.0496 8.51025Z"
        fill="#394149"
      />
    </svg>
  );
}

function Divider() {
  return <span aria-hidden className="mx-[13px] h-6 w-px shrink-0 bg-[#d8d8d8]" />;
}

function TopUtilityBar() {
  return (
    <div className="hidden rounded-b-[30px] bg-surface-alt sm:block">
      <div className="mx-auto flex h-16 max-w-[1200px] items-center justify-between px-4 text-[16px] text-[#040404] lg:px-0">
        {/* All three contacts plus the language pill do not fit until md —
            at exactly 640, where this bar first appears, they overflowed. */}
        <div className="flex min-w-0 items-center">
          <a href="tel:+8801896127507" className="flex shrink-0 items-center gap-[13px] transition-colors hover:text-brand">
            <PhoneIcon />
            +8801896127507
          </a>
          <span className="hidden md:contents">
            <Divider />
            <a href="mailto:dkbazardotcom@gmail.com" className="flex min-w-0 items-center gap-[13px] transition-colors hover:text-brand">
              <MailIcon />
              <span className="truncate">dkbazardotcom@gmail.com</span>
            </a>
          </span>
          <Divider />
          <Link href={ROUTES.cart} className="flex shrink-0 items-center gap-[13px] transition-colors hover:text-brand">
            <TrackOrderIcon />
            Track Your order
          </Link>
        </div>

        {/* Figma paints this #1a1a1a in LINEAR_DODGE over #ebe8e7, which resolves to white. */}
        <button
          type="button"
          className="flex h-[41px] w-[125px] shrink-0 items-center justify-between rounded-[15px] bg-white pr-[12px] pl-[23px] text-[16px] text-[#040404] shadow-[inset_0_0_0_1px_rgba(153,153,153,0.18)]"
        >
          English
          <ChevronDownIcon />
        </button>
      </div>
    </div>
  );
}

export function Header() {
  const { itemCount } = useCartTotals();
  const [menuOpen, setMenuOpen] = useState(false);
  // Any press outside the panel closes it — including elsewhere in the header
  // (search box, logo, the icon row), which testing against the <header> would
  // have missed. The toggle itself is exempt so its own onClick still runs.
  useEffect(() => {
    if (!menuOpen) return;
    const onDown = (e: PointerEvent) => {
      const t = e.target as Element | null;
      if (t?.closest?.('[role="dialog"]')) return;
      if (t?.closest?.('button[aria-haspopup="dialog"]')) return;
      setMenuOpen(false);
    };
    document.addEventListener("pointerdown", onDown);
    return () => document.removeEventListener("pointerdown", onDown);
  }, [menuOpen]);

  return (
    <>
      <TopUtilityBar />

      <header
        className="sticky top-0 z-30 bg-background/95 backdrop-blur border-b border-black/5"
      >
        <div className="mx-auto max-w-[1200px] px-4 lg:px-0 py-3">
        <div className="flex items-center gap-2 sm:gap-3">
          <Link
            href={ROUTES.home}
            className="relative mr-[6px] flex h-[44px] w-[112px] shrink-0 items-center sm:h-[56px] sm:w-[140px] lg:h-[68px] lg:w-[176px]"
          >
            <Image src="/header/logo-main.png" alt="DK Bazar" fill priority sizes="(max-width: 640px) 112px, 176px" className="object-contain object-left" />
          </Link>

          {/* Below md the Categories pill is hidden, so the menu needs its own
              way in — otherwise categories are unreachable on a phone. */}
          <button
            type="button"
            aria-label="Categories"
            aria-expanded={menuOpen}
            aria-haspopup="dialog"
            onClick={() => setMenuOpen((v) => !v)}
            className="flex h-[44px] w-[44px] shrink-0 items-center justify-center rounded-[12px] bg-ink text-white md:hidden"
          >
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true">
              <path d="M4 7h16M4 12h16M4 17h16" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" />
            </svg>
          </button>

          <Pill
            variant="solid-ink"
            aria-expanded={menuOpen}
            aria-haspopup="dialog"
            onClick={() => setMenuOpen((v) => !v)}
            className="hidden h-[44px] w-[128px] shrink-0 justify-center md:inline-flex"
          >
            Categories
          </Pill>

          {/* On phones the search moves to its own row below — inline it would
              leave the logo and the icon row fighting over ~120px. */}
          <div className="hidden h-[44px] min-w-[180px] flex-1 items-center rounded-[12px] border border-[#34CBFE] bg-[#e9e5e5] pr-[4px] pl-[21px] sm:flex">
            <input
              type="search"
              placeholder="Search here..."
              className="min-w-0 flex-1 bg-transparent text-[16px] text-[#202020] outline-none placeholder:text-[#8d8d8d]"
            />
            <span className="flex h-[38px] w-[38px] shrink-0 items-center justify-center rounded-[12px] bg-ink">
              <SearchButtonIcon />
            </span>
          </div>

          <span className="flex-1 sm:hidden" />

          <Pill variant="solid-ink" className="hidden h-[44px] w-[112px] shrink-0 lg:inline-flex">
            <LocationIcon />
            Location
          </Pill>

          {/* The icon row is trimmed as the viewport narrows — cart and account
              are the two that have to survive. */}
          <div className="flex shrink-0 items-center gap-[6px] sm:gap-[8px]">
            <IconButton icon={<MessageBubbleIcon />} aria-label="Messages" className="hidden lg:flex" />
            <IconButton icon={<BellIcon />} count={12} aria-label="Notifications" className="hidden md:flex" />
            <IconButton icon={<AlertIcon />} count={0} aria-label="Alerts" className="hidden lg:flex" />
            <Link href={ROUTES.cart}>
              <IconButton icon={<CartIcon />} count={itemCount} aria-label="Cart" />
            </Link>
            <span className="ml-[2px] flex h-[44px] w-[44px] shrink-0 items-center justify-center overflow-hidden rounded-full border-[2px] border-black bg-white sm:h-[64px] sm:w-[64px]">
              <Image src="/header/avater.png" alt="User avatar" width={64} height={64} className="h-full w-full object-cover" />
            </span>
          </div>
        </div>

        {/* mobile search row */}
        <div className="mt-[10px] flex h-[44px] items-center rounded-[12px] border border-[#34CBFE] bg-[#e9e5e5] pr-[4px] pl-[16px] sm:hidden">
          <input
            type="search"
            placeholder="Search here..."
            className="min-w-0 flex-1 bg-transparent text-[16px] text-[#202020] outline-none placeholder:text-[#8d8d8d]"
          />
          <span className="flex h-[38px] w-[38px] shrink-0 items-center justify-center rounded-[12px] bg-ink">
            <SearchButtonIcon />
          </span>
        </div>
        </div>

        {menuOpen && <CategoryMegaMenu onClose={() => setMenuOpen(false)} />}
      </header>
    </>
  );
}
