import { describe, expect, it } from "vitest";
import type { WebSite, WebPage } from "schema-dts";
import { buildWebsiteJsonLd, buildWebpageJsonLd } from "./jsonLdBuilders.js";

// ---------------------------------------------------------------------------
// buildWebsiteJsonLd
// ---------------------------------------------------------------------------

describe("buildWebsiteJsonLd", () => {
  it("returns a WebSite schema with correct @type", () => {
    const result = buildWebsiteJsonLd({
      name: "Aeroflot",
      url: "https://www.aeroflot.ru/ru/onlineboard",
      description: "Online flight board",
    });

    expect(result["@type"]).toBe("WebSite");
  });

  it("sets name, url, description", () => {
    const result = buildWebsiteJsonLd({
      name: "Aeroflot",
      url: "https://www.aeroflot.ru/ru/onlineboard",
      description: "Табло онлайн",
    });

    expect(result.name).toBe("Aeroflot");
    expect(result.url).toBe("https://www.aeroflot.ru/ru/onlineboard");
    expect(result.description).toBe("Табло онлайн");
  });

  it("omits potentialAction when searchUrl is not provided", () => {
    const result = buildWebsiteJsonLd({
      name: "Aeroflot",
      url: "https://www.aeroflot.ru/ru/onlineboard",
      description: "Online flight board",
    });

    expect((result as unknown as Record<string, unknown>).potentialAction).toBeUndefined();
  });

  it("attaches a SearchAction when searchUrl is provided", () => {
    const result = buildWebsiteJsonLd({
      name: "Aeroflot",
      url: "https://www.aeroflot.ru/ru/onlineboard",
      description: "Online flight board",
      searchUrl: "https://www.aeroflot.ru/ru/onlineboard/flight/{search_term}",
    });

    const action = (result as unknown as Record<string, unknown>).potentialAction as Record<string, unknown>;
    expect(action).toBeDefined();
    expect(action["@type"]).toBe("SearchAction");
    expect(action["target"]).toBe("https://www.aeroflot.ru/ru/onlineboard/flight/{search_term}");
    expect(action["query-input"]).toBe("required name=search_term");
  });

  it("is type-compatible with schema-dts WebSite", () => {
    const result = buildWebsiteJsonLd({
      name: "Aeroflot",
      url: "https://www.aeroflot.ru",
      description: "Aeroflot flights",
    });
    // TypeScript compile-time check
    const _typed: WebSite = result;
    expect(_typed["@type"]).toBe("WebSite");
  });
});

// ---------------------------------------------------------------------------
// buildWebpageJsonLd
// ---------------------------------------------------------------------------

describe("buildWebpageJsonLd", () => {
  it("returns a WebPage schema with correct @type", () => {
    const result = buildWebpageJsonLd({
      name: "Aeroflot Schedule",
      url: "https://www.aeroflot.ru/ru/schedule",
      description: "Schedule description",
    });

    expect(result["@type"]).toBe("WebPage");
  });

  it("sets name, url, description", () => {
    const result = buildWebpageJsonLd({
      name: "Aeroflot Schedule",
      url: "https://www.aeroflot.ru/ru/schedule",
      description: "Flight schedule",
    });

    expect(result.name).toBe("Aeroflot Schedule");
    expect(result.url).toBe("https://www.aeroflot.ru/ru/schedule");
    expect(result.description).toBe("Flight schedule");
  });

  it("omits inLanguage when not provided", () => {
    const result = buildWebpageJsonLd({
      name: "Aeroflot",
      url: "https://www.aeroflot.ru/ru/schedule",
      description: "desc",
    });

    expect(result.inLanguage).toBeUndefined();
  });

  it("sets inLanguage when provided", () => {
    const result = buildWebpageJsonLd({
      name: "Aeroflot",
      url: "https://www.aeroflot.ru/ru/schedule",
      description: "desc",
      inLanguage: "ru",
    });

    expect(result.inLanguage).toBe("ru");
  });

  it("is type-compatible with schema-dts WebPage", () => {
    const result = buildWebpageJsonLd({
      name: "Aeroflot",
      url: "https://www.aeroflot.ru",
      description: "desc",
    });
    const _typed: WebPage = result;
    expect(_typed["@type"]).toBe("WebPage");
  });
});
