import { renderToStaticMarkup } from "react-dom/server";
import { describe, expect, it } from "vitest";

import { PrivateAlbum } from "@/data/dto/private-room";

import { PrivateAlbumCard } from "../private-album-card";

const COVER_URL = "/images/private-room/banner.png";

function makeAlbum(
  overrides: Partial<Parameters<typeof PrivateAlbum.from>[0]> = {},
): PrivateAlbum {
  return PrivateAlbum.from({
    albumId: "album-1",
    title: "Private morning",
    content: "A quiet morning by the water.",
    previewText: "Only for you.",
    imageCount: 8,
    images: [],
    locked: false,
    unlocked: true,
    unlockCost: 320,
    publishedAt: "2026-07-01T08:11:35+00:00",
    lockDetail: { locked: false },
    ...overrides,
  });
}

function renderCard(album: PrivateAlbum): string {
  return renderToStaticMarkup(
    <PrivateAlbumCard
      album={album}
      displayName="Elio Silvestri"
      avatarUrl="/images/chat/pic-chat-elio.png"
      index={0}
      isUnlocking={false}
      onOpenGallery={() => undefined}
      onUnlock={() => undefined}
    />,
  );
}

describe("PrivateAlbumCard", () => {
  it("renders only the first unlocked image and its photo count", () => {
    const html = renderCard(
      makeAlbum({
        imageCount: 8,
        images: [
          { url: COVER_URL, locked: false, index: 0 },
          {
            url: "/images/splash/pic-bg-home.png",
            locked: false,
            index: 1,
          },
        ],
      }),
    );

    expect(html).toContain("banner.png");
    expect(html).not.toContain("pic-bg-home.png");
    expect(html).toContain("8 photos");
    expect(html).toContain("A quiet morning by the water.");
    expect(html).toContain('aria-label="Open 8 private album photos"');
    expect(html).toContain(
      'data-analytics-key="private_album.open_gallery"',
    );
  });

  it("uses the backend first image as the locked cover", () => {
    const html = renderCard(
      makeAlbum({
        content: null,
        locked: true,
        unlocked: false,
        lockDetail: { locked: true },
        images: [{ url: COVER_URL, locked: true, index: 0 }],
      }),
    );

    expect(html).toContain("banner.png");
    expect(html).toContain(
      'aria-label="Unlock 8 private room photos from Elio Silvestri"',
    );
    expect(html).toContain("Only for you.");
    expect(html).toContain("320 credits");
    expect(html).toContain('data-analytics-key="private_album.unlock"');
  });

  it("renders an empty cover when an unlocked album has no image", () => {
    const html = renderCard(makeAlbum());

    expect(html).toContain('aria-label="No private album photo available"');
    expect(html).toContain("No photo available");
  });
});
