import test from 'node:test';
import assert from 'node:assert/strict';

import {
  SYSTEM_ADMIN_MEMBER_ID,
  SYSTEM_ADMIN_MEMBER_NAME,
  SYSTEM_ADMIN_ROLE_ID,
  ensureSystemAdminMember,
  sanitizeSystemAdminPatch,
  canDeleteSystemMember,
  resolveMemberDisplayName,
  isMemberReference,
} from './member-system';
import { ensureMemberUsernames, type Member } from './members';

const oldAdmin: Member = {
  id: SYSTEM_ADMIN_MEMBER_ID,
  name: '陈十',
  departmentId: 'dept-2',
  roleId: 'role-dev',
  phone: '13200132008',
  email: 'chenshi@company.com',
  password: 'Ftb@2024',
  createdAt: '2024-01-01',
};

test('normalizes the built-in admin account when old data named it as a regular user', () => {
  const result = ensureSystemAdminMember([oldAdmin]);

  assert.equal(result.changed, true);
  assert.equal(result.members.length, 1);
  assert.equal(result.members[0].id, SYSTEM_ADMIN_MEMBER_ID);
  assert.equal(result.members[0].name, SYSTEM_ADMIN_MEMBER_NAME);
  assert.equal(result.members[0].username, 'admin');
  assert.equal(result.members[0].departmentId, '');
  assert.equal(result.members[0].email, '');
  assert.equal(result.members[0].roleId, SYSTEM_ADMIN_ROLE_ID);
  assert.equal(result.members[0].isSystem, true);
  assert.equal(result.members[0].phone, oldAdmin.phone);
});

test('adds a built-in admin account when member data has none', () => {
  const result = ensureSystemAdminMember([]);

  assert.equal(result.changed, true);
  assert.equal(result.members.length, 1);
  assert.equal(result.members[0].id, SYSTEM_ADMIN_MEMBER_ID);
  assert.equal(result.members[0].name, SYSTEM_ADMIN_MEMBER_NAME);
  assert.equal(result.members[0].username, 'admin');
  assert.equal(result.members[0].departmentId, '');
  assert.equal(result.members[0].email, '');
  assert.equal(result.members[0].roleId, SYSTEM_ADMIN_ROLE_ID);
});

test('keeps the built-in admin username when another cached member already uses admin', () => {
  const regularMember: Member = {
    id: 'm-1',
    name: '张三',
    username: 'admin',
    departmentId: 'dept-1',
    roleId: 'role-dev',
    phone: '',
    email: '',
    password: 'Ftb@2024',
    createdAt: '2024-01-01',
  };

  const ensured = ensureSystemAdminMember([regularMember, oldAdmin]);
  const usernameEnsured = ensureMemberUsernames(ensured.members);

  assert.equal(usernameEnsured.members[0].id, SYSTEM_ADMIN_MEMBER_ID);
  assert.equal(usernameEnsured.members[0].username, 'admin');
  assert.equal(usernameEnsured.members[1].username, 'admin1');
});

test('protects the built-in admin account from identity and role edits', () => {
  const patch = sanitizeSystemAdminPatch(SYSTEM_ADMIN_MEMBER_ID, {
    name: '陈十',
    username: 'chenshi',
    roleId: 'role-dev',
    departmentId: 'dept-3',
    phone: '13900139000',
    email: 'admin@company.com',
  });

  assert.equal(patch.name, undefined);
  assert.equal(patch.username, undefined);
  assert.equal(patch.roleId, undefined);
  assert.equal(patch.departmentId, undefined);
  assert.equal(patch.email, undefined);
  assert.equal(patch.phone, '13900139000');
});

test('blocks deleting the built-in admin account', () => {
  assert.equal(canDeleteSystemMember(SYSTEM_ADMIN_MEMBER_ID), false);
  assert.equal(canDeleteSystemMember('m-9'), true);
});

test('resolves legacy Chen Shi labels to the built-in admin display name', () => {
  const result = ensureSystemAdminMember([oldAdmin]);

  assert.equal(resolveMemberDisplayName('陈十', result.members), SYSTEM_ADMIN_MEMBER_NAME);
  assert.equal(resolveMemberDisplayName(SYSTEM_ADMIN_MEMBER_ID, result.members), SYSTEM_ADMIN_MEMBER_NAME);
  assert.equal(resolveMemberDisplayName('张三', result.members), '张三');
});

test('matches legacy admin names as the built-in admin member', () => {
  const result = ensureSystemAdminMember([oldAdmin]);
  const admin = result.members[0];

  assert.equal(isMemberReference('陈十', admin), true);
  assert.equal(isMemberReference(SYSTEM_ADMIN_MEMBER_ID, admin), true);
  assert.equal(isMemberReference(SYSTEM_ADMIN_MEMBER_NAME, admin), true);
  assert.equal(isMemberReference('张三', admin), false);
});
