-- Operational OCI Cohere profile for short game-name and scope extraction.
-- It inherits the active GPT profile's OCI credential, region, and metadata.

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

  IF v_exists > 0 THEN
    DBMS_CLOUD_AI.DROP_PROFILE(
      profile_name => 'SGMP_POC_OCI_COHERE_COMMAND',
      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 := JSON_OBJECT_T();
  v_attribute_json.put('provider', 'oci');
  v_attribute_json.put('credential_name', v_credential_name);
  v_attribute_json.put('model', 'cohere.command-latest');
  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_COHERE_COMMAND',
    attributes => v_attributes,
    description => 'Smilegate operational OCI Cohere Command profile for game scope extraction'
  );

  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_COHERE_COMMAND',
      attribute_name => source_attribute.attribute_name,
      attribute_value => source_attribute.attribute_value
    );
  END LOOP;
END;
/

SELECT profile_name, attribute_value AS model
  FROM user_cloud_ai_profile_attributes
 WHERE profile_name = 'SGMP_POC_OCI_COHERE_COMMAND'
   AND attribute_name = 'model';
