-- Customer-managed score policy for vector-only game identity resolution.

BEGIN
  EXECUTE IMMEDIATE q'[
    CREATE TABLE sg_game_scope_policy (
      policy_key   VARCHAR2(128) PRIMARY KEY,
      number_value NUMBER,
      text_value   VARCHAR2(4000),
      description  VARCHAR2(1000) NOT NULL,
      active_yn    CHAR(1) DEFAULT 'Y' NOT NULL,
      updated_at   TIMESTAMP(6) DEFAULT SYSTIMESTAMP NOT NULL,
      CONSTRAINT sg_game_scope_policy_active_ck CHECK (active_yn IN ('Y', 'N'))
    )]';
EXCEPTION
  WHEN OTHERS THEN
    IF SQLCODE != -955 THEN RAISE; END IF;
END;
/

MERGE INTO sg_game_scope_policy t
USING (
  SELECT 'GAME_ALIAS_MAX_COSINE_DISTANCE' AS policy_key,
         0.500000 AS number_value,
         CAST(NULL AS VARCHAR2(4000)) AS text_value,
         'Maximum cosine distance for accepting the closest independently embedded game alias.' AS description
    FROM dual
  UNION ALL
  SELECT 'SCOPE_GUIDANCE_NONE', NULL,
         '{"mode":"GAME_UNSPECIFIED","allowGameScopedObjects":false,"targetExecution":"COMMON_OBJECTS_OR_ZERO_ROW","instruction":"No game was selected. Do not use a game-scoped object. Use only a game-neutral common object when it answers the question; otherwise return a zero-row result."}',
         'Prompt guidance for a question without a selected game.'
    FROM dual
  UNION ALL
  SELECT 'SCOPE_GUIDANCE_SINGLE', NULL,
         '{"mode":"EXACT_TARGETS","allowGameScopedObjects":true,"targetExecution":"ONLY_RESOLVED_TARGETS","instruction":"Use only the resolved target in targets. Do not select another game-scoped object."}',
         'Prompt guidance for exactly one resolved game target.'
    FROM dual
  UNION ALL
  SELECT 'SCOPE_GUIDANCE_MULTI', NULL,
         '{"mode":"MULTIPLE_TARGETS","allowGameScopedObjects":true,"targetExecution":"ALL_RESOLVED_TARGETS","instruction":"Return results for all resolved available targets. Preserve unresolved targets as unavailable; do not replace them with another game."}',
         'Prompt guidance for multiple game targets.'
    FROM dual
  UNION ALL
  SELECT 'SCOPE_GUIDANCE_ALL', NULL,
         '{"mode":"ALL_CATALOG_TARGETS","allowGameScopedObjects":true,"targetExecution":"ALL_AVAILABLE_CATALOG_TARGETS","instruction":"Use all available catalog targets. Do not invent games or game-scoped objects outside the catalog."}',
         'Prompt guidance for every catalog game.'
    FROM dual
) s
ON (t.policy_key = s.policy_key)
WHEN MATCHED THEN UPDATE SET
  t.text_value = CASE
    WHEN s.policy_key LIKE 'SCOPE_GUIDANCE_%' THEN s.text_value
    ELSE t.text_value
  END,
  t.description = s.description,
  t.active_yn = 'Y',
  t.updated_at = SYSTIMESTAMP
WHEN NOT MATCHED THEN INSERT (
  policy_key, number_value, text_value, description, active_yn
) VALUES (
  s.policy_key, s.number_value, s.text_value, s.description, 'Y'
);
/

COMMENT ON TABLE sg_game_scope_policy IS
  'Customer-managed game scope policy values; changing a value requires no application deployment.';
COMMENT ON COLUMN sg_game_scope_policy.number_value IS
  'Numeric policy value. GAME_ALIAS_MAX_COSINE_DISTANCE applies to the closest alias vector.';
/
