<?xml version="1.0" encoding="UTF-8"?>
<changelog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:/var/app/InputFileSchema.xsd">
    <changeset id="SR-904_subaccount" author="nlittmann" run-always="true">
        <precondition>
            <sqlquery>
                <![CDATA[
                    WITH filtered_subaccount_ids AS (
                        SELECT DISTINCT(dp.subaccountid) AS subaccount_id
                        FROM FACTS.PROD.DIM_TRACK_CLEAN_MV dtc
                        INNER JOIN FACTS.PROD.DIM_RELEASE dr 
                            ON dtc.upc = dr.releaseid
                        INNER JOIN FACTS.PROD.DIM_PROJECT dp 
                            ON dr.projectid = dp.projectid
                        INNER JOIN FACTS.PROD.SUBACCOUNT s
                            ON dp.subaccountid = s.id
                        WHERE 
                            dtc.track_unique_id IN (SELECT DISTINCT(tuid) FROM FACTS.PROD.REGISTRY)
                        EXCEPT (
                            SELECT DISTINCT(ORCHARD_OBJ_ID) AS subaccount_id 
                            FROM FACTS.PROD.HAS_FINGERPRINT_RULE
                            WHERE ORCHARD_OBJ_TYPE = 'Subaccount'
                            AND service = 'youtube'
                        )
                    ),
                    territories_in_registry AS (
                        SELECT DISTINCT(territory) AS TERRITORY FROM FACTS.PROD.REGISTRY
                    ),
                    carveouts_per_territory AS (
                        SELECT
                            str.SUBACCOUNT_ID AS subaccount_id,
                            cou.COUNTRY_CODE AS territory_carveouts
                        FROM
                            ORCHARD_APP_REPORTING_V2.ART_RELATIONS_PROD_ART_RELATIONS.SUBACCOUNT_TERRITORY_RESTRICTION AS str
                            INNER JOIN ORCHARD_APP_REPORTING_V2.ART_RELATIONS_PROD_ART_RELATIONS.COUNTRY AS cou ON cou.ID = str.COUNTRY_ID
                            INNER JOIN territories_in_registry tir ON cou.COUNTRY_CODE = tir.TERRITORY
                        WHERE
                            str.subaccount_id IN (SELECT subaccount_id FROM filtered_subaccount_ids)
                        GROUP BY
                            1, 2
                    ),
                    carveouts_per_service_per_territory AS (
                        SELECT
                            sar.SUBACCOUNT_ID AS subaccount_id,
                            cou.COUNTRY_CODE AS territory_carveouts
                        FROM
                            ORCHARD_APP_REPORTING_V2.ART_RELATIONS_PROD_ART_RELATIONS.SUBACCOUNT_DMS_RESTRICTION AS sar
                            INNER JOIN ORCHARD_APP_REPORTING_V2.ART_RELATIONS_PROD_ART_RELATIONS.CUSTOMER_MASTER AS cm 
                                ON sar.DMS_ID = cm.CUSTOMER_ID
                            INNER JOIN ORCHARD_APP_REPORTING_V2.ART_RELATIONS_PROD_ART_RELATIONS.COUNTRY AS cou 
                                ON cou.ID = cm.TERRITORY
                            INNER JOIN territories_in_registry tir 
                                ON cou.COUNTRY_CODE = tir.TERRITORY
                            WHERE
                                cm.CUSTOMER_MASTER_MASTER_ID = 453
                                AND sar.subaccount_id in (SELECT subaccount_id FROM filtered_subaccount_ids)
                        GROUP BY
                            1, 2
                    )
                    SELECT * FROM (
                        SELECT * FROM carveouts_per_territory
                        UNION
                        SELECT * FROM carveouts_per_service_per_territory
                    )
                    GROUP BY 1, 2
                    ORDER BY 1, 2
                    ;
                ]]>
            </sqlquery>
        </precondition>
        <cypherquery>
            <![CDATA[
                MATCH(subaccount:Subaccount),(rule:FingerprintRule)
                WHERE subaccount.id = event.SUBACCOUNT_ID
                    AND rule.territory = event.TERRITORY_CARVEOUTS
                    AND rule.service = 'youtube'
                    AND rule.policy = 'carveout'
                MERGE (subaccount)-[rel:HAS_FINGERPRINT_RULE]->(rule)
                SET
                    rel.createdAt = datetime(),
                    rel.createdBy = 'database/SR-915_test_2/179/OrchAdminProfile',
                    rel.lastModifiedAt = datetime(),
                    rel.lastModifiedBy = 'database/SR-915_test_2/179/OrchAdminProfile'
                RETURN rel
            ]]>
        </cypherquery>
    </changeset>
</changelog>