import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { appendEventOptionalPayload } from "@/features/events/eventOptionalPayload";
import { eventExternalRegistrationLink, isEventDeletionRequestPending } from "@/features/events/eventLifecycle";
import { offeredInterestIds } from "@/features/events/components/eventFormInitialValues";
import { learnServeRejection, isLearnServeDeletionPending } from "@/features/opportunities/components/learnServeDetailsViewState";

// ─── Scope A (development) ──────────────────────────────────────────────────

describe("S-DEV-1: the owner of a rejected development opportunity sees the rejection", () => {
  test("creator + rejected → notice with the admin's reason", () => {
    assert.deepEqual(learnServeRejection({ approval_status: "rejected", rejected_reason: " صور غير مناسبة " }, true), { reason: "صور غير مناسبة" });
  });
  test("a missing or blank reason still shows the heading", () => {
    assert.deepEqual(learnServeRejection({ approval_status: "rejected", rejected_reason: null }, true), { reason: null });
    assert.deepEqual(learnServeRejection({ approval_status: "rejected", rejected_reason: "  " }, true), { reason: null });
  });
  test("never for a viewer, nor for pending/approved courses", () => {
    assert.equal(learnServeRejection({ approval_status: "rejected", rejected_reason: "x" }, false), null);
    assert.equal(learnServeRejection({ approval_status: "pending" }, true), null);
    assert.equal(learnServeRejection({ approval_status: "approved" }, true), null);
    assert.equal(learnServeRejection(undefined, true), null);
  });
  test("the detail column renders RejectionNotice from the helper", () => {
    const src = readFileSync("src/features/opportunities/components/LearnServeDetailsColumn.tsx", "utf8");
    assert.match(src, /learnServeRejection\(opportunityData, isCreator\)/);
    assert.match(src, /<RejectionNotice reason=\{rejection\.reason\} \/>/);
  });
});

describe("S-DEV-2: a pending development deletion request is stated, not hidden", () => {
  test("pending only on the server's deletion_status", () => {
    assert.equal(isLearnServeDeletionPending({ deletion_status: "pending" }), true);
    for (const status of ["not_requested", "rejected", "approved", null, undefined]) {
      assert.equal(isLearnServeDeletionPending({ deletion_status: status }), false);
    }
    assert.equal(isLearnServeDeletionPending(undefined), false);
  });
  test("DeletionReviewAction retains the pending notice without offering a new request", () => {
    const src = readFileSync("src/features/opportunities/components/DeletionReviewAction.tsx", "utf8");
    assert.ok(src.includes("if (pending)"));
    assert.ok(!src.includes("COMMON.REQUEST_DELETION"));
    assert.match(src, /COMMON\.DELETION_ALREADY_PENDING/);
    const modals = readFileSync("src/features/opportunities/components/LearnServeManageModals.tsx", "utf8");
    assert.match(modals, /pending=\{isLearnServeDeletionPending\(data\.opportunityData\)\}/);
  });
});

// ─── Scope B (events) — merged from the events review ───────────────────────

describe("S-DEV-E1: event deletion request already pending (server flags)", () => {
  test("pending only when the server refuses both the direct delete and a new request", () => {
    assert.equal(isEventDeletionRequestPending({ can_delete: false, can_request_deletion: false }), true);
  });
  test("before the 24 h cutoff the direct delete is offered: not pending, even if the page outlives the cutoff", () => {
    // OpportunityCapabilities: DELETION_DIRECT_AVAILABLE → can_request_deletion=false, can_delete=true.
    assert.equal(isEventDeletionRequestPending({ can_delete: true, can_request_deletion: false }), false);
  });
  test("after the cutoff with no request: the review request is offered", () => {
    assert.equal(isEventDeletionRequestPending({ can_delete: false, can_request_deletion: true }), false);
  });
  test("older payloads without flags never read as pending", () => {
    assert.equal(isEventDeletionRequestPending({}), false);
  });
  test("EventDeletionAction disables delete and hides a second request while pending", () => {
    const src = readFileSync("src/features/events/components/EventDeletionAction.tsx", "utf8");
    assert.match(src, /isEventDeletionRequestPending\(event\)/);
    assert.match(src, /const canDelete = !pending && canEditOrDelete/);
    assert.match(src, /COMMON\.DELETION_ALREADY_PENDING/);
  });
});

describe("Events contract pins (PDF p.22 / p.13)", () => {
  test("a Free Event sends an empty registration link so the server clears it", () => {
    const data = new FormData();
    appendEventOptionalPayload(data, { participation_type_value_en: "Free Event", registration_link: "https://x.test", gender: "", sponsors: [] });
    assert.equal(data.get("registration_link"), "");
    assert.equal(data.get("sponsor_organization_ids"), "none");
  });
  test("paid / free-with-registration keep the external link; Free Event has none", () => {
    assert.equal(eventExternalRegistrationLink({ participation_type_display: { value_en: "Paid Event" }, registration_link: " https://reg.test " }), "https://reg.test");
    assert.equal(eventExternalRegistrationLink({ participation_type_display: { value_en: "Free Event" }, registration_link: "https://reg.test" }), null);
  });
});

describe("S-DEV-E2: an event repost never carries the hidden held «عام» tag (A13)", () => {
  const offered = [{ id: "11" }, { id: "12" }];
  test("tags the picker no longer offers are dropped from the repost", () => {
    assert.deepEqual(offeredInterestIds(["11", "99"], offered), ["11"]);
  });
  test("a source holding only «عام» leaves nothing, so the required rule asks for a real tag", () => {
    assert.deepEqual(offeredInterestIds(["99"], offered), []);
  });
  test("while the options are still loading the ids are kept untouched", () => {
    assert.deepEqual(offeredInterestIds(["99"], []), ["99"]);
  });
  test("useEventForm filters the repost payload and validates against offered tags", () => {
    const src = readFileSync("src/features/events/components/useEventForm.ts", "utf8");
    assert.match(src, /id && isRepublish \? offeredInterestIds\(values\._interests, tagOptions\)/);
    assert.match(src, /"offered-interest"/);
  });
});
