<?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>Shift Attendance Filter</label>
    <protected>false</protected>
    <values>
        <field>b3s__Javascript_Code__c</field>
        <value xsi:type="xsd:string"><![CDATA[
{
    name: "shift_attendance",
    label: "Time and Attendance",
    type: 'shift',
    //Requires b3s__Check_In_Required__c, b3s__Check_In_Time__c and
    //b3s__Check_In_Distance__c in the shift definition's additionalFields.
    //Thresholds mirror the WFM Time and Attendance reports - keep in step.
    items: function() {
        return [
            { name: "no_check_in_60", label: "Not checked in, starts within 60 min" },
            { name: "no_check_in_120", label: "Not checked in, starts within 120 min" },
            { name: "late_risk", label: "At risk of lateness" },
            { name: "late", label: "Late clock-in (over 5 min)" },
            { name: "no_show", label: "No show (over 15 min)" },
            { name: "early_in", label: "Early clock-in (over 30 min)" },
            { name: "early_out", label: "Early clock-out (over 5 min)" },
            { name: "late_out", label: "Late clock-out (over 15 min)" },
            { name: "not_out", label: "Not clocked out after end" }
        ];
    },
    matchesItems: (function() {
        const MIN = 60000;
        const time = (value) => value ? Date.parse(value) : null;

        //Each rule gets the shift record (scheduled/actual times as epoch ms) and now
        const rules = {
            no_check_in_60: (s, now) => s.awaitingCheckIn && s.start > now && s.start <= now + 60 * MIN,
            no_check_in_120: (s, now) => s.awaitingCheckIn && s.start > now && s.start <= now + 120 * MIN,
            //Starts within the hour, not clocked in, and either never checked in
            //or checked in more than 5 km from the site
            late_risk: (s, now) => s.actualStart == null && s.start > now && s.start <= now + 60 * MIN &&
                (s.awaitingCheckIn || s.checkInKm > 5),
            late: (s) => s.actualStart != null && s.actualStart > s.start + 5 * MIN,
            no_show: (s, now) => s.actualStart == null && now > s.start + 15 * MIN,
            early_in: (s) => s.actualStart != null && s.actualStart < s.start - 30 * MIN,
            early_out: (s) => s.actualEnd != null && s.end != null && s.actualEnd < s.end - 5 * MIN,
            late_out: (s) => s.actualEnd != null && s.end != null && s.actualEnd > s.end + 15 * MIN,
            not_out: (s, now) => s.actualStart != null && s.actualEnd == null && s.end != null && now > s.end
        };

        return function(event, filterItems = []) {
            //Only constrain shifts — leave requests and other event types alone
            if (event.title !== 'shift') return true;

            const record = event.extendedProps?.record ?? {};
            const start = time(record.b3s__Scheduled_Start_Time__c);
            //Attendance only applies to assigned, scheduled, live shifts
            if (!record.b3s__Contact__c || start == null || record.b3s__Status__c === 'Cancelled') return false;

            const checkInDistance = record.b3s__Check_In_Distance__c;
            const shift = {
                start,
                end: time(record.b3s__Scheduled_End_Time__c),
                actualStart: time(record.b3s__Actual_Start_Time__c),
                actualEnd: time(record.b3s__Actual_End_Time__c),
                checkInKm: checkInDistance == null ? null : checkInDistance / 1000,
                awaitingCheckIn: record.b3s__Check_In_Required__c === true &&
                    !record.b3s__Check_In_Time__c && !record.b3s__Actual_Start_Time__c
            };
            const now = Date.now();

            return filterItems.some((item) => rules[item]?.(shift, now));
        };
    })()
}
        ]]></value>
    </values>
    <values>
        <field>b3s__Order__c</field>
        <value xsi:type="xsd:double">11.0</value>
    </values>
</CustomMetadata>
