import Link from "next/link";
import Image from "next/image";
import { TagList } from "@/components/shared/tag-list";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";

type ProductCardProps = {
  name: string;
  description: string;
  tags: string[];
  image: string;
  color: string;
  /** CSS object-position for the screenshot crop. */
  position: string;
  /** Details page; when set, the whole card is a link to it. */
  href?: string;
};

/**
 * Horizontal card: screenshot on a purple gradient panel + text.
 * Phone (Figma 390): a compact 350×164 card — 140px image panel, 12px title, 9px text, 8px tags.
 * data-figma-small-text: those sizes are below the 11px check on purpose (they come from the design).
 */
export function ProductCard({ name, description, tags, image, color, position, href }: ProductCardProps) {
  const card = (
    <Card data-figma-small-text className="group h-full flex-row gap-0 rounded-2xl border-2 border-lavender/50 bg-surface py-0 ring-0 transition [--card-spacing:--spacing(5)] hover:border-lavender max-md:[--card-spacing:--spacing(2.5)]">
      <div className="w-[140px] shrink-0 bg-linear-to-l from-brand-deep to-page p-2.5 md:w-[253px] rtl:bg-linear-to-r">
        <div className="relative h-[140px] overflow-hidden rounded-md md:h-[218px]">
          <Image
            src={image}
            alt=""
            fill
            sizes="(min-width: 744px) 233px, 120px"
            style={{ objectPosition: position }}
            className="object-cover transition duration-500 group-hover:scale-[1.03]"
          />
        </div>
      </div>
      <CardContent className="flex flex-1 flex-col justify-between gap-1 py-(--card-spacing) md:gap-4 max-md:[&_li>*]:px-1 max-md:[&_li>*]:py-0.5 max-md:[&_li>*]:text-[8px] max-md:[&_li>*]:leading-[14px]">
        <CardHeader className="gap-0 px-0 md:gap-2.5">
          <CardTitle render={<h3 />} className="font-display text-xs leading-6 font-bold text-white md:text-lg">
            {name}
          </CardTitle>
          <CardDescription className="font-display text-[9px] leading-[14px] text-subtle md:pt-2 md:text-xs md:leading-5">{description}</CardDescription>
        </CardHeader>
        <TagList tags={tags} color={color} />
      </CardContent>
    </Card>
  );

  return href ? (
    <Link href={href} prefetch className="block h-full rounded-2xl outline-none focus-visible:ring-3 focus-visible:ring-ring/50">
      {card}
    </Link>
  ) : (
    card
  );
}
