-- Generic Few-shot structure for multi-target comparisons on a common fact object.
-- The template is intentionally logical: no current game, prefix, ID, date, or result value is embedded.

DECLARE
  v_exists     NUMBER;
  v_input      CLOB;
  v_embedding  VECTOR;
BEGIN
  SELECT COUNT(*)
    INTO v_exists
    FROM sg_qa_vector_example
   WHERE source_type = 'POLICY_TEMPLATE'
     AND source_case_id = 'MULTI_COMMON_FACT_LEFT_JOIN';

  IF v_exists = 0 THEN
    v_input := TO_CLOB('Question pattern: Compare a common fact metric across multiple resolved games. Use only fact-query-eligible targets and separately report known registry-only or unavailable targets.')
      || CHR(10) || 'Logical object role: SALES_TRANSACTION'
      || CHR(10) || 'Required structure: active alias catalog distinct game set, left join fact, aggregate by catalog display identifier.';
    v_embedding := DBMS_VECTOR.UTL_TO_EMBEDDING(
      v_input,
      JSON(sg_qa_vector_params('search_document'))
    );

    INSERT INTO sg_qa_vector_example (
      question, answer_sql, answer_text, embedding_input, embedding, embedding_model,
      reference_status, reference_kind, target_type, object_role,
      inspection_status, inspection_note, verified_at, verified_by,
      source_case_id, source_type
    ) VALUES (
      'Compare a common fact metric across multiple resolved games, including games with no fact rows.',
      TO_CLOB('WITH resolved_games AS (' || CHR(10)
        || '  SELECT DISTINCT a."GAME_ID", a."GAME_NM"' || CHR(10)
        || '  FROM "SGMP_POC"."COMN_GAME_ALIAS_BAS" a' || CHR(10)
        || '  WHERE a."USE_YN" = ''Y''' || CHR(10)
        || '    AND (<ACTIVE_ALIAS_MATCHES_FOR_EACH_REQUESTED_GAME_TERM>)' || CHR(10)
        || ')' || CHR(10)
        || 'SELECT g."GAME_NM" AS "GAME_NAME",' || CHR(10)
        || '       NVL(SUM(CASE WHEN <FACT_DATE_AND_EXCLUSION_CONDITION>' || CHR(10)
        || '                    THEN CAST(f."<METRIC_COLUMN>" AS NUMBER) ELSE 0 END), 0) AS "METRIC_VALUE"' || CHR(10)
        || 'FROM resolved_games g' || CHR(10)
        || 'LEFT JOIN "SGMP_POC"."<APPROVED_COMMON_FACT_OBJECT>" f' || CHR(10)
        || '  ON f."GAME_ID" = g."GAME_ID"' || CHR(10)
        || 'GROUP BY g."GAME_NM"' || CHR(10)
        || 'ORDER BY g."GAME_NM"'),
      'Structural Few-shot only. Replace every angle-bracket placeholder from the current approved object metadata, the current game query plan, and the original question. Only ACTIVE_ALIAS targets enter the alias-driven LEFT JOIN and grouping; report registry-only or unavailable targets from the plan without manufacturing fact rows with DUAL/UNION.',
      v_input,
      v_embedding,
      'cohere.embed-v4.0',
      'APPROVED', 'SQL_TEMPLATE', 'MULTI', 'SALES_TRANSACTION',
      'VERIFIED',
      'Generic, non-customer-specific comparison structure. Verified against the game-alias and common-fact metadata contract; not an executable answer key.',
      SYSTIMESTAMP, 'SGMP_POC_METADATA_REVIEW',
      'MULTI_COMMON_FACT_LEFT_JOIN', 'POLICY_TEMPLATE'
    );
  END IF;
  COMMIT;
END;
/

-- Keep the approved template current when the policy text evolves. The vector
-- is rebuilt from its generic retrieval text; no customer answer is embedded.
DECLARE
  v_input     CLOB;
  v_embedding VECTOR;
BEGIN
  v_input := TO_CLOB('Question pattern: Compare a common fact metric across multiple resolved games. Use only fact-query-eligible targets and separately report known registry-only or unavailable targets.')
    || CHR(10) || 'Logical object role: SALES_TRANSACTION'
    || CHR(10) || 'Required structure: active alias catalog distinct game set, left join fact, aggregate by catalog display identifier.';
  v_embedding := DBMS_VECTOR.UTL_TO_EMBEDDING(
    v_input,
    JSON(sg_qa_vector_params('search_document'))
  );

  UPDATE sg_qa_vector_example
     SET question = 'Compare a common fact metric across multiple resolved games, using only fact-query-eligible targets.',
         answer_text = 'Structural Few-shot only. Replace every angle-bracket placeholder from the current approved object metadata, the current game query plan, and the original question. Only ACTIVE_ALIAS targets enter the alias-driven LEFT JOIN and grouping; report registry-only or unavailable targets from the plan without manufacturing fact rows with DUAL/UNION.',
         embedding_input = v_input,
         embedding = v_embedding,
         inspection_note = 'Generic, non-customer-specific comparison structure. Active-alias targets are fact-query eligible; registry-only targets are reported separately. Not an executable answer key.',
         verified_at = SYSTIMESTAMP,
         verified_by = 'SGMP_POC_METADATA_REVIEW'
   WHERE source_type = 'POLICY_TEMPLATE'
     AND source_case_id = 'MULTI_COMMON_FACT_LEFT_JOIN';
  COMMIT;
END;
/

SELECT example_id, reference_status, reference_kind, target_type, object_role,
       source_case_id, source_type
  FROM sg_qa_vector_example
 WHERE source_type = 'POLICY_TEMPLATE'
   AND source_case_id = 'MULTI_COMMON_FACT_LEFT_JOIN';
