import type { TripRole } from '@/types/db'

/**
 * Per-trip permission matrix. Nash: "The relation between the order and the
 * emails of the invited people into that order — that's what's relevant."
 * Invite rules follow the circle-based model in the product docs.
 */
export const INVITE_RULES: Record<TripRole, TripRole[]> = {
  broker: ['dispatcher', 'shipper', 'pilot', 'driver'],
  dispatcher: ['driver', 'pilot', 'broker'],
  driver: ['dispatcher', 'pilot'],
  pilot: ['driver'],
  shipper: ['broker'],
  admin: ['broker', 'dispatcher', 'driver', 'pilot', 'shipper'],
}

export function canInvite(inviterRole: TripRole, inviteeRole: TripRole): boolean {
  return INVITE_RULES[inviterRole]?.includes(inviteeRole) ?? false
}

/** If you can invite a role you can remove that role (docs rule). */
export function canRemove(removerRole: TripRole, removedRole: TripRole): boolean {
  return canInvite(removerRole, removedRole)
}

export function canUploadDocuments(role: TripRole): boolean {
  return role !== 'shipper'
}

// "Anyone that has access to this load will be able to buy the Google Maps" — Nash
export function canRequestServices(role: TripRole): boolean {
  return INVITE_RULES[role] !== undefined
}
