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

/**
 * BlockQuote component for displaying quoted content
 *
 * @example
 * ```tsx
 * <BlockQuote>
 *   The greatest glory in living lies not in never falling, but in rising every time we fall.
 * </BlockQuote>
 *
 * <BlockQuote variant="literary" citation="Alexander Pushkin">
 *   I loved you; even now I may confess, some embers of my love their fire retain...
 * </BlockQuote>
 * ```
 */

const blockquoteVariants = cva("mt-6 border-l-2 pl-6 italic", {
	variants: {
		variant: {
			default: "border-muted text-muted-foreground",
			primary: "border-primary text-primary/80",
			literary: "border-russet/40 text-foreground font-serif",
			emphasis: "border-l-4 font-medium",
			focused: "border-l-4 border-primary bg-primary/5 py-2 rounded-r-sm",
		},
	},
	defaultVariants: {
		variant: "default",
	},
});

export interface BlockQuoteProps
	extends React.HTMLAttributes<HTMLQuoteElement>,
		VariantProps<typeof blockquoteVariants> {
	/**
	 * Optional citation or attribution
	 */
	citation?: string;
	/**
	 * Optional citation link
	 */
	citationHref?: string;
}

const BlockQuote = forwardRef<HTMLQuoteElement, BlockQuoteProps>(
	({ className, variant, citation, citationHref, children, ...props }, ref) => {
		return (
			<figure className="my-6">
				<blockquote
					className={cn(blockquoteVariants({ variant, className }))}
					ref={ref}
					{...props}
				>
					{children}
				</blockquote>
				{citation && (
					<figcaption className="mt-2 text-sm text-muted-foreground">
						{citationHref ? (
							<cite>
								<a
									href={citationHref}
									className="font-medium text-foreground hover:underline"
									target="_blank"
									rel="noopener noreferrer"
								>
									{citation}
								</a>
							</cite>
						) : (
							<cite>— {citation}</cite>
						)}
					</figcaption>
				)}
			</figure>
		);
	},
);

BlockQuote.displayName = "BlockQuote";

export { BlockQuote, blockquoteVariants };
