import Image from "next/image";
import { cn } from "@/lib/utils";

export const VERIFICATION_BADGE_SIZE = 22;

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 } | null {
  const status = resolveVerificationStatus(isVerified, isRegister);
  if (!status) return null;

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

  return null;
}

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

  return (
    <Image
      width={size}
      height={size}
      alt={icon.alt}
      src={icon.src}
      className={cn("shrink-0", className)}
    />
  );
}

export default VerificationBadge;
