/**
 * AuthorStats - Composable statistics component for authors
 */

import type React from "react";
import { cva, type VariantProps } from "class-variance-authority";
import { BookOpen, Star, TrendingUp, Users } from "lucide-react";
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from "@/components/ui/tooltip";
import { cn } from "@/lib/utils";
import type { AuthorWithStats } from "../types";
import { authorUtils } from "../utils";

const authorStatsVariants = cva("", {
  variants: {
    layout: {
      horizontal: "flex items-center gap-4",
      vertical: "space-y-2",
      grid: "grid grid-cols-2 gap-2",
      "grid-3": "grid grid-cols-3 gap-2",
    },
    size: {
      sm: "text-sm",
      md: "text-base",
      lg: "text-lg",
    },
  },
  defaultVariants: {
    layout: "horizontal",
    size: "sm",
  },
});

interface AuthorStatsProps
  extends React.HTMLAttributes<HTMLDivElement>,
    VariantProps<typeof authorStatsVariants> {
  author: AuthorWithStats;
  showWorksCount?: boolean;
  showFollowersCount?: boolean;
  showRating?: boolean;
  showReadsCount?: boolean;
  format?: "numbers" | "abbreviated" | "full";
}

export function AuthorStats({
  author,
  layout,
  size,
  showWorksCount = true,
  showFollowersCount = true,
  showRating = true,
  showReadsCount = true,
  format = "abbreviated",
  className,
  ...props
}: AuthorStatsProps) {
  const stats: Array<{
    icon: React.ReactNode;
    label: string;
    value: string;
    key: string;
  }> = [];

  if (showWorksCount && author.worksCount) {
    stats.push({
      icon: <BookOpen className="h-4 w-4" />,
      label: "Works",
      value: authorUtils.formatNumber(author.worksCount, format),
      key: "works",
    });
  }

  if (showFollowersCount && author.followersCount) {
    stats.push({
      icon: <Users className="h-4 w-4" />,
      label: "Followers",
      value: authorUtils.formatNumber(author.followersCount, format),
      key: "followers",
    });
  }

  if (showRating && author.averageRating) {
    stats.push({
      icon: <Star className="h-4 w-4" />,
      label: "Rating",
      value: authorUtils.formatRating(author.averageRating),
      key: "rating",
    });
  }

  if (showReadsCount && author.totalReads) {
    stats.push({
      icon: <TrendingUp className="h-4 w-4" />,
      label: "Reads",
      value: authorUtils.formatNumber(author.totalReads, format),
      key: "reads",
    });
  }

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

  return (
    <div
      className={cn(authorStatsVariants({ layout, size }), className)}
      {...props}
    >
      {stats.map(({ icon, label, value, key }) => (
        <TooltipProvider key={key}>
          <Tooltip>
            <TooltipTrigger asChild>
              <div className="flex items-center gap-1 text-muted-foreground">
                {icon}
                <span className="font-medium text-foreground">{value}</span>
                <span className="hidden sm:inline">{label}</span>
              </div>
            </TooltipTrigger>
            <TooltipContent>
              <p>
                {value} {label}
              </p>
            </TooltipContent>
          </Tooltip>
        </TooltipProvider>
      ))}
    </div>
  );
}

export type { AuthorStatsProps };
export { authorStatsVariants };
