/**
 * An organization / team profile is headed by its registered name — the same
 * name the directory tile shows (BE-294) — and never by the username it was
 * seeded from at sign-up.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import {
  fromOwnOrganizationProfile,
  fromOwnTeamProfile,
  fromPublicOrganizationProfile,
  fromPublicTeamProfile,
} from "@/features/profile/components/individual/individualProfile";
import type { PublicProfileResponse } from "@/features/profile/components/commonProfileTypes";

const publicResponse = (profile: Record<string, unknown>) =>
  ({ id: 5, user_type: "organization", profile_data: { id: 5, ...profile } }) as unknown as PublicProfileResponse;

describe("organization and team profile heading", () => {
  test("public profile: the registered name wins over the username", () => {
    const response = publicResponse({ nickname: "alnoor_kw", company_name: "شركة النور" });
    assert.equal(fromPublicTeamProfile(response).name, "شركة النور");
    assert.equal(fromPublicOrganizationProfile(response).name, "شركة النور");
    assert.equal(fromPublicTeamProfile(response).nickname, "alnoor_kw", "the @handle keeps the username");
  });

  test("own profile: the registered name wins over the username", () => {
    const profile = { nickname: "alnoor_kw", company_name: "شركة النور" };
    assert.equal(fromOwnTeamProfile(profile, { id: 1, full_name: "Ali" }).name, "شركة النور");
    assert.equal(fromOwnOrganizationProfile(profile, { id: 1 }).name, "شركة النور");
  });

  test("an older payload without a registered name falls back to the nickname", () => {
    assert.equal(fromPublicTeamProfile(publicResponse({ nickname: "alnoor_kw" })).name, "alnoor_kw");
    assert.equal(fromOwnTeamProfile({ nickname: "alnoor_kw" }, { id: 1 }).name, "alnoor_kw");
  });
});
