import type { ReactNode } from "react";
import { BrandLogo, BrandMark } from "@/components/base/BrandLogo";

export function cn(...cls: Array<string | false | null | undefined>): string {
  return cls.filter(Boolean).join(" ");
}

/** Full lockup (mark + DSHN) — scales with the surrounding font size. */
export function Logo({ className = "" }: { className?: string }) {
  return <BrandLogo className={cn("w-[1.9em] h-[1.35em]", className)} />;
}

/** Infinity mark only — for compact square badges and small avatars. */
export function LogoMark({ className = "" }: { className?: string }) {
  return <BrandMark className={cn("w-[1.8em] h-[1em]", className)} />;
}

type BtnVariant = "primary" | "secondary" | "ghost" | "danger";

export function Btn({
  children,
  onClick,
  variant = "primary",
  full = false,
  disabled = false,
  className = "",
  type = "button",
  size = "md",
}: {
  children: ReactNode;
  onClick?: () => void;
  variant?: BtnVariant;
  full?: boolean;
  disabled?: boolean;
  className?: string;
  type?: "button" | "submit";
  size?: "md" | "sm";
}) {
  const base =
    "h-[52px] rounded-[14px] flex items-center justify-center gap-2 whitespace-nowrap transition-all duration-200 cursor-pointer active:scale-[0.98]";
  const sizes: Record<"md" | "sm", string> = {
    md: "px-5 text-[16px]",
    sm: "px-3 text-[15px]",
  };
  const variants: Record<BtnVariant, string> = {
    primary: "bg-primary-500 text-background-50 hover:bg-primary-600",
    secondary:
      "bg-background-100 text-foreground-950 border border-foreground-950 hover:bg-background-200",
    ghost: "bg-transparent text-foreground-700 hover:bg-background-200",
    danger: "bg-danger-500 text-background-50 hover:bg-danger-600",
  };
  return (
    <button
      type={type}
      onClick={onClick}
      disabled={disabled}
      className={cn(
        base,
        sizes[size],
        variants[variant],
        full && "w-full",
        disabled && "opacity-40 pointer-events-none",
        className,
      )}
    >
      {children}
    </button>
  );
}

export function IconBtn({
  icon,
  onClick,
  badge,
  dot = false,
  className = "",
  size = "w-11 h-11",
}: {
  icon: string;
  onClick?: () => void;
  badge?: number;
  dot?: boolean;
  className?: string;
  size?: string;
}) {
  return (
    <button
      type="button"
      onClick={onClick}
      className={cn(
        "relative rounded-full flex items-center justify-center text-foreground-950 hover:bg-background-200 transition-colors cursor-pointer",
        size,
        className,
      )}
    >
      <i className={cn(icon, "text-[22px] 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>
      )}
      {dot && (
        <span className="absolute top-1 left-1 w-2 h-2 rounded-full bg-danger-500 ring-2 ring-background-50" />
      )}
    </button>
  );
}

type Tone = "success" | "warn" | "danger" | "info" | "neutral" | "blush" | "mint";

export function Badge({
  children,
  tone = "neutral",
  icon,
  className = "",
}: {
  children: ReactNode;
  tone?: Tone;
  icon?: string;
  className?: string;
}) {
  const tones: Record<Tone, string> = {
    success: "bg-accent-100 text-accent-800",
    mint: "bg-accent-100 text-accent-800",
    warn: "bg-secondary-100 text-secondary-900",
    info: "bg-primary-100 text-foreground-800",
    neutral: "bg-background-200 text-foreground-700",
    danger: "bg-danger-100 text-danger-700",
    blush: "bg-blush-100 text-blush-800",
  };
  return (
    <span
      className={cn(
        "inline-flex items-center gap-1 rounded-full px-2.5 py-1 text-[11px] font-semibold whitespace-nowrap",
        tones[tone],
        className,
      )}
    >
      {icon && <i className={cn(icon, "text-[13px] leading-none")} />}
      {children}
    </span>
  );
}

export function Tag({ children, tone = "neutral" }: { children: ReactNode; tone?: Tone }) {
  const tones: Record<Tone, string> = {
    success: "bg-accent-50 text-accent-800",
    mint: "bg-accent-50 text-accent-800",
    warn: "bg-secondary-50 text-secondary-900",
    info: "bg-primary-50 text-foreground-700",
    neutral: "bg-background-200 text-foreground-700",
    danger: "bg-danger-50 text-danger-700",
    blush: "bg-blush-50 text-blush-800",
  };
  return (
    <span
      className={cn(
        "inline-flex items-center gap-1 rounded-full px-2.5 py-1 text-[10px] font-semibold whitespace-nowrap",
        tones[tone],
      )}
    >
      {children}
    </span>
  );
}

export function Card({
  children,
  className = "",
  onClick,
}: {
  children: ReactNode;
  className?: string;
  onClick?: () => void;
}) {
  return (
    <div
      onClick={onClick}
      className={cn(
        "bg-background-100 border border-background-200 rounded-[20px] p-4",
        onClick && "cursor-pointer hover:border-background-300 transition-colors",
        className,
      )}
    >
      {children}
    </div>
  );
}

export function SectionHeader({
  title,
  actionLabel,
  onAction,
  className = "",
}: {
  title: string;
  actionLabel?: string;
  onAction?: () => void;
  className?: string;
}) {
  return (
    <div className={cn("flex items-center justify-between gap-3", className)}>
      <h2 className="text-[18px] font-semibold text-foreground-950">{title}</h2>
      {actionLabel && (
        <button
          type="button"
          onClick={onAction}
          className="text-[13px] font-medium text-foreground-600 hover:text-foreground-950 flex items-center gap-0.5 cursor-pointer whitespace-nowrap"
        >
          {actionLabel}
          <i className="ri-arrow-left-s-line text-[16px] leading-none" />
        </button>
      )}
    </div>
  );
}

export function Chip({
  children,
  active = false,
  onClick,
  icon,
}: {
  children: ReactNode;
  active?: boolean;
  onClick?: () => void;
  icon?: string;
}) {
  return (
    <button
      type="button"
      onClick={onClick}
      className={cn(
        "flex items-center gap-1.5 rounded-full h-10 px-[18px] text-[13px] font-medium whitespace-nowrap transition-colors cursor-pointer shrink-0",
        active
          ? "bg-primary-500 text-background-50"
          : "bg-background-100 border border-background-200 text-foreground-700 hover:border-background-300",
      )}
    >
      {icon && <i className={cn(icon, "text-[15px] leading-none")} />}
      {children}
    </button>
  );
}

export function SearchInput({
  value,
  onChange,
  placeholder,
  onFilter,
  filterCount = 0,
}: {
  value: string;
  onChange: (v: string) => void;
  placeholder: string;
  onFilter?: () => void;
  filterCount?: number;
}) {
  return (
    <div className="flex w-full max-w-full items-center gap-2">
      <div className="flex-1 min-w-0 flex items-center gap-2 h-[52px] px-4 rounded-[14px] bg-background-100 border border-background-200 focus-within:border-foreground-400 transition-colors">
        <i className="ri-search-line text-[19px] text-foreground-500 leading-none shrink-0" />
        <input
          value={value}
          onChange={(e) => onChange(e.target.value)}
          placeholder={placeholder}
          className="flex-1 min-w-0 bg-transparent text-[14px] text-foreground-950 placeholder:text-foreground-500 outline-none font-body"
        />
        {value && (
          <button type="button" onClick={() => onChange("")} className="cursor-pointer shrink-0">
            <i className="ri-close-circle-fill text-[18px] text-foreground-400 leading-none" />
          </button>
        )}
      </div>
      {onFilter && (
        <button
          type="button"
          onClick={onFilter}
          aria-label="فلترة المنتجات"
          className="relative w-[52px] h-[52px] shrink-0 flex-none rounded-[14px] bg-background-100 border border-background-200 flex items-center justify-center text-foreground-950 hover:border-background-300 cursor-pointer"
        >
          <i className="ri-equalizer-line text-[19px] leading-none" />
          {filterCount > 0 && (
            <span className="absolute -top-1 -left-1 min-w-[18px] h-[18px] px-1 rounded-full bg-primary-500 text-background-50 text-[10px] font-bold flex items-center justify-center">
              {filterCount}
            </span>
          )}
        </button>
      )}
    </div>
  );
}

export function Stepper({
  value,
  onChange,
  min = 1,
  max = 99,
}: {
  value: number;
  onChange: (v: number) => void;
  min?: number;
  max?: number;
}) {
  return (
    <div className="flex items-center gap-3">
      <button
        type="button"
        onClick={() => onChange(Math.min(max, value + 1))}
        className="w-8 h-8 rounded-full border border-background-300 flex items-center justify-center text-foreground-950 hover:bg-background-200 cursor-pointer"
      >
        <i className="ri-add-line text-[17px] leading-none" />
      </button>
      <span className="w-8 text-center text-[15px] font-semibold text-foreground-950">{value}</span>
      <button
        type="button"
        onClick={() => onChange(Math.max(min, value - 1))}
        className="w-8 h-8 rounded-full border border-background-300 flex items-center justify-center text-foreground-950 hover:bg-background-200 cursor-pointer"
      >
        <i className="ri-subtract-line text-[17px] leading-none" />
      </button>
    </div>
  );
}

export function EmptyState({
  icon,
  title,
  desc,
  actionLabel,
  onAction,
  image,
}: {
  icon?: string;
  title: string;
  desc?: string;
  actionLabel?: string;
  onAction?: () => void;
  image?: string;
}) {
  return (
    <div className="flex flex-col items-center justify-center text-center py-14 px-6">
      {image ? (
        <div className="w-28 h-28 rounded-[24px] overflow-hidden mb-5 bg-background-200">
          <img src={image} alt={title} className="w-full h-full object-cover object-top" />
        </div>
      ) : (
        <div className="w-16 h-16 rounded-full bg-background-200 flex items-center justify-center mb-5">
          <i className={cn(icon ?? "ri-inbox-line", "text-[28px] text-foreground-500 leading-none")} />
        </div>
      )}
      <h3 className="text-[16px] font-semibold text-foreground-950">{title}</h3>
      {desc && <p className="text-[13px] text-foreground-600 mt-1.5 max-w-[240px] leading-relaxed">{desc}</p>}
      {actionLabel && onAction && (
        <div className="mt-5 w-full max-w-[220px]">
          <Btn onClick={onAction} full>
            {actionLabel}
          </Btn>
        </div>
      )}
    </div>
  );
}

export function PriceText({
  price,
  negotiable,
  size = "text-[15px]",
}: {
  price: number | null;
  negotiable: boolean;
  size?: string;
}) {
  if (negotiable || price === null) {
    return (
      <span className="inline-flex items-center rounded-full bg-secondary-100 text-secondary-900 px-2.5 py-1 text-[11px] font-semibold">
        السعر حسب التفاوض
      </span>
    );
  }
  return (
    <span className={cn("font-bold text-foreground-950", size)}>
      {price.toLocaleString("en-US")} <span className="text-[12px] font-semibold">ر.س</span>
    </span>
  );
}

export function BackHeader({
  title,
  onBack,
  action,
  subtitle,
}: {
  title: string;
  onBack: () => void;
  action?: ReactNode;
  subtitle?: string;
}) {
  return (
    <div className="sticky top-0 z-20 bg-background-50/95 backdrop-blur px-4 pt-3 pb-3 flex items-center gap-3 border-b border-background-200/70">
      <button
        type="button"
        onClick={onBack}
        className="w-11 h-11 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>
      <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>
  );
}