import Image from "next/image";
import { cn } from "cn";
import type { Mockup } from "@/content/catalog";

const cardClass = "relative w-full overflow-hidden rounded-3xl border border-white/50";

/** One phone in the grid: screenshot, then the frame, then the camera island (Figma "Halaqh" / "MEDF" / "Comparison"). */
function Phone({ src, alt }: { src: string; alt: string }) {
  return (
    <div className="relative aspect-[300/614]">
      <div className="absolute inset-x-[4.3%] inset-y-[1.1%] overflow-hidden rounded-[11px]">
        <Image src={src} alt={alt} fill sizes="100px" className="object-cover object-top" />
      </div>
      <Image src="/images/details/phone-frame.png" alt="" fill sizes="100px" className="object-cover" />
      <div aria-hidden className="absolute inset-x-0 top-[2%] flex justify-center">
        <span className="relative h-[4.4px] w-[16%] rounded-full bg-black">
          <Image src="/images/details/phone-lens.svg" alt="" width={3} height={3} className="absolute end-[7%] top-1/2 size-[3px] -translate-y-1/2" />
        </span>
      </div>
    </div>
  );
}

/**
 * Device card on a details page (Figma: 320 wide, 24px radius, white/50 border).
 * One screenshot: 384px tall on desktop, 448px on tablets, 320:384 on phones (Figma "Motakamel" frames).
 * Phone grids keep their own height and stay 320px wide on every screen.
 */
export function DetailsMockup({ mockup, alt, className }: { mockup: Mockup; alt: string; className?: string }) {
  if (mockup.kind === "screen") {
    return (
      <div
        className={cn(
          cardClass,
          "max-md:aspect-[320/384] md:max-lg:h-[448px] lg:w-80",
          mockup.tall ? "lg:h-[408px]" : "lg:h-96",
          className,
        )}
      >
        {mockup.background && <Image src={mockup.background} alt="" fill sizes="(min-width: 1024px) 320px, 100vw" className="object-cover" />}
        <Image
          src={mockup.screenshot}
          alt={alt}
          fill
          sizes="(min-width: 1024px) 320px, 100vw"
          style={{ objectPosition: mockup.position ?? "top" }}
          className={mockup.fit === "fill" ? "object-fill" : "object-cover"}
        />
      </div>
    );
  }

  const framed = mockup.kind === "framedPhones";
  return (
    <div className={cn(cardClass, "grid max-w-80 grid-cols-3", framed ? "p-2.5" : "gap-2.5 px-[11px] py-2", className)}>
      {mockup.screens.map((src, i) =>
        framed ? (
          <div key={src} className="relative aspect-[100/194.9]">
            <Image src={src} alt={i === 0 ? alt : ""} fill sizes="100px" className="object-cover" />
          </div>
        ) : (
          <Phone key={src} src={src} alt={i === 0 ? alt : ""} />
        ),
      )}
    </div>
  );
}
