<?xml version="1.0" encoding="UTF-8"?>
<CustomMetadata xmlns="http://soap.sforce.com/2006/04/metadata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <label>Scheduler: Double Booking</label>
    <protected>false</protected>
    <values>
        <field>b3s__Configuration__c</field>
        <value xsi:type="xsd:string"><![CDATA[
        
        ]]></value>
    </values>
    <values>
        <field>b3s__Javascript_Code__c</field>
        <value xsi:type="xsd:string"><![CDATA[
async ({ event, events, contactId, loaders, matchingModifiers, EvaluationResult }) => {
    let results = [];
    if (!contactId) {
        console.warn('No contact id for evaluation');
        return results;
    }

    // 1. Parse current shift date boundaries
    const currentStart = new Date(event.start).getTime();
    const currentEnd = new Date(event.end).getTime();

    // Guard clause: ensure valid date ranges on contextual event
    if (isNaN(currentStart) || isNaN(currentEnd) || currentStart >= currentEnd) {
        return results;
    }

    // 2. Iterate through all other memory-cached events to find collisions
    for (const loadedEvent of events) {
        // Skip comparing the event against itself
        if (loadedEvent.id === event.id) {
            continue;
        }

        // Filter: Only check for collisions against confirmed 'shift' types, ignoring availability requests
        if (!loadedEvent.title || loadedEvent.title.toLowerCase() !== 'shift') {
            continue;
        }

        // Extract the contact identifier from the loaded event
        const loadedEventContactId = loadedEvent.extendedProps?.record?.b3s__Contact__c; 
        
        // Only evaluate overlaps if the loaded shift is assigned to the same worker
        if (!loadedEventContactId || loadedEventContactId !== contactId) {
            continue;
        }

        // Parse loaded event date boundaries
        const loadedStart = new Date(loadedEvent.start).getTime();
        const loadedEnd = new Date(loadedEvent.end).getTime();

        if (isNaN(loadedStart) || isNaN(loadedEnd)) {
            continue;
        }

        // 3. Evaluate Overlap Condition (Standard Intersect Theory)
        // Two intervals [A, B] and [C, D] overlap if (A < D) and (B > C)
        const hasOverlap = currentStart < loadedEnd && currentEnd > loadedStart;

        if (hasOverlap) {
            const shiftName = loadedEvent.extendedProps?.record?.Name || 'Another Confirmed Shift';
            
            results.push(
                new EvaluationResult({
                    event: event,
                    detail: `🚫 Schedule Conflict: Overlaps with ${shiftName}`,
                    category: 'Availability',
                    points: -100,
                    isBlocking: false // Hard-blocks assignment over actual shifts
                })
            );
            
            // Break early since a single confirmed overlap is enough to block assignment
            break;
        }
    }

    return results;
}

        ]]></value>
    </values>
    <values>
        <field>b3s__Type__c</field>
        <value xsi:type="xsd:string">Scheduling</value>
    </values>
</CustomMetadata>
