/**
 * GA4 Event Schema — single source of truth for Envive → GA4 projection.
 *
 * Every EnviveMetricsEventName MUST have an entry here. The Record type enforces
 * this at compile time — adding a new enum member without a schema entry will
 * cause a TypeScript error.
 *
 * ## How to update this file
 *
 * When adding or modifying event properties, decide whether they belong in `allowedFields` using these criteria:
 * - **Low cardinality** — avoid unbounded values (free text, IDs, URLs, timestamps).
 * - **Non-sensitive** — no PII, user input text, or internal implementation details.
 * - **Flat scalar** — GA4 custom dimensions are flat strings/numbers/booleans.
 *   Nested objects should NOT be added; extract specific sub-fields instead.
 * - **Snake_case key** — must already be dot-notation snake_case (e.g. `chat.user_typed`).
 * - **Analytically useful** — the field should enable meaningful filtering/segmentation
 *   for merchants in their GA4 dashboards.
 *
 * If excluded from `allowedFields`, the projection layer will automatically drop it.
 *
 * IF IN DOUBT, DON'T HESITATE TO REACH OUT TO THE ANALYTICS TEAM!!!
 */
import { EnviveMetricsEventName } from '../amplitudeService/eventNames';

/**
 * Defines which fields to extract from `trigger.widget_interaction_data`
 * for a given `trigger.widget_interaction` value.
 *
 * Key: the value of `trigger.widget_interaction` (e.g. "widget_collapsed")
 * Value: mapping of { ga4FlatKey: sourceFieldInWidgetInteractionData }
 */
export type WidgetInteractionDataProjection = Record<string, Record<string, string>>;

export interface GA4ProjectedEventConfig {
  gaEventName: string;
  allowedFields: readonly string[];
  /**
   * When present, used instead of `allowedFields` to both filter and rename fields.
   * Key: the desired GA4 parameter name. Value: the source dot-notation key in eventProps.
   */
  fieldProjections?: Record<string, string>;
  widgetInteractionDataProjections?: WidgetInteractionDataProjection;
}

export interface GA4ExcludedEventConfig {
  gaEventName: null;
}

export type GA4EventSchemaEntry = GA4ProjectedEventConfig | GA4ExcludedEventConfig;

const WIDGET_INTERACTION_DATA_PROJECTIONS: WidgetInteractionDataProjection = {
  widget_collapsed: { interaction_collapse_source: 'widget_collapsed.collapse_source' },
  product_card_clicked: { interaction_product_id: 'product_card_clicked.product_id' },
  suggestion_scrolled: { interaction_suggestion_id: 'suggestion_scrolled.suggestion_id' },
};

export const GA4_EVENT_SCHEMA: Record<EnviveMetricsEventName, GA4EventSchemaEntry> = {
  [EnviveMetricsEventName.WidgetRendered]: {
    gaEventName: 'envive_widget_rendered',
    allowedFields: [],
    fieldProjections: {
      page_type: 'context.page_type',
      page_id: 'context.page_id',
      surface: 'context.surface',
      widget: 'trigger.widget',
      widget_role: 'trigger.widget_role',
    },
  },

  [EnviveMetricsEventName.WidgetInteraction]: {
    gaEventName: 'envive_widget_interaction',
    allowedFields: [],
    fieldProjections: {
      page_type: 'context.page_type',
      page_id: 'context.page_id',
      surface: 'context.surface',
      widget: 'trigger.widget',
      interaction_type: 'trigger.widget_interaction',
      interaction_class: 'trigger.interaction_class',
      widget_role: 'trigger.widget_role',
    },
    widgetInteractionDataProjections: WIDGET_INTERACTION_DATA_PROJECTIONS,
  },

  [EnviveMetricsEventName.ChatRequest]: {
    gaEventName: 'envive_chat_request',
    allowedFields: [
      'page_type',
      'page_id',
      'trigger.widget',
      'chat.request_type',
      // 'chat.request_text', // This is high cardinality and potentially PII but we might want to include it later
      'chat.user_typed',
      'chat.suggestion_id', // not currently implemented
      'chat.suggestion_category', // not currently implemented
      'chat.suggestion_created_at', // not currently implemented
      'chat.suggestion_is_answer', // not currently implemented
      'chat.form_type', // not currently implemented
    ],
  },

  [EnviveMetricsEventName.ChatResponse]: {
    gaEventName: 'envive_chat_response',
    allowedFields: [
      'page_type',
      'page_id',
      'trigger.widget', // not currently implemented (maybe it should'nt be???)
      'chat.user_typed',
      'chat.response_time_ms',
      'chat.product_cards_returned',
      'chat.product_ids_returned',
      'chat.review_cards_returned',
    ],
  },

  [EnviveMetricsEventName.EnviveInitialized]: {
    gaEventName: 'envive_initialized',
    allowedFields: [
      'environment.sales_agent_enabled',
      'environment.envive_enabled',
      // 'environment.search_enabled', // For now, search is not implemented. We will add it later.
      'performance.start_time_ms',
      'performance.initialize_time_ms',
    ],
  },

  // This event is not currently implemented
  [EnviveMetricsEventName.PageViewed]: {
    gaEventName: 'envive_page_context_evaluated',
    allowedFields: [],
    fieldProjections: {
      page_type: 'context.page_type',
      page_id: 'context.page_id',
      context_supported: 'context.supported',
      context_ready: 'context.ready',
      context_page_variant_id: 'context.page_variant_id',
      environment_envive_enabled: 'environment.envive_enabled',
    },
  },

  [EnviveMetricsEventName.WidgetTextRequest]: {
    gaEventName: null,
  },

  [EnviveMetricsEventName.WidgetTextResponse]: {
    gaEventName: null,
  },

  [EnviveMetricsEventName.WidgetTextClicked]: {
    gaEventName: null,
  },
};
