/**
 * AuthorName - Composable name component for authors
 */

import type React from "react";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/lib/utils";
import type { AuthorWithStats } from "../types";

const authorNameVariants = cva("font-medium", {
  variants: {
    size: {
      xs: "text-xs",
      sm: "text-sm",
      md: "text-base",
      lg: "text-lg",
      xl: "text-xl",
      "2xl": "text-2xl",
    },
    weight: {
      normal: "font-normal",
      medium: "font-medium",
      semibold: "font-semibold",
      bold: "font-bold",
    },
  },
  defaultVariants: {
    size: "md",
    weight: "medium",
  },
});

interface AuthorNameProps
  extends React.HTMLAttributes<HTMLHeadingElement>,
    VariantProps<typeof authorNameVariants> {
  author: Pick<AuthorWithStats, "name">;
  as?: "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "span" | "div";
  showLink?: boolean;
  href?: string;
}

export function AuthorName({
  author,
  size,
  weight,
  as: Component = "div",
  showLink,
  href,
  className,
  ...props
}: AuthorNameProps) {
  const content = (
    <Component
      className={cn(authorNameVariants({ size, weight }), className)}
      {...props}
    >
      {author.name}
    </Component>
  );

  if (showLink && href) {
    return (
      <a href={href} className="hover:text-primary transition-colors">
        {content}
      </a>
    );
  }

  return content;
}

export type { AuthorNameProps };
export { authorNameVariants };
