/**
 * The directory tile shows an organization / team by its registered name
 * (PDF p.9, BE-294), an individual by display name or nickname only (BE-169).
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { directoryDisplayName } from "@/features/profile/profileDirectoryName";
import type {
  OrganizationDirectoryItem,
  VolunteerDirectoryItem,
} from "@/features/profile/profileDirectoryRecord";

const org = (extra: Partial<OrganizationDirectoryItem> = {}): OrganizationDirectoryItem => ({
  id: 1,
  nickname: "jx",
  is_public: true,
  user_details: {
    id: 9,
    first_name: "Ali",
    last_name: "Hassan",
    profile_pic: null,
    gender_display: null,
    user_type: "organization",
  },
  ...extra,
});

const volunteer = (extra: Partial<VolunteerDirectoryItem> = {}): VolunteerDirectoryItem => ({
  id: 2,
  nickname: "nick",
  is_public: true,
  user_details: { id: 8, profile_pic: null, gender_display: null, user_type: "volunteer" },
  ...extra,
});

describe("directoryDisplayName", () => {
  test("an organization shows its registered name, not the seeded username", () => {
    assert.equal(
      directoryDisplayName(org({ company_name: "جمعية X", display_name: "جمعية X" }), false),
      "جمعية X",
    );
  });

  test("company_name is used when display_name is missing", () => {
    assert.equal(directoryDisplayName(org({ company_name: "Org X" }), false), "Org X");
  });

  test("an older payload without either falls back to nickname, then the account name", () => {
    assert.equal(directoryDisplayName(org(), false), "jx");
    assert.equal(directoryDisplayName(org({ nickname: null }), false), "Ali Hassan");
    assert.equal(
      directoryDisplayName(org({ nickname: null, user_details: { ...org().user_details, first_name: null, last_name: null } }), false),
      "—",
    );
  });

  test("a blank company name does not hide the fallback", () => {
    assert.equal(directoryDisplayName(org({ company_name: "  ", display_name: "" }), false), "jx");
  });

  test("an individual shows display_name or nickname and never a real name", () => {
    assert.equal(directoryDisplayName(volunteer({ display_name: "Shown Name" }), true), "Shown Name");
    assert.equal(directoryDisplayName(volunteer(), true), "nick");
    assert.equal(directoryDisplayName(volunteer({ nickname: null }), true), "—");
  });
});
