-- Creates a non-operational comparison profile for Smilegate game-scope chat.
-- The active GPT profile remains unchanged. Provider credentials and profile
-- metadata are copied from it so the only comparison variable is the model.

DECLARE
  v_exists PLS_INTEGER;
  v_credential_name VARCHAR2(128);
  v_region VARCHAR2(128);
  v_compartment_id VARCHAR2(4000);
  v_attributes CLOB;
  v_attribute_json JSON_OBJECT_T := JSON_OBJECT_T();
BEGIN
  SELECT COUNT(*)
    INTO v_exists
    FROM user_cloud_ai_profiles
   WHERE profile_name = 'SGMP_POC_OCI_LLAMA4SCOUT';

  IF v_exists > 0 THEN
    DBMS_CLOUD_AI.DROP_PROFILE(
      profile_name => 'SGMP_POC_OCI_LLAMA4SCOUT',
      force        => TRUE
    );
  END IF;

  SELECT DBMS_LOB.SUBSTR(attribute_value, 128, 1)
    INTO v_credential_name
    FROM user_cloud_ai_profile_attributes
   WHERE profile_name = 'SGMP_POC_OCI_GPT54MINI'
     AND attribute_name = 'credential_name';

  SELECT DBMS_LOB.SUBSTR(attribute_value, 128, 1)
    INTO v_region
    FROM user_cloud_ai_profile_attributes
   WHERE profile_name = 'SGMP_POC_OCI_GPT54MINI'
     AND attribute_name = 'region';

  SELECT DBMS_LOB.SUBSTR(attribute_value, 4000, 1)
    INTO v_compartment_id
    FROM user_cloud_ai_profile_attributes
   WHERE profile_name = 'SGMP_POC_OCI_GPT54MINI'
     AND attribute_name = 'oci_compartment_id';

  v_attribute_json.put('provider', 'oci');
  v_attribute_json.put('credential_name', v_credential_name);
  v_attribute_json.put('model', 'meta.llama-4-scout-17b-16e-instruct');
  v_attribute_json.put('region', v_region);
  v_attribute_json.put('oci_compartment_id', v_compartment_id);
  v_attributes := v_attribute_json.to_clob;

  DBMS_CLOUD_AI.CREATE_PROFILE(
    profile_name => 'SGMP_POC_OCI_LLAMA4SCOUT',
    attributes   => v_attributes,
    description  => 'Non-operational Smilegate game-scope latency comparison'
  );

  FOR source_attribute IN (
    SELECT attribute_name, attribute_value
      FROM user_cloud_ai_profile_attributes
     WHERE profile_name = 'SGMP_POC_OCI_GPT54MINI'
       AND attribute_name NOT IN (
         'credential_name', 'model', 'provider', 'provider_endpoint',
         'region', 'oci_compartment_id', 'oci_endpoint_id',
         'oci_apiformat', 'oci_runtimetype'
       )
  ) LOOP
    DBMS_CLOUD_AI.SET_ATTRIBUTE(
      profile_name    => 'SGMP_POC_OCI_LLAMA4SCOUT',
      attribute_name  => source_attribute.attribute_name,
      attribute_value => source_attribute.attribute_value
    );
  END LOOP;
END;
/

SELECT attribute_name, attribute_value
  FROM user_cloud_ai_profile_attributes
 WHERE profile_name = 'SGMP_POC_OCI_LLAMA4SCOUT'
   AND attribute_name IN (
     'provider', 'model', 'credential_name', 'region', 'oci_compartment_id'
   )
 ORDER BY attribute_name;
