-- Example consumer code for consumer-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 consumer activation';
SET template_name = 'consumer_run_provider_activation_template';

-- Install 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']
    );

-- Run the activation.
-- Use a different segment name for each run to distinguish results between runs, if desired.
-- Don't forget the FALSE last argument to specify that this is a provider activation.
SET segment_name = 'my_consumer_run_provider_activation_segment';
CALL samooha_by_snowflake_local_db.consumer.run_activation(
  $cleanroom_name,
  $segment_name,
  $template_name,
  ['SAMOOHA_SAMPLE_DATABASE.DEMO.CUSTOMERS'], 
  ['SAMOOHA_SAMPLE_DATABASE.DEMO.CUSTOMERS'],
  object_construct(
    'c_join_col', 'c.hashed_email',
    'p_join_col', 'p.hashed_email'
  ),
  FALSE);

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