export interface ParticipantColor {
  key: string;
  text: string;
  background: string;
  border: string;
  avatarBackground: string;
  avatarText: string;
}

export type ThemeMode = 'light' | 'dark';

const REQUESTER_LIGHT: ParticipantColor = {
  key: 'requester',
  text: '#475569',
  background: '#F1F5F9',
  border: '#CBD5E1',
  avatarBackground: '#64748B',
  avatarText: '#FFFFFF',
};

const REQUESTER_DARK: ParticipantColor = {
  key: 'requester',
  text: '#94A3B8',
  background: '#1E293B',
  border: '#334155',
  avatarBackground: '#475569',
  avatarText: '#F1F5F9',
};

const ASSIGNEE_LIGHT: ParticipantColor = {
  key: 'assignee',
  text: '#047857',
  background: '#ECFDF5',
  border: '#A7F3D0',
  avatarBackground: '#059669',
  avatarText: '#FFFFFF',
};

const ASSIGNEE_DARK: ParticipantColor = {
  key: 'assignee',
  text: '#6EE7B7',
  background: '#064E3B',
  border: '#047857',
  avatarBackground: '#10B981',
  avatarText: '#ECFDF5',
};

const LIGHT_PALETTE: ParticipantColor[] = [
  {
    key: 'blue',
    text: '#1D4ED8',
    background: '#EFF6FF',
    border: '#BFDBFE',
    avatarBackground: '#3B82F6',
    avatarText: '#FFFFFF',
  },
  {
    key: 'teal',
    text: '#0F766E',
    background: '#F0FDFA',
    border: '#99F6E4',
    avatarBackground: '#14B8A6',
    avatarText: '#FFFFFF',
  },
  {
    key: 'green',
    text: '#15803D',
    background: '#F0FDF4',
    border: '#BBF7D0',
    avatarBackground: '#22C55E',
    avatarText: '#FFFFFF',
  },
  {
    key: 'amber',
    text: '#B45309',
    background: '#FFFBEB',
    border: '#FDE68A',
    avatarBackground: '#F59E0B',
    avatarText: '#FFFFFF',
  },
  {
    key: 'violet',
    text: '#6D28D9',
    background: '#F5F3FF',
    border: '#DDD6FE',
    avatarBackground: '#8B5CF6',
    avatarText: '#FFFFFF',
  },
  {
    key: 'rose',
    text: '#BE123C',
    background: '#FFF1F2',
    border: '#FECDD3',
    avatarBackground: '#F43F5E',
    avatarText: '#FFFFFF',
  },
  {
    key: 'cyan',
    text: '#0E7490',
    background: '#ECFEFF',
    border: '#A5F3FC',
    avatarBackground: '#06B6D4',
    avatarText: '#FFFFFF',
  },
  {
    key: 'indigo',
    text: '#4338CA',
    background: '#EEF2FF',
    border: '#C7D2FE',
    avatarBackground: '#6366F1',
    avatarText: '#FFFFFF',
  },
  {
    key: 'lime',
    text: '#4D7C0F',
    background: '#F7FEE7',
    border: '#D9F99D',
    avatarBackground: '#84CC16',
    avatarText: '#FFFFFF',
  },
  {
    key: 'orange',
    text: '#C2410C',
    background: '#FFF7ED',
    border: '#FED7AA',
    avatarBackground: '#F97316',
    avatarText: '#FFFFFF',
  },
  {
    key: 'sky',
    text: '#0369A1',
    background: '#F0F9FF',
    border: '#BAE6FD',
    avatarBackground: '#0EA5E9',
    avatarText: '#FFFFFF',
  },
  {
    key: 'fuchsia',
    text: '#A21CAF',
    background: '#FDF4FF',
    border: '#F5D0FE',
    avatarBackground: '#D946EF',
    avatarText: '#FFFFFF',
  },
];

const DARK_PALETTE: ParticipantColor[] = [
  {
    key: 'blue',
    text: '#93C5FD',
    background: '#172554',
    border: '#1E40AF',
    avatarBackground: '#2563EB',
    avatarText: '#EFF6FF',
  },
  {
    key: 'teal',
    text: '#5EEAD4',
    background: '#042F2E',
    border: '#115E59',
    avatarBackground: '#0D9488',
    avatarText: '#F0FDFA',
  },
  {
    key: 'green',
    text: '#86EFAC',
    background: '#052E16',
    border: '#166534',
    avatarBackground: '#16A34A',
    avatarText: '#F0FDF4',
  },
  {
    key: 'amber',
    text: '#FCD34D',
    background: '#422006',
    border: '#92400E',
    avatarBackground: '#D97706',
    avatarText: '#FFFBEB',
  },
  {
    key: 'violet',
    text: '#C4B5FD',
    background: '#2E1065',
    border: '#5B21B6',
    avatarBackground: '#7C3AED',
    avatarText: '#F5F3FF',
  },
  {
    key: 'rose',
    text: '#FDA4AF',
    background: '#4C0519',
    border: '#9F1239',
    avatarBackground: '#E11D48',
    avatarText: '#FFF1F2',
  },
  {
    key: 'cyan',
    text: '#67E8F9',
    background: '#164E63',
    border: '#0E7490',
    avatarBackground: '#0891B2',
    avatarText: '#ECFEFF',
  },
  {
    key: 'indigo',
    text: '#A5B4FC',
    background: '#312E81',
    border: '#4338CA',
    avatarBackground: '#4F46E5',
    avatarText: '#EEF2FF',
  },
  {
    key: 'lime',
    text: '#BEF264',
    background: '#1A2E05',
    border: '#3F6212',
    avatarBackground: '#65A30D',
    avatarText: '#F7FEE7',
  },
  {
    key: 'orange',
    text: '#FDBA74',
    background: '#431407',
    border: '#9A3412',
    avatarBackground: '#EA580C',
    avatarText: '#FFF7ED',
  },
  {
    key: 'sky',
    text: '#7DD3FC',
    background: '#0C4A6E',
    border: '#0369A1',
    avatarBackground: '#0284C7',
    avatarText: '#F0F9FF',
  },
  {
    key: 'fuchsia',
    text: '#F0ABFC',
    background: '#4A044E',
    border: '#86198F',
    avatarBackground: '#C026D3',
    avatarText: '#FDF4FF',
  },
];

export function hashStringToIndex(value: string, paletteLength: number): number {
  if (paletteLength <= 0) {
    return 0;
  }

  const normalized = value.trim();
  if (!normalized) {
    return 0;
  }

  let hash = 0;
  for (let i = 0; i < normalized.length; i += 1) {
    hash = (hash << 5) - hash + normalized.charCodeAt(i);
    hash |= 0;
  }

  return Math.abs(hash) % paletteLength;
}

export function getRequesterColor(mode: ThemeMode = 'light'): ParticipantColor {
  return mode === 'dark' ? REQUESTER_DARK : REQUESTER_LIGHT;
}

export function getAssigneeColor(mode: ThemeMode = 'light'): ParticipantColor {
  return mode === 'dark' ? ASSIGNEE_DARK : ASSIGNEE_LIGHT;
}

export function getAgentColor(mode: ThemeMode = 'light'): ParticipantColor {
  return getAssigneeColor(mode);
}

const DEPARTMENT_HEAD_LIGHT: ParticipantColor = {
  key: 'department_head',
  text: '#4338CA',
  background: '#EEF2FF',
  border: '#C7D2FE',
  avatarBackground: '#6366F1',
  avatarText: '#FFFFFF',
};

const DEPARTMENT_HEAD_DARK: ParticipantColor = {
  key: 'department_head',
  text: '#A5B4FC',
  background: '#312E81',
  border: '#4338CA',
  avatarBackground: '#4F46E5',
  avatarText: '#EEF2FF',
};

const COMPANY_ADMIN_LIGHT: ParticipantColor = {
  key: 'company_admin',
  text: '#B45309',
  background: '#FFFBEB',
  border: '#FDE68A',
  avatarBackground: '#D97706',
  avatarText: '#FFFFFF',
};

const COMPANY_ADMIN_DARK: ParticipantColor = {
  key: 'company_admin',
  text: '#FCD34D',
  background: '#422006',
  border: '#92400E',
  avatarBackground: '#F59E0B',
  avatarText: '#FFFBEB',
};

const SYSTEM_LIGHT: ParticipantColor = {
  key: 'system',
  text: '#64748B',
  background: '#F8FAFC',
  border: '#E2E8F0',
  avatarBackground: '#94A3B8',
  avatarText: '#FFFFFF',
};

const SYSTEM_DARK: ParticipantColor = {
  key: 'system',
  text: '#94A3B8',
  background: '#1E293B',
  border: '#334155',
  avatarBackground: '#64748B',
  avatarText: '#F8FAFC',
};

export function getDepartmentHeadColor(mode: ThemeMode = 'light'): ParticipantColor {
  return mode === 'dark' ? DEPARTMENT_HEAD_DARK : DEPARTMENT_HEAD_LIGHT;
}

export function getCompanyAdminColor(mode: ThemeMode = 'light'): ParticipantColor {
  return mode === 'dark' ? COMPANY_ADMIN_DARK : COMPANY_ADMIN_LIGHT;
}

export function getSystemColor(mode: ThemeMode = 'light'): ParticipantColor {
  return mode === 'dark' ? SYSTEM_DARK : SYSTEM_LIGHT;
}

/** Deterministic palette entry for ticket collaborators (not assignee/requester). */
export function getCollaboratorColor(userId: string, mode: ThemeMode = 'light'): ParticipantColor {
  return getParticipantColor(userId, undefined, mode);
}

export function mixParticipantAccent(color: string, weightPercent = 20, base = 'transparent'): string {
  return `color-mix(in srgb, ${color} ${weightPercent}%, ${base})`;
}

/** @deprecated Prefer resolveTicketParticipantColor with full ticket context. */
export function resolveParticipantColorForUser(
  userId: string,
  requesterId: string,
  mode: ThemeMode = 'light',
  context?: {
    assigneeId?: string | null;
    collaboratorUserIds?: string[];
    collaboratorRolesByUserId?: Record<string, string[]>;
    systemUserIds?: string[];
  },
): ParticipantColor {
  // Lazy import avoided — inline minimal context resolution for backward compatibility.
  if (userId && userId === requesterId) {
    return getRequesterColor(mode);
  }

  if (context?.assigneeId && userId === context.assigneeId) {
    return getAgentColor(mode);
  }

  const roleNames = (context?.collaboratorRolesByUserId?.[userId] ?? []).map((role) =>
    role.toLowerCase(),
  );

  if (
    roleNames.some(
      (role) =>
        role.includes('super_admin') ||
        role.includes('company_admin') ||
        role.startsWith('company_admin_'),
    )
  ) {
    return getCompanyAdminColor(mode);
  }

  if (roleNames.some((role) => role.includes('department_head') || role.includes('manager'))) {
    return getDepartmentHeadColor(mode);
  }

  if (context?.collaboratorUserIds?.includes(userId)) {
    return getCollaboratorColor(userId, mode);
  }

  if (!userId?.trim() || context?.systemUserIds?.includes(userId)) {
    return getSystemColor(mode);
  }

  return getAgentColor(mode);
}

function getPalette(mode: ThemeMode): ParticipantColor[] {
  return mode === 'dark' ? DARK_PALETTE : LIGHT_PALETTE;
}

export function getParticipantColor(
  userId: string,
  role?: string,
  mode: ThemeMode = 'light',
): ParticipantColor {
  if (role === 'requester') {
    return getRequesterColor(mode);
  }

  if (role === 'assignee') {
    return getAssigneeColor(mode);
  }

  const palette = getPalette(mode);
  const fallback: ParticipantColor = { ...palette[0], key: 'fallback' };

  if (!userId?.trim()) {
    return fallback;
  }

  const index = hashStringToIndex(userId, palette.length);
  return palette[index] ?? fallback;
}

export function isRequesterParticipant(
  participant: { id?: string; user_id?: string; role?: string },
  ticket: { requester: { id: string } },
): boolean {
  const participantId = participant.id ?? participant.user_id;
  if (participantId && participantId === ticket.requester.id) {
    return true;
  }

  return participant.role === 'requester';
}

export function getParticipantPaletteLength(mode: ThemeMode = 'light'): number {
  return getPalette(mode).length;
}
