<?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>App: Double Booking</label>
    <protected>false</protected>
    <values>
        <field>b3s__Configuration__c</field>
        <value xsi:nil="true"/>
    </values>
    <values>
        <field>b3s__Javascript_Code__c</field>
        <value xsi:type="xsd:string"><![CDATA[
async ({ event, events, contactId, loaders, EvaluationResult }) => {
    if (!contactId) {
        console.warn('No contact id for evaluation');
        return [];
    }
    console.log('App: Double Booking: event', event);
    console.log('App: Double Booking: events', events);
    console.log('App: Double Booking: loaders', loaders);
    
    const workerShifts = loaders.find(l => l.name == 'candidateShifts').loader.records;    
    console.log('App: Double Booking: candidateShifts', workerShifts);
    
    const checkOverlap = (shift, otherShifts) => {
        return otherShifts.some((otherShift) => {
            const shiftStart = new Date(shift.extendedProps.b3s__Scheduled_Start_Time__c).getTime();
            const shiftEnd = new Date(shift.extendedProps.b3s__Scheduled_End_Time__c).getTime();

            const otherShiftStart = new Date(
                otherShift.b3s__Scheduled_Start_Time__c,
            ).getTime();

            const otherShiftEnd = new Date(otherShift.b3s__Scheduled_End_Time__c).getTime();

            return (
                (otherShiftStart >= shiftStart && otherShiftStart <= shiftEnd) ||  // other starts within main
                (otherShiftEnd >= shiftStart && otherShiftEnd <= shiftEnd) ||      // other ends within main
                (otherShiftStart <= shiftStart && otherShiftEnd >= shiftEnd) ||    // other entirely contains main
                (shiftStart <= otherShiftEnd && shiftEnd >= otherShiftStart)       // partial overlap
            );

        });
    };

    if (checkOverlap(event, workerShifts)) {
        return [
            new EvaluationResult({
                event: event,
                detail: '🚫 Double booked',
                category: 'Availability',
                points: -50,
                isBlocking: false
            }),
        ];
    }
    return [];
}


        ]]></value>
    </values>
    <values>
        <field>b3s__Type__c</field>
        <value xsi:type="xsd:string">Clock In / Out Component</value>
    </values>
</CustomMetadata>
