MATCH (soundRecording:NrSoundRecording:NrOwnership { id: $id })
SET soundRecording.displayArtistName = $displayArtistName,
    soundRecording.lastModifiedAt = datetime(),
    soundRecording.lastModifiedBy = "graphql-neighbouring-rights/updateOwnershipSoundRecordingNrParticipants/" + $identityId
WITH soundRecording

CALL apoc.lock.nodes([soundRecording])

OPTIONAL MATCH (soundRecording)-[oldRel:HAS_PRIMARY_ARTIST|HAS_FEATURED_ARTIST]->(oldParticipant:NrParticipant:NrOwnership)
WITH soundRecording, collect(oldRel) as oldRelations, collect(oldParticipant) as oldParticipants

UNWIND $primaryArtists as primaryArtist
MERGE (primaryParticipant:NrParticipant:NrOwnership {name: primaryArtist})
ON CREATE SET
    primaryParticipant.id = randomUUID(),
    primaryParticipant.createdAt = datetime(),
    primaryParticipant.createdBy = "graphql-neighbouring-rights/updateOwnershipSoundRecordingNrParticipants/" + $identityId,
    primaryParticipant.lastModifiedAt = datetime(),
    primaryParticipant.lastModifiedBy = "graphql-neighbouring-rights/updateOwnershipSoundRecordingNrParticipants/" + $identityId
ON MATCH SET
    primaryParticipant.lastModifiedAt = datetime(),
    primaryParticipant.lastModifiedBy = "graphql-neighbouring-rights/updateOwnershipSoundRecordingNrParticipants/" + $identityId

WITH soundRecording, oldRelations, oldParticipants, collect(primaryParticipant) as newParticipants
CALL {
    WITH soundRecording, oldRelations, oldParticipants, newParticipants
    UNWIND $featuredArtists as featuredArtist
    MERGE (featuredParticipant:NrParticipant:NrOwnership {name: featuredArtist})
    ON CREATE SET
        featuredParticipant.id = randomUUID(),
        featuredParticipant.createdAt = datetime(),
        featuredParticipant.createdBy = "graphql-neighbouring-rights/updateOwnershipSoundRecordingNrParticipants/" + $identityId,
        featuredParticipant.lastModifiedAt = datetime(),
        featuredParticipant.lastModifiedBy = "graphql-neighbouring-rights/updateOwnershipSoundRecordingNrParticipants/" + $identityId
    ON MATCH SET
        featuredParticipant.lastModifiedAt = datetime(),
        featuredParticipant.lastModifiedBy = "graphql-neighbouring-rights/updateOwnershipSoundRecordingNrParticipants/" + $identityId
    RETURN collect(featuredParticipant) as newFeaturedParticipants
}
WITH soundRecording, oldRelations, oldParticipants, newParticipants, newFeaturedParticipants

CALL apoc.lock.nodes(oldParticipants + newParticipants + newFeaturedParticipants)

CALL {
    WITH oldRelations
    FOREACH (r IN oldRelations | DELETE r)
}

WITH soundRecording, newParticipants, newFeaturedParticipants

UNWIND newParticipants as newParticipant
MERGE (soundRecording)-[primaryRel:HAS_PRIMARY_ARTIST]->(newParticipant)
SET
    primaryRel.lastModifiedAt = datetime(),
    primaryRel.lastModifiedBy = "graphql-neighbouring-rights/updateOwnershipSoundRecordingNrParticipants/" + $identityId
WITH soundRecording, newFeaturedParticipants

CALL {
    WITH soundRecording, newFeaturedParticipants
    UNWIND newFeaturedParticipants as newFtParticipant
    MERGE (soundRecording)-[featuredRel:HAS_FEATURED_ARTIST]->(newFtParticipant)
    SET
        featuredRel.lastModifiedAt = datetime(),
        featuredRel.lastModifiedBy = "graphql-neighbouring-rights/updateOwnershipSoundRecordingNrParticipants/" + $identityId
}
WITH soundRecording

RETURN soundRecording.id AS soundRecordingId
