import test from "node:test";
import assert from "node:assert/strict";

import {
  extractZillowStructuredPhotoCandidatesFromNextDataScript,
  extractZillowIdentifierHintsFromNextDataScript,
  shouldUseStructuredZillowPhotos,
} from "./zillow-photo-data.js";

test("extractZillowStructuredPhotoCandidatesFromNextDataScript reads responsivePhotos", () => {
  const scriptText = JSON.stringify({
    props: {
      pageProps: {
        componentProps: {
          gdpClientCache: JSON.stringify({
            SomeQuery: {
              property: {
                responsivePhotos: [
                  {
                    url: "https://photos.zillowstatic.com/fp/photo-one-p_d.jpg",
                    mixedSources: {
                      jpeg: [{ url: "https://photos.zillowstatic.com/fp/photo-one-cc_ft_384.jpg", width: 384 }],
                    },
                  },
                  {
                    url: "https://photos.zillowstatic.com/fp/photo-two-p_d.jpg",
                  },
                ],
              },
            },
          }),
        },
      },
    },
  });

  assert.deepEqual(extractZillowStructuredPhotoCandidatesFromNextDataScript(scriptText), [
    { url: "https://photos.zillowstatic.com/fp/photo-one-p_d.jpg" },
    { url: "https://photos.zillowstatic.com/fp/photo-two-p_d.jpg" },
  ]);
});

test("extractZillowStructuredPhotoCandidatesFromNextDataScript falls back to mixedSources", () => {
  const scriptText = JSON.stringify({
    props: {
      pageProps: {
        componentProps: {
          gdpClientCache: JSON.stringify({
            SomeQuery: {
              property: {
                responsivePhotos: [
                  {
                    mixedSources: {
                      jpeg: [
                        { url: "https://photos.zillowstatic.com/fp/photo-one-cc_ft_384.jpg", width: 384 },
                        { url: "https://photos.zillowstatic.com/fp/photo-one-cc_ft_1536.jpg", width: 1536 },
                      ],
                      webp: [{ url: "https://photos.zillowstatic.com/fp/photo-one-cc_ft_1152.webp", width: 1152 }],
                    },
                  },
                ],
              },
            },
          }),
        },
      },
    },
  });

  assert.deepEqual(extractZillowStructuredPhotoCandidatesFromNextDataScript(scriptText), [
    { url: "https://photos.zillowstatic.com/fp/photo-one-cc_ft_1536.jpg", width: 1536 },
  ]);
});

test("shouldUseStructuredZillowPhotos returns true when structured photos already match the announced count", () => {
  const candidates = Array.from({ length: 29 }, (_, index) => ({
    url: `https://photos.zillowstatic.com/fp/photo-${index + 1}-p_d.jpg`,
  }));

  assert.equal(shouldUseStructuredZillowPhotos(candidates, 29), true);
});

test("shouldUseStructuredZillowPhotos returns false when structured photos are incomplete for the announced count", () => {
  const candidates = Array.from({ length: 12 }, (_, index) => ({
    url: `https://photos.zillowstatic.com/fp/photo-${index + 1}-p_d.jpg`,
  }));

  assert.equal(shouldUseStructuredZillowPhotos(candidates, { expectedPhotoCount: 29 }), false);
});

test("shouldUseStructuredZillowPhotos returns true when meta-description count matches even without visible page count", () => {
  const candidates = Array.from({ length: 29 }, (_, index) => ({
    url: `https://photos.zillowstatic.com/fp/photo-${index + 1}-p_d.jpg`,
  }));

  assert.equal(
    shouldUseStructuredZillowPhotos(candidates, { expectedPhotoCount: null, fallbackPhotoCount: 29 }),
    true
  );
});

test("shouldUseStructuredZillowPhotos returns true for a substantial structured set when no count signal is available", () => {
  const candidates = Array.from({ length: 18 }, (_, index) => ({
    url: `https://photos.zillowstatic.com/fp/photo-${index + 1}-p_d.jpg`,
  }));

  assert.equal(
    shouldUseStructuredZillowPhotos(candidates, { expectedPhotoCount: null, fallbackPhotoCount: null }),
    true
  );
});

test("shouldUseStructuredZillowPhotos returns false for a tiny structured set when no count signal is available", () => {
  const candidates = Array.from({ length: 5 }, (_, index) => ({
    url: `https://photos.zillowstatic.com/fp/photo-${index + 1}-p_d.jpg`,
  }));

  assert.equal(
    shouldUseStructuredZillowPhotos(candidates, { expectedPhotoCount: null, fallbackPhotoCount: null }),
    false
  );
});

test("extractZillowIdentifierHintsFromNextDataScript finds parcel and APN style identifiers", () => {
  const scriptText = JSON.stringify({
    props: {
      pageProps: {
        componentProps: {
          gdpClientCache: JSON.stringify({
            SomeQuery: {
              property: {
                parcelId: "1234567890",
                apn: "APN-77-55",
              },
            },
          }),
        },
      },
    },
  });

  assert.deepEqual(extractZillowIdentifierHintsFromNextDataScript(scriptText), {
    parcelId: "1234567890",
    apn: "APN-77-55",
  });
});
