<?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-2306_add_vendor_star_access:1" author="epohrebna">
        <neo4j:cypher>
            <![CDATA[
            UNWIND [
                'abatura@theorchard.com',
                'elizabete.kolbjer@theorchard.com',
                'jake.matthews@awal.com'
            ] AS userEmail
            MATCH (identity:Identity {email: userEmail})-[:HAS_PROFILE]->(profile:Profile {profileType: 'MoneyhubProfile'})
            MATCH (vendor:Vendor {id: '*'})
            SET profile.fullCatalogAccess = true,
                profile.lastModifiedAt = datetime(),
                profile.lastModifiedBy = 'database/"WAR-2306_add_vendor_star_access:1'
            MERGE (profile)-[:HAS_ACCESS_TO]->(vendor)
            ]]>
        </neo4j:cypher>
    </changeSet>

    <changeSet id="WAR-2306_add_vendor_star_access:2" author="epohrebna">
        <neo4j:cypher>
            <![CDATA[
            WITH [
                'adavis@theorchard.com',
                'cleclerc@theorchard.com',
                'cora@theorchard.com',
                'dpassariello@theorchard.com',
                'gbrito@theorchard.com',
                'kleeds@theorchard.com',
                'ktuohy@theorchard.com',
                'pedwards@theorchard.com',
                'rrighthand@theorchard.com'
            ] AS emails
            UNWIND emails AS email
            MATCH (identity:Identity {email: email})
            WITH collect(identity) AS identities
            WITH identities, size(identities) AS totalRows
            UNWIND range(0, totalRows - 1) AS rowNumber
            WITH totalRows, rowNumber, identities[rowNumber] AS identity
            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 - totalRows + rowNumber + 1,
                profileType: 'MoneyhubProfile',
                profileName: identity.name,
                brand: identity.defaultBrand,
                fullCatalogAccess: true,
                roles: ['accounting'],
                uuid: apoc.create.uuid(),
                createdAt: datetime(),
                lastModifiedAt: datetime(),
                createdBy: 'database/WAR-2306_add_vendor_star_access:2',
                lastModifiedBy: 'database/WAR-2306_add_vendor_star_access:2'
            })
            MERGE (identity)-[:HAS_PROFILE]->(profile)
            WITH profile
            MATCH (vendor:Vendor {id: '*'})
            MERGE (profile)-[:HAS_ACCESS_TO]->(vendor);
            ]]>
        </neo4j:cypher>
    </changeSet>
</databaseChangeLog>
