-- One short OCI Chat call extracts game-name mentions. Identity resolution is
-- performed later by catalog vector score, not by another LLM call.
CREATE OR REPLACE FUNCTION sg_game_extract_mentions(p_question IN CLOB)
  RETURN CLOB AUTHID DEFINER
IS
  v_prompt CLOB;
  v_result CLOB;
  v_extract JSON_OBJECT_T;
BEGIN
  v_prompt := 'Extract only game identity mentions from the user question. '
    || 'A registered game title, alias, GAME_ID, GAME_PREFIX, or catalog key is a game mention and must be preserved exactly as written. '
    || 'Metrics, acronyms, dates, filters, and database object or column names are not game mentions unless they are themselves an explicit registered game identity. '
    || 'When a title-like noun directly qualifies a game data request such as user master, character, sales, AU, NRU, server, or game log, preserve that noun as a game-name mention even when it is not in a catalog. '
    || 'Do not discard an unknown title merely because it cannot be resolved. General scope words such as common, overall, all, total, or every are not game-name mentions unless they are part of an explicit title. '
    || 'Return exactly one JSON object with keys game_mentions (array of strings) '
    || 'and scope_hint (GLOBAL, SINGLE_GAME, MULTI_GAME, ALL_GAMES, UNKNOWN). '
    || 'Do not resolve one game identity to another and do not generate SQL. '
    || 'Return raw JSON only: no prose, no Markdown, and no code fence. Question: '
    || DBMS_LOB.SUBSTR(p_question, 4000, 1);

  v_result := DBMS_CLOUD_AI.GENERATE(
    prompt => v_prompt,
    profile_name => 'SGMP_POC_OCI_COHERE_COMMAND',
    action => 'chat'
  );
  v_extract := JSON_OBJECT_T.parse(v_result);
  IF NOT v_extract.has('game_mentions') OR NOT v_extract.has('scope_hint') THEN
    RAISE_APPLICATION_ERROR(
      -20091,
      'Game mention extraction must return game_mentions and scope_hint JSON keys.'
    );
  END IF;
  RETURN v_result;
END;
/
