import type { Member } from './members';

export const SYSTEM_ADMIN_MEMBER_ID = 'm-8';
export const SYSTEM_ADMIN_MEMBER_NAME = '超级管理员';
export const SYSTEM_ADMIN_ROLE_ID = 'role-admin';
export const SYSTEM_ADMIN_DEPARTMENT_ID = '';
const LEGACY_SYSTEM_ADMIN_NAMES = ['陈十'];

export const SYSTEM_ADMIN_MEMBER: Member = {
  id: SYSTEM_ADMIN_MEMBER_ID,
  name: SYSTEM_ADMIN_MEMBER_NAME,
  username: 'admin',
  departmentId: SYSTEM_ADMIN_DEPARTMENT_ID,
  roleId: SYSTEM_ADMIN_ROLE_ID,
  phone: '13200132008',
  email: '',
  password: 'Ftb@2024',
  createdAt: '2024-01-01',
  isSystem: true,
};

function normalizeSystemAdminMember(member: Partial<Member>): Member {
  return {
    ...SYSTEM_ADMIN_MEMBER,
    ...member,
    id: SYSTEM_ADMIN_MEMBER_ID,
    name: SYSTEM_ADMIN_MEMBER_NAME,
    username: SYSTEM_ADMIN_MEMBER.username,
    departmentId: SYSTEM_ADMIN_DEPARTMENT_ID,
    roleId: SYSTEM_ADMIN_ROLE_ID,
    phone: member.phone || SYSTEM_ADMIN_MEMBER.phone,
    email: SYSTEM_ADMIN_MEMBER.email,
    password: member.password || SYSTEM_ADMIN_MEMBER.password,
    createdAt: member.createdAt || SYSTEM_ADMIN_MEMBER.createdAt,
    isSystem: true,
  };
}

export function ensureSystemAdminMember(members: Member[]): { members: Member[]; changed: boolean } {
  const index = members.findIndex((m) => m.id === SYSTEM_ADMIN_MEMBER_ID);
  if (index === -1) {
    return { members: [SYSTEM_ADMIN_MEMBER, ...members], changed: true };
  }

  const current = members[index];
  const normalized = normalizeSystemAdminMember(current);
  const changed =
    index !== 0 ||
    current.name !== normalized.name ||
    current.username !== normalized.username ||
    current.departmentId !== normalized.departmentId ||
    current.roleId !== normalized.roleId ||
    current.phone !== normalized.phone ||
    current.email !== normalized.email ||
    current.password !== normalized.password ||
    current.createdAt !== normalized.createdAt ||
    current.isSystem !== true;

  if (!changed) return { members, changed: false };

  return {
    members: [normalized, ...members.filter((_, i) => i !== index)],
    changed: true,
  };
}

export function sanitizeSystemAdminPatch(id: string, data: Partial<Member>): Partial<Member> {
  if (id !== SYSTEM_ADMIN_MEMBER_ID) return data;
  const { name, username, departmentId, roleId, email, isSystem, ...allowed } = data;
  return { ...allowed, isSystem: true };
}

export function canDeleteSystemMember(id: string): boolean {
  return id !== SYSTEM_ADMIN_MEMBER_ID;
}

export function isSystemAdminMember(member: Pick<Member, 'id'>): boolean {
  return member.id === SYSTEM_ADMIN_MEMBER_ID;
}

export function resolveMemberDisplayName(ref: string | undefined | null, members: Member[]): string {
  if (!ref) return '-';
  const member = members.find((m) => m.id === ref || m.name === ref);
  if (member) return member.name;
  if (LEGACY_SYSTEM_ADMIN_NAMES.includes(ref)) {
    const admin = members.find((m) => m.id === SYSTEM_ADMIN_MEMBER_ID);
    return admin?.name ?? SYSTEM_ADMIN_MEMBER_NAME;
  }
  return ref;
}

export function isMemberReference(ref: string | undefined | null, member: Pick<Member, 'id' | 'name'>): boolean {
  if (!ref) return false;
  if (ref === member.id || ref === member.name) return true;
  return member.id === SYSTEM_ADMIN_MEMBER_ID && LEGACY_SYSTEM_ADMIN_NAMES.includes(ref);
}
