/**
 * AuthorMeta - Composable metadata component for authors
 */

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

const authorMetaVariants = cva(
  "flex items-center gap-1 text-muted-foreground",
  {
    variants: {
      size: {
        xs: "text-xs",
        sm: "text-xs",
        md: "text-sm",
        lg: "text-base",
      },
    },
    defaultVariants: {
      size: "sm",
    },
  }
);

interface AuthorMetaProps
  extends React.HTMLAttributes<HTMLDivElement>,
    VariantProps<typeof authorMetaVariants> {
  author: Pick<AuthorWithStats, "birthYear" | "deathYear" | "country"> & {
    language?: string;
  };
  showYears?: boolean;
  showCountry?: boolean;
  showLanguage?: boolean;
  format?: "compact" | "full";
}

export function AuthorMeta({
  author,
  size,
  showYears = true,
  showCountry = true,
  showLanguage = true,
  format = "compact",
  className,
  ...props
}: AuthorMetaProps) {
  const metaItems: Array<{ icon: React.ReactNode; text: string; key: string }> =
    [];

  if (showYears && (author.birthYear || author.deathYear)) {
    const yearText = formatLifespan(author.birthYear, author.deathYear);
    metaItems.push({
      icon: <Calendar className="h-3 w-3" />,
      text: yearText,
      key: "years",
    });
  }

  if (showCountry && author.country) {
    metaItems.push({
      icon: <MapPin className="h-3 w-3" />,
      text: author.country,
      key: "country",
    });
  }

  if (showLanguage && author.language) {
    metaItems.push({
      icon: <Globe className="h-3 w-3" />,
      text: author.language,
      key: "language",
    });
  }

  if (metaItems.length === 0) return null;

  return (
    <div className={cn("flex flex-wrap gap-3", className)} {...props}>
      {metaItems.map(({ icon, text, key }) => (
        <div key={key} className={authorMetaVariants({ size })}>
          {icon}
          <span>{text}</span>
        </div>
      ))}
    </div>
  );
}

export type { AuthorMetaProps };
export { authorMetaVariants };
