/**
 * Global context object passed to all translation process functions
 * Contains lookup tables and computed values from external sources
 */
export interface TranslationContext {
  // Set of API User.id values for FK validation
  userIds: Set<string>;

  // Set of API ServiceTicket.id values for FK validation
  serviceTicketIds: Set<number>;

  // Set of API Opportunity.id values for FK validation
  opportunityIds: Set<number>;

  // Set of API CatalogItem.id values for FK validation
  catalogItemIds: Set<number>;

  // Set of API CorporateLocation.id values for FK validation
  corporateLocationIds: Set<number>;

  // Set of API Company.id values for FK validation
  companyIds: Set<number>;

  // Set of API CompanyAddress.id values for FK validation
  companyAddressIds: Set<number>;

  // Set of API Contact.id values for FK validation
  contactIds: Set<number>;

  // Set of API OpportunityStage.id values for FK validation
  opportunityStageIds: Set<number>;

  // User lookups: CW memberRecId -> API User.id
  usersByMemberRecId: Map<number, string>;

  // User lookups: CW memberRecId -> API User.cwIdentifier
  userIdentifiersByMemberRecId: Map<number, string>;

  // User lookups: CW member identifier string -> API User.id
  usersByIdentifier: Map<string, string>;

  // Service ticket board lookups: API ServiceTicketBoard.id -> API ServiceTicketBoard.uid
  serviceTicketBoardUidsById: Map<number, string>;

  // Optional custom-field derived lookups keyed by CW SR_Service_RecID
  billingTypeByTicketId: Map<number, string>;
  billingInstructionsByTicketId: Map<number, string>;

  // Opportunity/Product custom-field lookups loaded from CW reporting views.
  opportunityNarrativeByOpportunityId: Map<number, string>;
  productCustomByIvProductId: Map<
    number,
    {
      procurementNotes: string | null;
      productNarrative: string | null;
    }
  >;

  // Required FK fallback for Opportunity.typeId
  defaultOpportunityTypeId: number | null;

  // Set of valid OpportunityStatus.id values for FK validation
  opportunityStatusIds: Set<number>;

  // Set of valid ScheduleStatus.id values for FK validation
  scheduleStatusIds: Set<number>;

  // Set of valid ScheduleType.id values for FK validation
  scheduleTypeIds: Set<number>;

  // Set of valid ScheduleSpan.id values for FK validation
  scheduleSpanIds: Set<number>;

  // Set of API TaxCode.id values for FK validation
  taxCodeIds: Set<number>;

  // Set of API ActivityType.id values for FK validation
  activityTypeIds: Set<number>;

  // Set of API ActivityStatus.id values for FK validation
  activityStatusIds: Set<number>;

  // Set of API Activity.id values for FK validation (used by ActivityNotes)
  activityIds: Set<number>;

  // Set of API TimeEntryChargeCode.id values for FK validation
  timeEntryChargeCodeIds: Set<number>;

  // Map: CW TE_Status_ID (smallint) -> API TimeEntryStatus.id (rec ID)
  timeEntryStatusIdByTeStatusId: Map<number, number>;
}

/**
 * Create an empty context object with initialized maps
 */
export function createTranslationContext(): TranslationContext {
  return {
    userIds: new Set(),
    serviceTicketIds: new Set(),
    opportunityIds: new Set(),
    catalogItemIds: new Set(),
    corporateLocationIds: new Set(),
    companyIds: new Set(),
    companyAddressIds: new Set(),
    contactIds: new Set(),
    opportunityStageIds: new Set(),
    usersByMemberRecId: new Map(),
    userIdentifiersByMemberRecId: new Map(),
    usersByIdentifier: new Map(),
    serviceTicketBoardUidsById: new Map(),
    billingTypeByTicketId: new Map(),
    billingInstructionsByTicketId: new Map(),
    opportunityNarrativeByOpportunityId: new Map(),
    productCustomByIvProductId: new Map(),
    defaultOpportunityTypeId: null,
    opportunityStatusIds: new Set(),
    scheduleStatusIds: new Set(),
    scheduleTypeIds: new Set(),
    scheduleSpanIds: new Set(),
    taxCodeIds: new Set(),
    activityTypeIds: new Set(),
    activityStatusIds: new Set(),
    activityIds: new Set(),
    timeEntryChargeCodeIds: new Set(),
    timeEntryStatusIdByTeStatusId: new Map(),
  };
}
