CREATE TEMP FUNCTION enumerated_array(results ARRAY<STRING>, _groups ARRAY<STRING>) AS (
  ARRAY(
    SELECT
      STRUCT(
        off + 1 AS position,
        r AS result_type,
        `mozfun.norm.result_type_to_product_name`(r) AS product_result_type,
        _groups[SAFE_OFFSET(off)] AS result_group
      )
    FROM
      UNNEST(results) AS r
      WITH OFFSET off
  )
);

CREATE TEMP FUNCTION get_event_action(event_name STRING, engagement_type STRING) AS (
  CASE
    WHEN event_name = 'engagement'
      AND (engagement_type IN ("click", "drop_go", "enter", "go_button", "paste_go"))
      THEN 'engaged'
    WHEN event_name = 'abandonment'
      THEN 'abandoned'
    WHEN event_name = 'engagement'
      AND (engagement_type NOT IN ("click", "drop_go", "enter", "go_button", "paste_go"))
      THEN "annoyance"
    ELSE NULL
  END
);

CREATE TEMP FUNCTION get_is_terminal(selected_result STRING, engagement_type STRING) AS (
  --events where the urlbar dropdown menu remains open (i.e., the urlbar session did not end)
  COALESCE(
    NOT (selected_result = 'tab_to_search' AND engagement_type IN ('click', 'enter', 'go_button'))
    AND NOT (
      selected_result = 'tip_dismissal_acknowledgement'
      AND engagement_type IN ('click', 'enter')
    )
    AND NOT (
      engagement_type IN (
        'dismiss',
        'inaccurate_location',
        'not_interested',
        'not_relevant',
        'show_less_frequently'
      )
    ),
    TRUE
  )
);

WITH events_unnested AS (
  SELECT
    DATE(submission_timestamp) AS submission_date,
    client_info.client_id AS glean_client_id,
    metrics.uuid.legacy_telemetry_client_id AS legacy_telemetry_client_id,
    metrics.uuid.legacy_telemetry_profile_group_id AS profile_group_id,
    sample_id,
    event.name AS event_name,
    timestamp AS event_timestamp,
    GENERATE_UUID() AS event_id,
    ping_info.experiments,
    ping_info.seq,
    normalized_channel,
    normalized_country_code,
    `moz-fx-data-shared-prod`.udf.normalize_search_engine(
      mozfun.map.get_key(extra, "search_engine_default_id")
    ) AS normalized_engine,
    COALESCE(metrics.boolean.urlbar_pref_suggest_data_collection, FALSE) AS pref_data_collection,
    COALESCE(metrics.boolean.urlbar_pref_suggest_sponsored, FALSE) AS pref_sponsored_suggestions,
    COALESCE(metrics.boolean.urlbar_pref_suggest_nonsponsored, FALSE) AS pref_fx_suggestions,
    mozfun.map.get_key(extra, "engagement_type") AS engagement_type,
    mozfun.map.get_key(extra, "interaction") AS interaction,
    SAFE_CAST(mozfun.map.get_key(extra, "n_chars") AS int) AS num_chars_typed,
    SAFE_CAST(mozfun.map.get_key(extra, "n_results") AS int) AS num_total_results,
  --If 0, then no result was selected.
    NULLIF(
      SAFE_CAST(mozfun.map.get_key(extra, "selected_position") AS int),
      0
    ) AS selected_position,
    mozfun.map.get_key(extra, "selected_result") AS selected_result,
    enumerated_array(
      SPLIT(mozfun.map.get_key(extra, "results"), ','),
      SPLIT(mozfun.map.get_key(extra, "groups"), ',')
    ) AS results,
  FROM
    `{{ project_id }}.{{ app_name }}_stable.events_v1`,
    UNNEST(events) AS event
  WHERE
    DATE(submission_timestamp) = @submission_date
    AND event.category = 'urlbar'
    AND event.name IN ('engagement', 'abandonment')
)
SELECT
  *,
  `mozfun.norm.result_type_to_product_name`(selected_result) AS product_selected_result,
  get_event_action(event_name, engagement_type) AS event_action,
  get_is_terminal(selected_result, engagement_type) AS is_terminal,
  CASE
    WHEN get_event_action(event_name, engagement_type) IN ('engaged', 'annoyance')
      THEN selected_result
    ELSE NULL
  END AS engaged_result_type,
  CASE
    WHEN get_event_action(event_name, engagement_type) IN ('engaged', 'annoyance')
      THEN `mozfun.norm.result_type_to_product_name`(selected_result)
    ELSE NULL
  END AS product_engaged_result_type,
  CASE
    WHEN get_event_action(event_name, engagement_type) = 'annoyance'
      THEN engagement_type
    ELSE NULL
  END AS annoyance_signal_type,
FROM
  events_unnested
