/**
 * AuthorCard - Main author display card component
 * Refactored to use composable pieces for better maintainability
 */

import type React from "react";
import { cva, type VariantProps } from "class-variance-authority";
import { Card, CardContent } from "@/components/ui/card";
import { cn } from "@/lib/utils";
import type { AuthorWithStats } from "./types";
import {
  AuthorAvatar,
  AuthorName,
  AuthorBio,
  AuthorMeta,
  AuthorStats,
  AuthorGenres,
} from "./composables/";

const authorCardVariants = cva(
  "group bg-card border border-border hover:shadow-md transition-all duration-200",
  {
    variants: {
      variant: {
        default: "bg-background hover:bg-accent/50",
        featured: "border-primary/20 bg-primary/5 hover:bg-primary/10",
        compact: "bg-background",
        minimal: "border-none shadow-none hover:shadow-sm bg-transparent",
      },
      orientation: {
        horizontal: "",
        vertical: "",
      },
      size: {
        sm: "",
        md: "",
        lg: "",
      },
    },
    defaultVariants: {
      variant: "default",
      orientation: "horizontal",
      size: "md",
    },
  }
);

interface AuthorCardProps
  extends React.HTMLAttributes<HTMLDivElement>,
    VariantProps<typeof authorCardVariants> {
  author: AuthorWithStats;
  showBio?: boolean;
  showGenres?: boolean;
  showStats?: boolean;
  showMeta?: boolean;
  interactive?: boolean;
  href?: string;
}

export function AuthorCard({
  author,
  variant,
  orientation,
  size,
  showBio = true,
  showGenres = true,
  showStats = true,
  showMeta = true,
  interactive = true,
  href,
  className,
  ...props
}: AuthorCardProps) {
  const isVertical = orientation === "vertical";
  const isSmall = size === "sm";
  const isLarge = size === "lg";

  const CardWrapper = ({ children }: { children: React.ReactNode }) => {
    if (href && interactive) {
      return (
        <a href={href} className="block">
          <Card
            className={cn(
              authorCardVariants({ variant, orientation, size }),
              className
            )}
          >
            {children}
          </Card>
        </a>
      );
    }

    return (
      <Card
        className={cn(
          authorCardVariants({ variant, orientation, size }),
          className
        )}
        {...props}
      >
        {children}
      </Card>
    );
  };

  return (
    <CardWrapper>
      <CardContent
        className={cn(
          "p-6",
          isSmall && "p-4",
          isLarge && "p-8",
          isVertical ? "text-center space-y-4" : "flex gap-4"
        )}
      >
        {/* Avatar */}
        <div className={isVertical ? "flex justify-center" : "flex-shrink-0"}>
          <AuthorAvatar
            author={author}
            size={isSmall ? "md" : isLarge ? "xl" : "lg"}
          />
        </div>

        {/* Content */}
        <div
          className={cn(
            "flex-1 min-w-0",
            isVertical ? "space-y-3" : "space-y-2"
          )}
        >
          {/* Name */}
          <AuthorName
            author={author}
            size={isSmall ? "md" : isLarge ? "xl" : "lg"}
            weight="semibold"
            className="group-hover:text-primary transition-colors"
          />

          {/* Meta information */}
          {showMeta && (
            <AuthorMeta author={author} size={isSmall ? "xs" : "sm"} />
          )}

          {/* Bio */}
          {showBio && (
            <AuthorBio
              author={author}
              size={isSmall ? "xs" : "sm"}
              lines={isVertical ? 3 : 2}
              maxLength={isSmall ? 100 : 150}
            />
          )}

          {/* Genres */}
          {showGenres && (
            <AuthorGenres
              author={author}
              maxGenres={isSmall ? 2 : 3}
              size={isSmall ? "sm" : "default"}
              variant="outline"
            />
          )}

          {/* Stats */}
          {showStats && (
            <AuthorStats
              author={author}
              layout={isVertical ? "grid" : "horizontal"}
              size={isSmall ? "sm" : "md"}
              format="abbreviated"
            />
          )}
        </div>
      </CardContent>
    </CardWrapper>
  );
}

export type { AuthorCardProps };
export { authorCardVariants };
