USE ROLE SAMOOHA_APP_ROLE;
USE SECONDARY ROLES none;
USE WAREHOUSE dcr_wh_small;

SET collaboration_name = 'SF_DEMO_SINGLE_ACCOUNT_OVERLAP';

CALL SAMOOHA_BY_SNOWFLAKE_LOCAL_DB.REGISTRY.VIEW_REGISTERED_DATA_OFFERINGS();

CALL SAMOOHA_BY_SNOWFLAKE_LOCAL_DB.REGISTRY.VIEW_REGISTERED_TEMPLATES();

CALL SAMOOHA_BY_SNOWFLAKE_LOCAL_DB.COLLABORATION.INITIALIZE(
    $$
    api_version: 2.0.0
    spec_type: collaboration
    name: SF_DEMO_SINGLE_ACCOUNT_OVERLAP
    owner: SINGLE_ACCOUNT
    collaborator_identifier_aliases:
      SINGLE_ACCOUNT: ORGNAME.ACCOUNT_NAME_1
    analysis_runners:
      SINGLE_ACCOUNT:
        data_providers:
          SINGLE_ACCOUNT:
            data_offerings:
              - id: SF_DEMO_EXPOSURES_v0
              - id: SF_DEMO_PURCHASES_v0
              - id: SF_DEMO_CUSTOMER_SPINE_v0
        templates:
          - id: SF_DEMO_AUDIENCE_OVERLAP_v0
          - id: SF_DEMO_STATE_SPEND_ANALYSIS_v0
    $$
);

CALL SAMOOHA_BY_SNOWFLAKE_LOCAL_DB.COLLABORATION.GET_STATUS($collaboration_name);

CALL SAMOOHA_BY_SNOWFLAKE_LOCAL_DB.COLLABORATION.JOIN($collaboration_name);

CALL SAMOOHA_BY_SNOWFLAKE_LOCAL_DB.COLLABORATION.GET_STATUS($collaboration_name);

CALL SAMOOHA_BY_SNOWFLAKE_LOCAL_DB.COLLABORATION.VIEW_DATA_OFFERINGS($collaboration_name);

CALL SAMOOHA_BY_SNOWFLAKE_LOCAL_DB.COLLABORATION.VIEW_TEMPLATES($collaboration_name);

CALL SAMOOHA_BY_SNOWFLAKE_LOCAL_DB.COLLABORATION.RUN(
    $collaboration_name,
    $$
    api_version: 2.0.0
    spec_type: analysis
    template: SF_DEMO_AUDIENCE_OVERLAP_v0
    template_configuration:
      view_mappings:
        source_tables:
          - SINGLE_ACCOUNT.SF_DEMO_PURCHASES_v0.purchases
          - SINGLE_ACCOUNT.SF_DEMO_EXPOSURES_v0.exposures
    $$
);

CALL SAMOOHA_BY_SNOWFLAKE_LOCAL_DB.COLLABORATION.RUN(
    $collaboration_name,
    $$
    api_version: 2.0.0
    spec_type: analysis
    template: SF_DEMO_STATE_SPEND_ANALYSIS_v0
    template_configuration:
      view_mappings:
        source_tables:
          - SINGLE_ACCOUNT.SF_DEMO_PURCHASES_v0.purchases
          - SINGLE_ACCOUNT.SF_DEMO_EXPOSURES_v0.exposures
          - SINGLE_ACCOUNT.SF_DEMO_CUSTOMER_SPINE_v0.customer_spine
    $$
);


CALL SAMOOHA_BY_SNOWFLAKE_LOCAL_DB.COLLABORATION.TEARDOWN($collaboration_name);

CALL SAMOOHA_BY_SNOWFLAKE_LOCAL_DB.COLLABORATION.GET_STATUS($collaboration_name);

CALL SAMOOHA_BY_SNOWFLAKE_LOCAL_DB.COLLABORATION.TEARDOWN($collaboration_name);

CALL SAMOOHA_BY_SNOWFLAKE_LOCAL_DB.COLLABORATION.GET_STATUS($collaboration_name);
