import { useState, type ReactNode } from "react";
import { cn } from "@/pages/app/components/ui";
import BoDrawer from "@/pages/app/components/bo/BoDrawer";

export default function BoHeader({
  title,
  subtitle,
  onBack,
  showMenu = false,
  action,
}: {
  title: string;
  subtitle?: string;
  onBack?: () => void;
  showMenu?: boolean;
  action?: ReactNode;
}) {
  const [open, setOpen] = useState(false);

  return (
    <>
      <div className="sticky top-0 z-30 bg-background-50/95 backdrop-blur px-4 pt-3 pb-3 flex items-center gap-3 border-b border-background-200/70">
        {onBack ? (
          <button
            type="button"
            onClick={onBack}
            className="w-10 h-10 rounded-full flex items-center justify-center text-foreground-950 hover:bg-background-200 cursor-pointer shrink-0"
          >
            <i className="ri-arrow-right-line text-[22px] leading-none" />
          </button>
        ) : showMenu ? (
          <button
            type="button"
            onClick={() => setOpen(true)}
            className="w-10 h-10 rounded-full flex items-center justify-center text-foreground-950 hover:bg-background-200 cursor-pointer shrink-0"
          >
            <i className="ri-menu-3-line text-[22px] leading-none" />
          </button>
        ) : null}

        <div className="flex-1 min-w-0">
          <h1 className="text-[17px] font-semibold text-foreground-950 truncate">{title}</h1>
          {subtitle && <p className="text-[11.5px] text-foreground-600 truncate">{subtitle}</p>}
        </div>

        {action}
      </div>
      {showMenu && <BoDrawer open={open} onClose={() => setOpen(false)} />}
    </>
  );
}

export function BoIconButton({
  icon,
  onClick,
  badge,
}: {
  icon: string;
  onClick?: () => void;
  badge?: number;
}) {
  return (
    <button
      type="button"
      onClick={onClick}
      className="relative w-10 h-10 rounded-full flex items-center justify-center text-foreground-950 hover:bg-background-200 cursor-pointer shrink-0"
    >
      <i className={cn(icon, "text-[21px] leading-none")} />
      {badge !== undefined && badge > 0 && (
        <span className="absolute -top-0.5 -left-0.5 min-w-[18px] h-[18px] px-1 rounded-full bg-primary-500 text-background-50 text-[10px] font-bold flex items-center justify-center">
          {badge}
        </span>
      )}
    </button>
  );
}