// This query reads the `profileType` and the `profileId` from the context
// and returns whether or not the Video is connected to a Channel that has indicated access via it's channelId or artistId attribute
MATCH(userProfile:Profile {profileType: $parameters.profileType, profileId: toInteger($parameters.profileId)})

UNWIND $keys AS id
MATCH (v:Video {id: id})
OPTIONAL MATCH (v)-[:APPEARS_IN_CHANNEL]->(channel)

CALL apoc.when(
    (v.videoContentType IS NOT NULL AND v.videoContentType = 'ART_TRACK_VIDEO') OR (channel.channelOwner IS NOT NULL AND channel.channelOwner = 'VEVO'),
    '
        OPTIONAL MATCH (v)<-[:REPRESENTS_VIDEO]-(:GlobalSoundRecording)-[:REPRESENTS]->(lsr:LabelSoundRecording)
        OPTIONAL MATCH labelPath2 = (userProfile)-[:HAS_ACCESS_TO|HAS_ADMIN_ACCESS_TO]->(:Vendor {id: lsr.vendorId})
        OPTIONAL MATCH subaccountPath2 = (userProfile)-[:HAS_ACCESS_TO|HAS_ADMIN_ACCESS_TO]->(:Subaccount {id: lsr.subaccountId})
        OPTIONAL MATCH (lsr)<-[:IS_PLACEMENT_OF]-(t:Track)
        OPTIONAL MATCH artistPath3 = (userProfile)-[:HAS_ACCESS_TO|HAS_ADMIN_ACCESS_TO]->(:LabelParticipant)-[:PARTICIPATED_IN]->(t)
        OPTIONAL MATCH artistPath4 = (userProfile)-[:HAS_ACCESS_TO|HAS_ADMIN_ACCESS_TO]->(:ArtistInfo)<-[:CREATED_FROM]-(:LabelParticipant)-[:PARTICIPATED_IN]->(t)
        RETURN (labelPath2 IS NOT NULL OR subaccountPath2 IS NOT NULL OR artistPath3 IS NOT NULL OR artistPath4 IS NOT NULL) AS isPartOfCatalog
    ',
    '
        OPTIONAL MATCH artistPath1 = (userProfile)-[:HAS_ACCESS_TO|HAS_ADMIN_ACCESS_TO]->(:LabelParticipant)-[:CREATED_FROM]->(:ArtistInfo {id: c.artistId})
        OPTIONAL MATCH artistPath2 = (userProfile)-[:HAS_ACCESS_TO|HAS_ADMIN_ACCESS_TO]->(:ArtistInfo {id: c.artistId})<-[:CREATED_FROM]-(:LabelParticipant)
        OPTIONAL MATCH labelPath = (userProfile)-[:HAS_ACCESS_TO|HAS_ADMIN_ACCESS_TO]->(:Vendor {id: c.labelId})
        OPTIONAL MATCH subaccountPath = (userProfile)-[:HAS_ACCESS_TO|HAS_ADMIN_ACCESS_TO]->(:Subaccount {id: c.subaccountId})
        RETURN (artistPath1 IS NOT NULL OR artistPath2 IS NOT NULL OR labelPath IS NOT NULL OR subaccountPath IS NOT NULL) AS isPartOfCatalog
    ',
    {
        c: channel,
        v: v,
        userProfile: userProfile
    }
)

YIELD value
WITH id, collect(value)[0] AS v
RETURN id, v.isPartOfCatalog
