<?xml version="1.0" encoding="UTF-8"?>
<changelog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.liquigraph.org/schema/1.0/liquigraph.xsd">
    <changeset id="PLATFORM-2509-merge-artistprofile-to-insightsprofile:1" author="rsatsangi" run-on-change="true">
        <query>
            <![CDATA[
            MATCH (i:Identity)-[i_ap_rel:HAS_PROFILE]->(ap:Profile { profileType: 'ArtistProfile' })
            WHERE NOT (i:Identity)-[:HAS_PROFILE]->(:Profile { profileType: 'InsightsProfile' })
            WITH i
            MATCH (increment:IncrementId {nodeName: 'Profile'})
            WITH i, increment
            MERGE (ip:Profile {
                profileId: increment.id,
                profileType: 'InsightsProfile',
                profileName: apoc.text.join(['InsightsProfile', i.name], ' '),
                roles: ['analytics'],
                createdOn: datetime(),
                createdBy: 'PLATFORM-2509'
            })
            SET increment.id = increment.id + 1
            WITH i, ip
            CALL apoc.merge.relationship(i, 'HAS_PROFILE', NULL, NULL, ip, NULL) YIELD rel as rel
            RETURN i.id;
            ]]>
        </query>
        <query>
            <![CDATA[
            MATCH (i:Identity)-[iap_rel:HAS_PROFILE]->(ap:Profile { profileType: 'ArtistProfile' })
            WHERE 'analytics' IN ap.roles
            WITH i, iap_rel, collect(ap) as artist_profiles
                MATCH (i:Identity)-[:HAS_PROFILE]->(ip:Profile { profileType: 'InsightsProfile' })
            WITH i, iap_rel, artist_profiles, ip
            CALL apoc.refactor.setType(iap_rel, apoc.text.join(['DELETED_', apoc.rel.type(iap_rel)], '')) YIELD input, output as iap_rel_deleted
            SET iap_rel_deleted.deletedOn = datetime()
            SET iap_rel_deleted.deletedBy = 'PLATFORM-2509'
            WITH i, iap_rel, artist_profiles, ip
            UNWIND artist_profiles as ap
                MATCH (ap)-[ap_rel]->(ap_rel_node)
                    WHERE substring(apoc.rel.type(ap_rel), 0, 8) <> 'DELETED_'
                    AND NOT (ip)-[:HAS_FOLLOWED|:DELETED_HAS_FOLLOWED]->(ap_rel_node)
                WITH i, iap_rel, ip, ap, ap_rel, ap_rel_node
                CALL apoc.merge.relationship(
                    ip,
                    apoc.rel.type(ap_rel),
                    apoc.any.properties(ap_rel),
                    {
                        createdOn: datetime(),
                        createdBy: 'PLATFORM-2509'
                    },
                    ap_rel_node,
                    {
                        updatedOn: datetime(),
                        updatedBy: 'PLATFORM-2509'
                    }
                ) YIELD rel as ip_merged_rel
                WITH i, iap_rel, ip, ap, ap_rel, ap_rel_node, ip_merged_rel
                CALL apoc.refactor.setType(ap_rel, apoc.text.join(['DELETED_', apoc.rel.type(ap_rel)], '')) YIELD input, output as ap_rel_deleted
                WITH i, iap_rel, ip, ap, ap_rel, ap_rel_node, ip_merged_rel, ap_rel_deleted
                SET ap_rel_deleted.deletedOn = datetime()
                SET ap_rel_deleted.deletedBy = 'PLATFORM-2509'
                SET ip_merged_rel.createdOn = datetime()
                SET ip_merged_rel.createdBy = 'PLATFORM-2509'
                WITH *
            RETURN i.id;
            ]]>
        </query>
        <query>
            <![CDATA[
            MATCH (i:Identity)-[:DELETED_HAS_PROFILE]->(ap:Profile { profileType: 'ArtistProfile' })
            WHERE 'analytics' IN ap.roles
            WITH i, collect(ap) as artist_profiles
                MATCH (i:Identity)-[:HAS_PROFILE]->(ip:Profile { profileType: 'InsightsProfile' })
            WITH i, artist_profiles, ip
            UNWIND artist_profiles as ap
                MATCH (ap)-[ap_rel]->(ap_rel_node)
                    WHERE substring(apoc.rel.type(ap_rel), 0, 8) <> 'DELETED_'
                    AND (ip)-[:HAS_FOLLOWED|:DELETED_HAS_FOLLOWED]->(ap_rel_node)
                WITH i, ap, ap_rel, ap_rel_node
                CALL apoc.refactor.setType(ap_rel, apoc.text.join(['DELETED_', apoc.rel.type(ap_rel)], '')) YIELD input, output as ap_rel_deleted
                SET ap_rel_deleted.deletedOn = datetime()
                SET ap_rel_deleted.deletedBy = 'PLATFORM-2509'
                WITH *
            RETURN i.id;
            ]]>
        </query>
    </changeset>
</changelog>
