<?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: Availability</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, matchingModifiers, EvaluationResult }) => {
    let results = [];
    if (!contactId) {
        console.warn('No contact id for evaluation');
        return results;
    }

    const currentStart = new Date(event.start).getTime();
    const currentEnd = new Date(event.end).getTime();

    if (isNaN(currentStart) || isNaN(currentEnd) || currentStart >= currentEnd) {
        return results;
    }

    let availabilityMatchFound = false;

    // Iterate through loaded memory cached events to look for "request" type records covering the shift range
    for (const loadedEvent of events) {
        if (loadedEvent.id === event.id) {
            continue;
        }

        // Target availability request types only
        if (!loadedEvent.title || loadedEvent.title.toLowerCase() !== 'request') {
            continue;
        }

        // Verify the request record belongs to the same contact being evaluated
        const requestContactId = loadedEvent.extendedProps?.record?.b3s__Contact__c;
        if (!requestContactId || requestContactId !== contactId) {
            continue;
        }

        const requestStart = new Date(loadedEvent.start).getTime();
        const requestEnd = new Date(loadedEvent.end).getTime();

        if (isNaN(requestStart) || isNaN(requestEnd)) {
            continue;
        }

        // Check if the shift falls within or intersects with the availability record window
        const hasOverlap = currentStart < requestEnd && currentEnd > requestStart;

        if (hasOverlap) {
            const requestType = loadedEvent.extendedProps?.record?.b3s__Type__c;
            const requestName = loadedEvent.extendedProps?.record?.Name || 'Availability Windows';

            switch (requestType) {
                case 'Unavailable':
                    results.push(
                        new EvaluationResult({
                            event: event,
                            detail: `🚫 Conflict: Worker is marked Unavailable during this period (${requestName})`,
                            category: 'Availability',
                            points: -100,
                            maxPoints: 25,
                            isBlocking: false, // Soft/Hard block allocation
                            isVisible: true
                        })
                    );
                    availabilityMatchFound = true;
                    break;

                case 'Available':
                    results.push(
                        new EvaluationResult({
                            event: event,
                            detail: `🌟 Preferred: Worker has verified availability confirmed (${requestName})`,
                            category: 'Availability',
                            points: 25,
                            maxPoints: 25,
                            isBlocking: false, 
                            isVisible: true
                        })
                    );
                    availabilityMatchFound = true;
                    break;

                case 'Flexible Schedule':
                    results.push(
                        new EvaluationResult({
                            event: event,
                            detail: `⚠️ Notice: Flexible availability match (${requestName}). Review constraints before matching.`,
                            category: 'Availability',
                            points: 0,
                            maxPoints: 25,
                            isBlocking: false, 
                            isVisible: true
                        })
                    );
                    availabilityMatchFound = true;
                    break;

                default:
                    // Unknown or unhandled types skip processing adjustments
                    break;
            }
        }
    }

    // Optional: If no intentional availability log matches the timeframe, 
    // it defaults safely to an empty evaluation array.
    return results;
}        
        ]]></value>
    </values>
    <values>
        <field>b3s__Type__c</field>
        <value xsi:type="xsd:string">Clock In / Out Component</value>
    </values>
</CustomMetadata>
