<?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-3508_vendor_star_access:1" author="menger">
        <neo4j:cypher>
            <![CDATA[
            MATCH (identity:Identity)
                -[:HAS_PROFILE]->(:Profile {profileType: 'MoneyhubProfile'})
                -[deleted_access:DELETED_HAS_ACCESS_TO]->(:Vendor {id: '*'})
            WHERE identity.id IN [
                "07993440-3848-4f52-bb38-71d12a7cf647",
                "abfd524d-f012-473a-a05a-16ed0a1c6d74",
                "e49e65bf-b437-4c51-8b48-07a653f0ce55",
                "c45263d3-8a32-4d13-8cbf-5344134b96ff",
                "2e797f64-7654-4096-b830-2e2191a22f6f"
            ]
            DELETE deleted_access
            ]]>
        </neo4j:cypher>
    </changeSet>

    <changeSet id="WAR-3508_vendor_star_access:2" author="menger">
        <neo4j:cypher>
            <![CDATA[
            MATCH (identity:Identity)
                -[:HAS_PROFILE]->(profile:Profile {profileType: 'MoneyhubProfile'}),
                (vendor:Vendor {id: '*'})
            WHERE identity.id IN [
                "07993440-3848-4f52-bb38-71d12a7cf647",
                "abfd524d-f012-473a-a05a-16ed0a1c6d74",
                "c45263d3-8a32-4d13-8cbf-5344134b96ff",
                "2e797f64-7654-4096-b830-2e2191a22f6f"
            ]
            MERGE (profile)-[:HAS_ACCESS_TO]->(vendor)
            SET profile.lastModifiedAt = datetime(),
                profile.lastModifiedBy = 'database/WAR-3508_vendor_star_access:2';
            ]]>
        </neo4j:cypher>
    </changeSet>

    <changeSet id="WAR-3508_vendor_star_access:3" author="menger">
        <neo4j:cypher>
            <![CDATA[
            MATCH (identity:Identity {id: 'cc09c751-8167-43c2-ae8c-3d244906d8c3'})
            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-3508_vendor_star_access:3',
                lastModifiedBy: 'database/WAR-3508_vendor_star_access:3'
            })
            MERGE (identity)-[:HAS_PROFILE]->(profile)
            WITH profile
            MATCH (vendor:Vendor {id: '*'})
            MERGE (profile)-[:HAS_ACCESS_TO]->(vendor)
            ]]>
        </neo4j:cypher>
    </changeSet>

</databaseChangeLog>
