/**
 * Utility functions for mapping users to participants
 */

import { Participant, Approver, Spectator } from '../types/createRequest.types';

/**
 * Map initiator to participant
 */
export function mapInitiatorToParticipant(user: any): Participant {
  const initiatorId = user?.userId || '';
  const initiatorEmail = (user as any)?.email || '';
  const initiatorName = (user as any)?.displayName || 
                       (user as any)?.name || 
                       initiatorEmail.split('@')[0] || 
                       'Initiator';

  return {
    userId: initiatorId,
    userEmail: initiatorEmail,
    userName: initiatorName,
    participantType: 'INITIATOR',
    canComment: true,
    canViewDocuments: true,
    canDownloadDocuments: true,
    notificationEnabled: true,
    addedBy: initiatorId,
  };
}

/**
 * Map approvers to participants
 */
export function mapApproversToParticipants(
  approvers: Approver[],
  initiatorId: string
): Participant[] {
  return approvers
    .filter((a) => a?.email)
    .map((a) => ({
      userId: a.userId || undefined,
      userEmail: a.email,
      userName: a.name || a.email.split('@')[0] || 'Approver',
      participantType: 'APPROVER' as const,
      canComment: true,
      canViewDocuments: true,
      canDownloadDocuments: true,
      notificationEnabled: true,
      addedBy: initiatorId,
    }));
}

/**
 * Map spectators to participants
 */
export function mapSpectatorsToParticipants(
  spectators: Spectator[],
  initiatorId: string
): Participant[] {
  return spectators
    .filter((s) => s?.email)
    .map((s) => ({
      userId: s.id || s.userId || undefined,
      userEmail: s.email,
      userName: s.name || s.email.split('@')[0] || 'Spectator',
      participantType: 'SPECTATOR' as const,
      canComment: true,
      canViewDocuments: true,
      canDownloadDocuments: true,
      notificationEnabled: true,
      addedBy: initiatorId,
    }));
}

/**
 * Build complete participants array
 */
export function buildParticipantsArray(
  user: any,
  approvers: Approver[],
  spectators: Spectator[]
): Participant[] {
  const initiator = mapInitiatorToParticipant(user);
  const initiatorId = user?.userId || '';
  
  const approverParticipants = mapApproversToParticipants(approvers, initiatorId);
  const spectatorParticipants = mapSpectatorsToParticipants(spectators, initiatorId);

  return [initiator, ...approverParticipants, ...spectatorParticipants];
}

/**
 * Filter duplicate participants (for edit mode)
 */
export function filterDuplicateParticipants(
  approvers: Approver[],
  spectators: Spectator[]
): Spectator[] {
  const approverIds = approvers.map((a) => a?.userId).filter(Boolean);
  const approverEmails = approvers
    .map((a) => a?.email?.toLowerCase?.())
    .filter(Boolean);

  return spectators.filter((s) => {
    const isDuplicateId = approverIds.includes(s?.id);
    const isDuplicateEmail = approverEmails.includes((s?.email || '').toLowerCase());
    return !isDuplicateId && !isDuplicateEmail;
  });
}

