-- Basic Snowflake Collaboration Data Clean Rooms example.
-- This file represents user "bob" in a two-collaborator clean room example.

-- Run this worksheet in a Snowflake account with access to the latest version of
-- Snowflake Data Clean Rooms.

-- This file  demonstrates the following actions:
-- * Joining a collaboration
-- * Registering and adding a template and a data offering to an existing collaboration.
-- * Running an analysis.

-- For more information, read docs.snowflake.com/user-guide/cleanrooms/v2/using

USE WAREHOUSE APP_WH;
USE ROLE SAMOOHA_APP_ROLE;

-- Secondary roles can't be active when calling join or link_data_offering.
USE SECONDARY ROLES NONE;

-- Create a template.
CALL samooha_by_snowflake_local_db.registry.register_template(
    $$
    api_version: 2.0.0
    spec_type: template
    name: limit_10_template
    version: 2026_01_12_V1
    type: sql_analysis
    description: test_description
    template:
      SELECT * FROM IDENTIFIER({{ SOURCE_TABLE[0] }}) LIMIT 10;
    $$
);
SET limit_10_template = 'limit_10_template_2026_01_12_V1'; --'<template_id>';

-- See which collaborations you are invited to, or have joined.
CALL samooha_by_snowflake_local_db.collaboration.view_collaborations();

-- Use SOURCE_NAME column value from the response to view_collaborations().
SET collaboration_id = '<collaboration_id>';

-- Use OWNER_ACCOUNT column value from the response to view_collaborations().
SET collaborator_data_sharing_id = '<collaborator_id>';

-- Review and join the collaboration.
-- Joining is asynchronous, so you must call get_status until the status is JOINED before
-- you can perform actions on the collaboration.
CALL samooha_by_snowflake_local_db.collaboration.review($collaboration_id, $collaborator_data_sharing_id);
CALL samooha_by_snowflake_local_db.collaboration.join($collaboration_id);
CALL samooha_by_snowflake_local_db.collaboration.get_status($collaboration_id);

-- Offer to share a template with alice but not yourself.
-- Alice enabled auto-approve on her account, so this request will
-- be auto-approved, and the template will be added immediately.
CALL samooha_by_snowflake_local_db.collaboration.add_template_request($collaboration_id, $limit_10_template, ['alice']);
CALL samooha_by_snowflake_local_db.collaboration.view_update_requests($collaboration_id);

-- View templates that you can use in this collaboration. Notice that you can only see your own templates because
-- the initial collaboration spec permitted the other template only to alice, not to you (bob).
CALL samooha_by_snowflake_local_db.collaboration.view_templates($collaboration_id);

-- SWITCH TO other account and request adding a template, and then come back to approve the request.

-- You haven't enabled template auto-approve, so you must approve the request before the template is added.
CALL samooha_by_snowflake_local_db.collaboration.view_update_requests($collaboration_id);
CALL samooha_by_snowflake_local_db.collaboration.approve_update_request(
  $collaboration_id,
  '<request_ID>'
);

-- Register your own data offering.
CALL samooha_by_snowflake_local_db.registry.register_data_offering(
    $$
    api_version: 2.0.0
    spec_type: data_offering
    version: v1
    name: bob_data
    datasets:
     - alias: my_customer_list
       data_object_fqn: SAMOOHA_SAMPLE_DATABASE.DEMO.CUSTOMERS_2
       object_class: custom
       allowed_analyses: template_only
       schema_and_template_policies:
         hashed_email:
           category: join_custom
         status:
           category: passthrough
    $$
);

SET my_data_id = '<data offering id>';

-- Share the data offering with yourself and alice.
CALL samooha_by_snowflake_local_db.collaboration.link_data_offering(
  $collaboration_id,
  $my_data_id,
  ['alice', 'bob']
);

-- Run an analysis with your template.
CALL samooha_by_snowflake_local_db.collaboration.run(
  $collaboration_id,
  $limit_10_template,
  [
    $my_data_id || '.my_customer_list',
  ],
  [], -- No local data needed.
  {}  -- No template arguments needed.
);
  
-- SWITCH TO other account to run an analysis.

-- Try running an analysis using the template that you shared with alice but not yourself.
-- This will fail, because you aren't listed as an analysis
-- runner for this template.
CALL samooha_by_snowflake_local_db.collaboration.run(
  $collaboration_id,
  $limit_10_template,
  [
    $my_data_id || '.my_customer_list'
  ],
  [],
  {}
  );
