<?xml version="1.0" encoding="UTF-8"?>
<changelog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <changeset id="IN-16558_fix_josep_boada_labelparticipant_track_relations:1" author="aoshomoji" run-always="true">
        <precondition>
            <sqlquery>
                <![CDATA[
                    WITH events AS (
                        SELECT
                            RECORD_CONTENT:payload:start:ids:uuid::STRING AS label_participant_uuid,
                            RECORD_CONTENT:payload:end:ids:id::NUMBER AS track_id,
                            COALESCE(
                                RECORD_CONTENT:payload:after:properties:participated_as::STRING,
                                RECORD_CONTENT:payload:before:properties:participated_as::STRING
                            ) AS participated_as,
                            RECORD_CONTENT:payload:before:properties:sequenceNumber::NUMBER AS sequence_number,
                            RECORD_CONTENT:meta:timestamp AS ts,
                            RECORD_CONTENT:meta:timestamp::VARCHAR::TIMESTAMP::DATETIME AS dt,
                            RECORD_CONTENT:meta:operation::STRING AS operation,
                            CASE WHEN operation = 'deleted' THEN 1
                                 WHEN operation = 'created' THEN 2
                                 WHEN operation = 'updated' THEN 3
                            END AS transaction_event_order
                        FROM FACTS.PROD.CDC_MUSICGRAPH_PARTICIPATEDIN
                        WHERE RECORD_CONTENT:payload:end:ids:id IS NOT NULL AND RECORD_CONTENT:payload:start:ids:id IS NOT NULL
                            AND ARRAY_CONTAINS('LabelParticipant'::VARIANT, RECORD_CONTENT:payload:start:labels)
                            AND ARRAY_CONTAINS('Track'::VARIANT, RECORD_CONTENT:payload:end:labels)
                            AND ARRAY_CONTAINS('Orchard'::VARIANT, RECORD_CONTENT:payload:end:labels)
                    ),
                    deleted_performers AS (
                        SELECT DISTINCT label_participant_uuid, track_id, sequence_number
                        FROM events
                        WHERE label_participant_uuid IN ('edcc268a-5e7d-4cc9-ae72-d96f60ffb6a4', '07fe322b-4f12-40bc-8697-21aaf20e592f')
                        AND participated_as = 'performer'
                        AND operation = 'deleted'
                        AND dt::date >= '2025-08-01'
                        QUALIFY ROW_NUMBER() OVER (PARTITION BY label_participant_uuid, track_id, participated_as ORDER BY ts DESC, transaction_event_order DESC) = 1
                    ),
                    current_performers AS (
                        SELECT DISTINCT lp.label_participant_uuid, lp.track_id
                        FROM FACTS.PROD.LABEL_PARTICIPANT_PARTICIPATED_IN_ORCHARD_TRACK lp
                        WHERE lp.label_participant_uuid IN ('edcc268a-5e7d-4cc9-ae72-d96f60ffb6a4', '07fe322b-4f12-40bc-8697-21aaf20e592f')
                        AND lp.participated_as = 'performer'
                    )
                    SELECT DISTINCT dp.label_participant_uuid, dp.track_id, dp.sequence_number
                    FROM deleted_performers dp
                    LEFT JOIN current_performers cp ON cp.label_participant_uuid = dp.label_participant_uuid
                        AND cp.track_id = dp.track_id
                    WHERE cp.label_participant_uuid IS NULL
                        AND cp.track_id IS NULL;
                ]]>
            </sqlquery>
        </precondition>
        <cypherquery>
            <![CDATA[
                MATCH (lp:LabelParticipant {uuid: event.LABEL_PARTICIPANT_UUID})
                MATCH (t:Track:Orchard {id: toInteger(event.TRACK_ID)})
                MERGE (lp)-[participation:PARTICIPATED_IN {participated_as: 'performer'}]->(t)
                    ON CREATE SET
                        participation.sequenceNumber = toInteger(event.SEQUENCE_NUMBER),
                        participation.createdAt = datetime(),
                        participation.createdBy = 'database/IN-16558_fix_josep_boada_labelparticipant_track_relations',
                        participation.lastModifiedAt = datetime(),
                        participation.lastModifiedBy = 'database/IN-16558_fix_josep_boada_labelparticipant_track_relations',
                        participation.touchedByBackfill = datetime()
                    ON MATCH SET
                        participation.sequenceNumber = toInteger(event.SEQUENCE_NUMBER),
                        participation.lastModifiedAt = datetime(),
                        participation.lastModifiedBy = 'database/IN-16558_fix_josep_boada_labelparticipant_track_relations',
                        participation.touchedByBackfill = datetime()
                RETURN count(participation)
            ]]>
        </cypherquery>
    </changeset>
</changelog>
