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 ProjectCardProps = {
  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;
};

export function ProjectCard({ name, description, tags, image, color, position, href }: ProjectCardProps) {
  const card = (
    <Card className="group h-full gap-2 rounded-2xl border border-lavender/40 bg-surface pt-0 ring-0 outline-2 outline-transparent transition [--card-spacing:--spacing(5)] hover:border-lavender hover:outline-lavender max-lg:gap-3 max-lg:[--card-spacing:--spacing(3)] lg:h-[438px]">
      {/* Phone & tablet (Figma 390 / 744): 200px image, 12px padding and gaps, 16px title. */}
      <div className="relative mb-3 h-[234px] shrink-0 overflow-hidden max-lg:mb-0 max-lg:h-[200px]">
        <Image
          src={image}
          alt=""
          fill
          sizes="(min-width: 1024px) 400px, (min-width: 768px) 50vw, 100vw"
          style={{ objectPosition: position }}
          className="object-cover transition duration-500 group-hover:scale-[1.03]"
        />
        <div aria-hidden className="absolute inset-0 bg-card-fade" />
      </div>
      <CardHeader className="gap-2 max-lg:gap-3">
        <CardTitle render={<h3 dir="auto" />} className="font-display text-lg leading-6 font-bold text-white max-lg:text-base">
          {name}
        </CardTitle>
        <CardDescription className="font-display text-xs leading-5 text-subtle">{description}</CardDescription>
      </CardHeader>
      <CardContent>
        <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
  );
}
