/**
 * Full audit (8 Nov 2026) — Community and notifications regressions.
 *
 *  - F-COM-1/2: «للتواصل» only on idea posts, never on the reader's own idea
 *    (`contact-creator` answers 422 there).
 *  - F-COM-5: community image rules (PDF p.26/p.42) incl. the 10-file cap.
 *  - F-COM-6: `/replies/` always paginates; every page must be merged.
 *  - F-COM-8: notification dates follow the reader's language.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { canContactPostAuthor } from "@/features/community/communityContact";
import { postImagesError, COMMUNITY_IMAGE_MAX_BYTES } from "@/features/community/communityUploadRules";
import { formatNotificationDate } from "@/features/notification/notificationDate";
import apiClient from "@/lib/api/client";
import { getCommunityReplies } from "@/features/community/services/communityApi";

const author = { id: 7, full_name: "A", profile_pic: null, gender_display: null, is_public: true, user_type: "individual" };

describe("canContactPostAuthor", () => {
  test("idea post by someone else shows the button", () => {
    assert.equal(canContactPostAuthor({ proposing_idea: true, is_creator: false, user: author }, 9), true);
  });
  test("a share (non-idea) post never shows it", () => {
    assert.equal(canContactPostAuthor({ proposing_idea: false, is_creator: false, user: author }, 9), false);
  });
  test("own idea is hidden by the flag or, without it, by the author id", () => {
    assert.equal(canContactPostAuthor({ proposing_idea: true, is_creator: true, user: author }, 9), false);
    assert.equal(canContactPostAuthor({ proposing_idea: true, is_creator: false, user: author }, "7"), false);
  });
  test("a guest still sees it (the click asks them to sign in)", () => {
    assert.equal(canContactPostAuthor({ proposing_idea: true, is_creator: false, user: author }, undefined), true);
  });
});

const file = (type: string, size = 1000) => ({ type, size }) as File;

describe("postImagesError", () => {
  test("jpg/png/webp within 10 MB pass, nothing picked passes", () => {
    assert.equal(postImagesError([file("image/jpeg"), file("image/png"), file("image/webp")]), null);
    assert.equal(postImagesError([]), null);
    assert.equal(postImagesError(undefined), null);
  });
  test("other types and oversized files are refused", () => {
    assert.equal(postImagesError([file("image/gif")]), "COMMON.REVIEW_IMAGE_LIMIT");
    assert.equal(postImagesError([file("image/png", COMMUNITY_IMAGE_MAX_BYTES + 1)]), "COMMON.REVIEW_IMAGE_LIMIT");
  });
  test("more than 10 new files in one request is refused", () => {
    assert.equal(postImagesError(Array.from({ length: 11 }, () => file("image/png"))), "COMMON.POST_IMAGES_MAX");
    assert.equal(postImagesError(Array.from({ length: 10 }, () => file("image/png"))), null);
  });
});

describe("formatNotificationDate", () => {
  test("Arabic month names with Western digits, English otherwise", () => {
    const ar = formatNotificationDate("2026-11-08T09:00:00Z", "ar");
    assert.match(ar, /نوفمبر/);
    assert.match(ar, /2026/);
    assert.match(formatNotificationDate("2026-11-08T09:00:00Z", "en"), /8 November 2026/);
  });
  test("an unparsable timestamp renders empty", () => {
    assert.equal(formatNotificationDate("not a date", "en"), "");
  });
});

describe("getCommunityReplies", () => {
  test("reads every page and merges the rows", async () => {
    const pages: number[] = [];
    const original = apiClient.defaults.adapter;
    apiClient.defaults.adapter = async (config) => {
      const page = Number(config.params?.page);
      pages.push(page);
      return {
        data: { key: "success", msg: "", code: 200, response_status: { error: false, validation_errors: [] }, data: [{ id: page }], meta: { pagination: { page, limit: 100, total: 3, total_pages: 3 } } },
        status: 200, statusText: "OK", headers: {}, config,
      };
    };
    try {
      const result = await getCommunityReplies("5");
      assert.deepEqual(pages, [1, 2, 3]);
      assert.deepEqual(result.data.map((row) => row.id), [1, 2, 3]);
    } finally {
      apiClient.defaults.adapter = original;
    }
  });
});
