<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
                   xmlns:neo4j="http://www.liquibase.org/xml/ns/dbchangelog-ext"
                   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">

    <changeSet id="WAR-2835_vendor_star_access:1" author="menger">
        <neo4j:cypher>
            <![CDATA[
            MATCH (identity:Identity {id: 'd0ff66e0-7a32-4b14-9819-756f56a11ad5'})
            MERGE (increment:IncrementId {nodeName: 'Profile'})
                ON CREATE SET increment.id = 2
                ON MATCH SET increment.id = increment.id + 1
            CREATE (profile:Profile {
                profileId: (increment.id),
                profileType: 'MoneyhubProfile',
                profileName: identity.name,
                brand: identity.defaultBrand,
                fullCatalogAccess: true,
                roles: ['accounting'],
                uuid: apoc.create.uuid(),
                createdAt: datetime(),
                lastModifiedAt: datetime(),
                createdBy: 'database/WAR-2835_vendor_star_access:1',
                lastModifiedBy: 'database/WAR-2835_vendor_star_access:1'
            })
            MERGE (identity)-[:HAS_PROFILE]->(profile)
            WITH profile
            MATCH (vendor:Vendor {id: '*'})
            MERGE (profile)-[:HAS_ACCESS_TO {
                createdAt: datetime(),
                createdBy: 'database/WAR-2835_vendor_star_access:1',
                lastModifiedAt: datetime(),
                lastModifiedBy: 'database/WAR-2835_vendor_star_access:1'
            }]->(vendor)
            ]]>
        </neo4j:cypher>
    </changeSet>

    <changeSet id="WAR-2835_vendor_star_access:2" author="menger">
        <neo4j:cypher>
            <![CDATA[
            MATCH (identity:Identity {id: '5bbcee1b-fbc6-4171-8989-a87febedc1a1'})
            MERGE (increment:IncrementId {nodeName: 'Profile'})
                ON CREATE SET increment.id = 2
                ON MATCH SET increment.id = increment.id + 1
            CREATE (profile:Profile {
                profileId: (increment.id),
                profileType: 'MoneyhubProfile',
                profileName: identity.name,
                brand: identity.defaultBrand,
                fullCatalogAccess: true,
                roles: ['accounting'],
                uuid: apoc.create.uuid(),
                createdAt: datetime(),
                lastModifiedAt: datetime(),
                createdBy: 'database/WAR-2835_vendor_star_access:2',
                lastModifiedBy: 'database/WAR-2835_vendor_star_access:2'
            })
            MERGE (identity)-[:HAS_PROFILE]->(profile)
            WITH profile
            MATCH (vendor:Vendor {id: '*'})
            MERGE (profile)-[:HAS_ACCESS_TO {
                createdAt: datetime(),
                createdBy: 'database/WAR-2835_vendor_star_access:2',
                lastModifiedAt: datetime(),
                lastModifiedBy: 'database/WAR-2835_vendor_star_access:2'
            }]->(vendor)
            ]]>
        </neo4j:cypher>
    </changeSet>

    <changeSet id="WAR-2835_vendor_star_access:3" author="menger">
        <neo4j:cypher>
            <![CDATA[
            MATCH (identity:Identity {id: 'c85362cb-175e-4c21-82ff-bcfbf9c87697'})
            MERGE (increment:IncrementId {nodeName: 'Profile'})
                ON CREATE SET increment.id = 2
                ON MATCH SET increment.id = increment.id + 1
            CREATE (profile:Profile {
                profileId: (increment.id),
                profileType: 'MoneyhubProfile',
                profileName: identity.name,
                brand: identity.defaultBrand,
                fullCatalogAccess: true,
                roles: ['accounting'],
                uuid: apoc.create.uuid(),
                createdAt: datetime(),
                lastModifiedAt: datetime(),
                createdBy: 'database/WAR-2835_vendor_star_access:3',
                lastModifiedBy: 'database/WAR-2835_vendor_star_access:3'
            })
            MERGE (identity)-[:HAS_PROFILE]->(profile)
            WITH profile
            MATCH (vendor:Vendor {id: '*'})
            MERGE (profile)-[:HAS_ACCESS_TO {
                createdAt: datetime(),
                createdBy: 'database/WAR-2835_vendor_star_access:3',
                lastModifiedAt: datetime(),
                lastModifiedBy: 'database/WAR-2835_vendor_star_access:3'
            }]->(vendor)
            ]]>
        </neo4j:cypher>
    </changeSet>

    <changeSet id="WAR-2835_vendor_star_access:4" author="menger">
        <neo4j:cypher>
            <![CDATA[
            MATCH (identity:Identity {id: 'e2b00787-34eb-4ba7-92d7-6e8913fbaea2'})
            MERGE (increment:IncrementId {nodeName: 'Profile'})
                ON CREATE SET increment.id = 2
                ON MATCH SET increment.id = increment.id + 1
            CREATE (profile:Profile {
                profileId: (increment.id),
                profileType: 'MoneyhubProfile',
                profileName: identity.name,
                brand: identity.defaultBrand,
                fullCatalogAccess: true,
                roles: ['accounting'],
                uuid: apoc.create.uuid(),
                createdAt: datetime(),
                lastModifiedAt: datetime(),
                createdBy: 'database/WAR-2835_vendor_star_access:4',
                lastModifiedBy: 'database/WAR-2835_vendor_star_access:4'
            })
            MERGE (identity)-[:HAS_PROFILE]->(profile)
            WITH profile
            MATCH (vendor:Vendor {id: '*'})
            MERGE (profile)-[:HAS_ACCESS_TO {
                createdAt: datetime(),
                createdBy: 'database/WAR-2835_vendor_star_access:4',
                lastModifiedAt: datetime(),
                lastModifiedBy: 'database/WAR-2835_vendor_star_access:4'
            }]->(vendor)
            ]]>
        </neo4j:cypher>
    </changeSet>

    <changeSet id="WAR-2835_vendor_star_access:5" author="menger">
        <neo4j:cypher>
            <![CDATA[
            MATCH (identity:Identity {id: 'b721b3ca-8ff8-4290-969a-20e39c365c86'})
                -[:HAS_PROFILE]->(profile:Profile {profileType: 'MoneyhubProfile'})
                -[old_relationship:DELETED_HAS_ACCESS_TO]->(vendor:Vendor {id: '*'})
            MERGE (increment:IncrementId {nodeName: 'Profile'})
                ON CREATE SET increment.id = 2
                ON MATCH SET increment.id = increment.id + 1
            DELETE old_relationship
            MERGE (profile)-[:HAS_ACCESS_TO {
                createdAt: datetime(),
                createdBy: 'database/WAR-2835_vendor_star_access:5',
                lastModifiedAt: datetime(),
                lastModifiedBy: 'database/WAR-2835_vendor_star_access:5'
            }]->(vendor)
            ]]>
        </neo4j:cypher>
    </changeSet>

</databaseChangeLog>
