import { cn } from "@/lib/utils";

/** Optional fixed px size; default follows parent text (1em). */
export const VERIFICATION_BADGE_SIZE = 14;

export const VERIFICATION_COLORS = {
  verified: "#2196EB",
  license: "#EAB308",
  pending: "#9CA3AF",
} as const;

type VerificationBadgeProps = {
  isVerified?: string | null;
  isRegister?: string | boolean | null;
  className?: string;
  size?: number;
};

export function resolveVerificationStatus(
  isVerified?: string | null,
  isRegister?: string | boolean | null
): string | null {
  if (isVerified && isVerified !== "none") {
    return isVerified;
  }

  if (isRegister === true || isRegister === "true") {
    return "pending";
  }

  return null;
}

export function getVerificationIcon(
  isVerified?: string | null,
  isRegister?: string | boolean | null
): { src: string; alt: string; color: string } | null {
  const status = resolveVerificationStatus(isVerified, isRegister);
  if (!status) return null;

  if (status === "true") {
    return {
      src: "/images/icons/verify2.svg",
      alt: "تیک مجوز",
      color: VERIFICATION_COLORS.license,
    };
  }
  if (status === "verified") {
    return {
      src: "/images/icons/verify.svg",
      alt: "تیک تایید",
      color: VERIFICATION_COLORS.verified,
    };
  }
  if (status === "pending") {
    return {
      src: "/images/icons/not-verify.svg",
      alt: "در انتظار تایید",
      color: VERIFICATION_COLORS.pending,
    };
  }

  return null;
}

function VerificationBadge({
  isVerified,
  isRegister,
  className,
  size,
}: VerificationBadgeProps) {
  const icon = getVerificationIcon(isVerified, isRegister);
  if (!icon) return null;

  return (
    <span
      role="img"
      aria-label={icon.alt}
      title={icon.alt}
      className={cn(
        "inline-block shrink-0 bg-current align-middle",
        size == null && "h-[1em] w-[1em]",
        className
      )}
      style={{
        ...(size != null ? { width: size, height: size } : undefined),
        color: icon.color,
        WebkitMaskImage: `url(${icon.src})`,
        maskImage: `url(${icon.src})`,
        WebkitMaskRepeat: "no-repeat",
        maskRepeat: "no-repeat",
        WebkitMaskPosition: "center",
        maskPosition: "center",
        WebkitMaskSize: "contain",
        maskSize: "contain",
      }}
    />
  );
}

export default VerificationBadge;
