"use client";

import Link from "next/link";
import type { Route } from "next";
import { usePathname } from "next/navigation";
import { cn } from "@/lib/utils";

function TodayIcon({ active }: { active: boolean }) {
  return (
    <svg viewBox="0 0 20 20" fill="none" aria-hidden="true" className={cn("h-[18px] w-[18px]", active ? "text-stone-900" : "text-stone-400")}>
      <rect x="3.5" y="4.5" width="13" height="12" rx="2.5" stroke="currentColor" strokeWidth="1.5" />
      <path d="M6.5 2.8v3.1M13.5 2.8v3.1M3.5 8h13" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" />
    </svg>
  );
}

function PaymentsIcon({ active }: { active: boolean }) {
  return (
    <svg viewBox="0 0 20 20" fill="none" aria-hidden="true" className={cn("h-[18px] w-[18px]", active ? "text-stone-900" : "text-stone-400")}>
      <rect x="2.8" y="4.5" width="14.4" height="11" rx="2.5" stroke="currentColor" strokeWidth="1.5" />
      <path d="M2.8 8.1h14.4" stroke="currentColor" strokeWidth="1.5" />
      <path d="M6 12.1h3.2" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" />
    </svg>
  );
}

function PeopleIcon({ active }: { active: boolean }) {
  return (
    <svg viewBox="0 0 20 20" fill="none" aria-hidden="true" className={cn("h-[18px] w-[18px]", active ? "text-stone-900" : "text-stone-400")}>
      <path d="M10 10a2.9 2.9 0 1 0 0-5.8A2.9 2.9 0 0 0 10 10Z" stroke="currentColor" strokeWidth="1.5" />
      <path d="M4.8 16.2c.8-2.3 2.8-3.7 5.2-3.7s4.4 1.4 5.2 3.7" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" />
    </svg>
  );
}

function HistoryIcon({ active }: { active: boolean }) {
  return (
    <svg viewBox="0 0 20 20" fill="none" aria-hidden="true" className={cn("h-[18px] w-[18px]", active ? "text-stone-900" : "text-stone-400")}>
      <path d="M10 4.1a5.9 5.9 0 1 1-5.4 8.3" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" />
      <path d="M10 6.4v3.8l2.2 1.6" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
      <path d="M4 4.7v3.1h3.1" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  );
}

export function TabBar() {
  const pathname = usePathname();
  const items: Array<{
    href: Route;
    label: string;
    icon: (active: boolean) => React.ReactNode;
    active: boolean;
  }> = [
    {
      href: "/today",
      label: "Today",
      icon: (active) => <TodayIcon active={active} />,
      active: pathname === "/today"
    },
    {
      href: "/payments",
      label: "Payments",
      icon: (active) => <PaymentsIcon active={active} />,
      active: pathname === "/payments"
    },
    {
      href: "/people",
      label: "People",
      icon: (active) => <PeopleIcon active={active} />,
      active: pathname === "/people" || pathname.startsWith("/people/")
    },
    {
      href: "/history",
      label: "History",
      icon: (active) => <HistoryIcon active={active} />,
      active: pathname === "/history" || pathname.startsWith("/history/")
    }
  ];

  return (
    <nav className="fixed bottom-0 left-1/2 z-40 flex w-full max-w-md -translate-x-1/2 justify-between border-t border-stone-200 bg-white px-2 pb-[max(env(safe-area-inset-bottom),0.5rem)] pt-2">
      {items.map((item) => (
        <Link
          key={item.href}
          href={item.href}
          className="flex min-w-0 flex-1 flex-col items-center justify-center gap-1 px-1 py-2"
        >
          {item.icon(item.active)}
          <span className={cn("text-[11px] font-medium leading-none", item.active ? "text-stone-900" : "text-stone-500")}>
            {item.label}
          </span>
        </Link>
      ))}
    </nav>
  );
}
