/**
 * Company Admin invitations — shared by the Carrier and Broker pages
 * (Nash, 2026-09-19): "copy-paste a list of emails, send an invite, and
 * everybody gets that invite… expediting the onboarding process."
 */
export const BULK_INVITE_LIMIT = 200

/**
 * Pasted text → clean, de-duplicated emails. Accepts lines, commas,
 * semicolons, spaces and "Name <email>" forms. Lines with no email at all
 * come back as `invalid` so the admin sees them before sending.
 */
export function parseEmailList(raw: string): { emails: string[]; invalid: string[] } {
  const emails: string[] = []
  const invalid: string[] = []
  const seen = new Set<string>()
  for (const line of raw.split(/\r?\n/)) {
    if (!line.trim()) continue
    const found = line.match(/[^\s<>,;"'()]+@[^\s<>,;"'()]+/g) ?? []
    for (const token of found) {
      const candidate = token.trim().toLowerCase().replace(/[.,;]+$/, '')
      if (!/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(candidate)) { invalid.push(candidate); continue }
      if (seen.has(candidate)) continue
      seen.add(candidate)
      emails.push(candidate)
    }
    if (found.length === 0) invalid.push(line.trim())
  }
  return { emails, invalid }
}

/** What the page shows under "Invited — waiting for sign-up". */
export interface InvitedPerson {
  id: string
  name: string | null
  email: string
  invitedAt: string | null
}
