/**
 * AuthorAvatar - Composable avatar component for authors
 */

import type React from "react";
import { cva, type VariantProps } from "class-variance-authority";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import { cn } from "@/lib/utils";
import type { AuthorWithStats } from "../types";
import { authorUtils } from "../utils";

const authorAvatarVariants = cva("", {
  variants: {
    size: {
      xs: "h-6 w-6",
      sm: "h-8 w-8",
      md: "h-12 w-12",
      lg: "h-16 w-16",
      xl: "h-20 w-20",
      "2xl": "h-24 w-24",
    },
  },
  defaultVariants: {
    size: "md",
  },
});

interface AuthorAvatarProps
  extends React.HTMLAttributes<HTMLDivElement>,
    VariantProps<typeof authorAvatarVariants> {
  author: Pick<AuthorWithStats, "name" | "portrait">;
  showBadge?: boolean;
  badgeContent?: React.ReactNode;
}

export function AuthorAvatar({
  author,
  size,
  showBadge,
  badgeContent,
  className,
  ...props
}: AuthorAvatarProps) {
  return (
    <div className={cn("relative", className)} {...props}>
      <Avatar className={authorAvatarVariants({ size })}>
        <AvatarImage
          src={author.portrait}
          alt={author.name}
          className="object-cover"
        />
        <AvatarFallback className="bg-primary/10 text-primary">
          {authorUtils.getInitials(author.name)}
        </AvatarFallback>
      </Avatar>
      {showBadge && badgeContent && (
        <div className="absolute -bottom-1 -right-1">{badgeContent}</div>
      )}
    </div>
  );
}

export type { AuthorAvatarProps };
export { authorAvatarVariants };
