/**
 * Full audit (8 Nov 2026) — Events area regressions.
 */
import { describe, test } from "node:test";
import assert from "node:assert/strict";
import { EVENT_CATEGORY_KEYS, eventCategoryState, resolveEventTypeId } from "@/features/events/components/eventTypeLookup";
import { eventCardImage, eventCardLocation, eventGalleryImages } from "@/features/events/eventCardFields";
import { externalHref, withWebScheme } from "@/features/events/eventLifecycle";
import { appendEventOptionalPayload } from "@/features/events/eventOptionalPayload";
import { invalidateEventReads } from "@/features/events/eventQueryKeys";
import type { QueryClient } from "@tanstack/react-query";

describe("F-EVT-1 — category pages resolve rows that kept their legacy slug", () => {
  /* What the staging catalog really answers: the synchronizer keeps an
     existing row's slug, so two categories never carry the catalog key. */
  const choices = [
    { id: 60, slug: "expo-carnival", value_en: "Exhibitions and festivals" },
    { id: 61, slug: "camps", value_en: "Camps" },
    { id: 63, slug: "sport-activity", value_en: "Sports activities" },
    { id: 181, slug: "conferences", value_en: "Conferences" },
    { id: 182, slug: "seminars", value_en: "Seminars" },
  ];

  test("every category page finds its type", () => {
    assert.equal(resolveEventTypeId(choices, EVENT_CATEGORY_KEYS.exhibitions), 60);
    assert.equal(resolveEventTypeId(choices, EVENT_CATEGORY_KEYS.sports), 63);
    assert.equal(resolveEventTypeId(choices, EVENT_CATEGORY_KEYS.camps), 61);
    assert.equal(resolveEventTypeId(choices, EVENT_CATEGORY_KEYS.conferences), 181);
  });

  test("a freshly seeded catalog (catalog-key slugs) still resolves", () => {
    const fresh = [{ id: 1, slug: "exhibitions-festivals", value_en: "Exhibitions and festivals" }, { id: 3, slug: "sports-activities", value_en: "Sports activities" }];
    assert.equal(resolveEventTypeId(fresh, EVENT_CATEGORY_KEYS.exhibitions), 1);
    assert.equal(resolveEventTypeId(fresh, EVENT_CATEGORY_KEYS.sports), 3);
  });

  test("a missing type is still «unavailable», never the whole feed", () => {
    const id = resolveEventTypeId([{ id: 9, slug: "meetups", value_en: "Meetups" }], EVENT_CATEGORY_KEYS.camps);
    assert.equal(id, undefined);
    assert.equal(eventCategoryState(EVENT_CATEGORY_KEYS.camps, id, false, false), "unavailable");
    assert.equal(eventCategoryState("", undefined, false, false), "all");
  });
});

describe("F-EVT-4/5 — card picture and location line", () => {
  test("the card image wins over the gallery; old events fall back to the first photo", () => {
    assert.equal(eventCardImage({ cover_image: "https://cdn/cover.jpg", event_images: [{ id: 1, image: "https://cdn/a.jpg" }] }), "https://cdn/cover.jpg");
    assert.equal(eventCardImage({ cover_image: null, event_images: [{ id: 1, image: null }, { id: 2, image: "https://cdn/b.jpg" }] }), "https://cdn/b.jpg");
    assert.equal(eventCardImage({ event_images: [] }), null);
  });

  test("a link-only event has no location text (row hidden), not «Loading location...»", () => {
    assert.equal(eventCardLocation({ map_desc: null, location_en: null, location_ar: null }, "ar"), "");
    assert.equal(eventCardLocation({ map_desc: "Kuwait City", location_en: null, location_ar: null }, "en"), "Kuwait City");
    assert.equal(eventCardLocation({ map_desc: null, location_en: "Hall", location_ar: "قاعة" }, "ar"), "قاعة");
  });
});

describe("F-EVT-6 — external links carry a scheme", () => {
  test("a typed www link is sent with https so Laravel's url rule accepts it", () => {
    assert.equal(withWebScheme(" www.example.com/register "), "https://www.example.com/register");
    assert.equal(withWebScheme("http://example.com"), "http://example.com");
    assert.equal(withWebScheme("   "), "");
    const data = new FormData();
    appendEventOptionalPayload(data, { participation_type_value_en: "Paid Event", registration_link: "example.org/r", gender: "", sponsors: [] });
    assert.equal(data.get("registration_link"), "https://example.org/r");
  });

  test("only http(s) links are opened; a schemeless legacy link is not treated as a Fursa path", () => {
    assert.equal(externalHref("example.org/r"), "https://example.org/r");
    assert.equal(externalHref("javascript:alert(1)"), null);
    assert.equal(externalHref(null), null);
  });
});

describe("F-EVT-9 — event writes refresh every cached event read", () => {
  test("detail, lists, profile tabs and the homepage carousel are invalidated", async () => {
    const keys: unknown[] = [];
    const client = { invalidateQueries: async ({ queryKey }: { queryKey: unknown[] }) => { keys.push(queryKey); } } as unknown as QueryClient;
    await invalidateEventReads(client);
    assert.deepEqual(keys, [["event-details"], ["events-list"], ["profile-events"], ["events-homepage"]]);
  });
});


describe("event card artwork is separate from the documentation gallery", () => {
  test("a dedicated cover is excluded while actual gallery images retain their order", () => {
    const event = { cover_image: "/card.jpg", event_images: [
      { id: 1, image: "/card.jpg" }, { id: 2, image: "/activity-a.jpg" }, { id: 3, image: "/activity-b.jpg" },
    ] };
    assert.equal(eventCardImage(event), "/card.jpg");
    assert.deepEqual(eventGalleryImages(event).map(image => image.id), [2, 3]);
  });
  test("legacy first-photo artwork does not become an initial gallery photo", () => {
    assert.deepEqual(eventGalleryImages({ event_images: [{ id: 1, image: "/card.jpg" }] }), []);
    assert.deepEqual(eventGalleryImages({ cover_image: "/card.jpg", event_images: [] }), []);
  });
  test("a dedicated card does not remove unrelated gallery images", () => {
    assert.deepEqual(eventGalleryImages({ cover_image: "/card.jpg", event_images: [{ id: 2, image: "/activity.jpg" }] }).map(image => image.id), [2]);
  });
});
