-- Example consumer code for provider-run provider activation.
-- See https://docs.snowflake.com/user-guide/cleanrooms/activation for details.

USE WAREHOUSE app_wh;
USE ROLE samooha_app_role;

SET cleanroom_name = 'test activation';
SET template_name = 'activation_provider_activation_template';

-- Install the clean room. If you re-run this after a successful installation,
-- allow it to complete or else you will corrupt the clean room.
CALL samooha_by_snowflake_local_db.consumer.install_cleanroom($cleanroom_name, '<PROVIDER_LOCATOR>');

-- Link your data into the clean room.
CALL samooha_by_snowflake_local_db.consumer.link_datasets($cleanroom_name, ['SAMOOHA_SAMPLE_DATABASE.DEMO.CUSTOMERS']);

-- Set the join policy.
CALL samooha_by_snowflake_local_db.consumer.set_join_policy($cleanroom_name, ['SAMOOHA_SAMPLE_DATABASE.DEMO.CUSTOMERS:HASHED_EMAIL']);

-- Set activation policy.
CALL samooha_by_snowflake_local_db.consumer.set_activation_policy($cleanroom_name, 
[ 
    $template_name || ':SAMOOHA_SAMPLE_DATABASE.DEMO.CUSTOMERS:STATUS',  
    $template_name || ':SAMOOHA_SAMPLE_DATABASE.DEMO.CUSTOMERS:AGE_BAND',
    $template_name || ':SAMOOHA_SAMPLE_DATABASE.DEMO.CUSTOMERS:HASHED_EMAIL']
    );

-- SWITCH TO PROVIDER, who will request permission to run a template.

-- Grant provider-run analysis --

-- Has provider requested provider-run templates? If so, allow it.
-- Optionally set warehouse limitations on the provider by calling set_provider_run_configuration.
CALL samooha_by_snowflake_local_db.library.is_provider_run_enabled($cleanroom_name);
CALL samooha_by_snowflake_local_db.consumer.enable_templates_for_provider_run($cleanroom_name, [$template_name], FALSE);

-- Grant provider activation --

-- Approve the provider's request to activate a template
CALL samooha_by_snowflake_local_db.consumer.approve_provider_activation_consent($cleanroom_name, $template_name);

-- SWITCH TO PROVIDER to run the query.

-- Clean up when done
CALL samooha_by_snowflake_local_db.consumer.uninstall_cleanroom($cleanroom_name);
