import { describe, expect, it } from "vitest";
import { buildFlightsMapSeo } from "./seo.js";
import { buildFlightsMapJsonLd } from "./json-ld.js";

/** Stub t() that returns the key for assertion. */
function stubT(key: string): string {
  return key;
}

const CANONICAL = "https://www.aeroflot.ru";

// ---------------------------------------------------------------------------
// buildFlightsMapSeo
// ---------------------------------------------------------------------------

describe("buildFlightsMapSeo", () => {
  it("uses FLIGHTS-MAP.MAIN translation keys", () => {
    const result = buildFlightsMapSeo(stubT, "ru", CANONICAL);

    expect(result.title).toBe("SEO.FLIGHTS-MAP.MAIN.TITLE");
    expect(result.description).toBe("SEO.FLIGHTS-MAP.MAIN.DESCRIPTION");
  });

  it("sets canonical to /{locale}/flights-map", () => {
    const result = buildFlightsMapSeo(stubT, "en", CANONICAL);

    expect(result.canonical).toBe("https://www.aeroflot.ru/en/flights-map");
  });

  it("includes hreflang with 10 entries", () => {
    const result = buildFlightsMapSeo(stubT, "ru", CANONICAL);

    expect(result.hreflang).toHaveLength(10);
  });

  it("sets og tags", () => {
    const result = buildFlightsMapSeo(stubT, "ru", CANONICAL);

    expect(result.og.title).toBe(result.title);
    expect(result.og.type).toBe("website");
    expect(result.og.locale).toBe("ru");
    expect(result.og.siteName).toBe("Aeroflot");
    expect(result.og.url).toBe("https://www.aeroflot.ru/ru/flights-map");
  });

  it("sets twitter card to summary", () => {
    const result = buildFlightsMapSeo(stubT, "ru", CANONICAL);

    expect(result.twitter).toBeDefined();
    expect(result.twitter!.card).toBe("summary");
  });

  it("4.1.3-R21: Flight-Map uses 'Карта полетов' for all map pages per TZ Table 6", () => {
    const mockT = (key: string): string => {
      if (key === "SEO.FLIGHTS-MAP.MAIN.TITLE") return "Карта полетов";
      if (key === "SEO.FLIGHTS-MAP.MAIN.DESCRIPTION")
        return "Карта полетов авиакомпании 'Аэрофлот'. Информация о направлениях рейсов.";
      return key;
    };

    const seo = buildFlightsMapSeo(mockT, "ru", CANONICAL);

    expect(seo.title).toBe("Карта полетов");
  });
});

// ---------------------------------------------------------------------------
// buildFlightsMapJsonLd
// ---------------------------------------------------------------------------

describe("buildFlightsMapJsonLd", () => {
  it("returns a WebPage schema", () => {
    const result = buildFlightsMapJsonLd("ru", CANONICAL);

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

  it("sets the correct URL with locale", () => {
    const result = buildFlightsMapJsonLd("en", CANONICAL);

    expect(result.url).toBe("https://www.aeroflot.ru/en/flights-map");
  });

  it("sets inLanguage to the locale", () => {
    const result = buildFlightsMapJsonLd("ja", CANONICAL);

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

  it("includes isPartOf WebSite reference", () => {
    const result = buildFlightsMapJsonLd("ru", CANONICAL);

    expect(result.isPartOf).toBeDefined();
    const site = result.isPartOf as { "@type": string; name: string; url: string };
    expect(site["@type"]).toBe("WebSite");
    expect(site.name).toBe("Aeroflot");
    expect(site.url).toBe(CANONICAL);
  });

  it("sets name with site name prefix", () => {
    const result = buildFlightsMapJsonLd("ru", CANONICAL);

    expect(result.name).toContain("Aeroflot");
    expect(result.name).toContain("Flight Map");
  });
});

// ---------------------------------------------------------------------------
// TZ §4.1.19 — OpenGraph completeness assertions (Flights Map)
// ---------------------------------------------------------------------------

describe("4.1.19-R: OpenGraph completeness (Flights Map)", () => {
  const ORIGIN = "https://flights.aeroflot.ru";

  it("4.1.19-R: Flights Map page has all required OG tags", () => {
    const seo = buildFlightsMapSeo(stubT, "ru", ORIGIN);

    expect(seo.og.title).toBeTruthy();
    expect(seo.og.description).toBeTruthy();
    expect(seo.og.url).toMatch(/^https:\/\//);
    expect(seo.og.image).toMatch(/^https:\/\//);
    expect(seo.og.type).toBe("website");
    expect(seo.og.locale).toBe("ru");
    expect(seo.og.siteName).toBe("Aeroflot");
  });
});

// ---------------------------------------------------------------------------
// TZ §4.1.20 — Robots, canonical, hreflang assertions (Flights Map)
// ---------------------------------------------------------------------------

describe("4.1.20-R: canonical + hreflang (Flights Map)", () => {
  const ORIGIN = "https://flights.aeroflot.ru";

  it("4.1.20-R: Flights Map canonical has no query params", () => {
    const seo = buildFlightsMapSeo(stubT, "ru", ORIGIN);

    expect(seo.canonical).not.toContain("?");
    expect(seo.canonical).toBe(`${ORIGIN}/ru/flights-map`);
  });

  it("4.1.20-R: hreflang has all 9 languages + x-default", () => {
    const seo = buildFlightsMapSeo(stubT, "ru", ORIGIN);

    expect(seo.hreflang.length).toBeGreaterThanOrEqual(9);
    expect(seo.hreflang.some((h) => h.lang === "x-default")).toBe(true);
  });

  it("4.1.20-R: hreflang covers all 9 supported languages", () => {
    const seo = buildFlightsMapSeo(stubT, "ru", ORIGIN);
    const langs = seo.hreflang.map((h) => h.lang);

    expect(langs).toContain("ru");
    expect(langs).toContain("en");
    expect(langs).toContain("es");
    expect(langs).toContain("fr");
    expect(langs).toContain("it");
    expect(langs).toContain("ja");
    expect(langs).toContain("ko");
    expect(langs).toContain("zh");
    expect(langs).toContain("de");
  });

  it("4.1.20-R: hreflang hrefs are absolute URLs", () => {
    const seo = buildFlightsMapSeo(stubT, "ru", ORIGIN);

    for (const entry of seo.hreflang) {
      expect(entry.href).toMatch(/^https:\/\//);
    }
  });

  it("4.1.20-R: noindex is not set on the Flights Map page (indexable)", () => {
    const seo = buildFlightsMapSeo(stubT, "ru", ORIGIN);

    expect(seo.noindex).toBeFalsy();
  });
});
