import { TimeTrackingPolicyAutoCloseMode } from '../../business/enum/timeTracking/policy';
import { TimeTrackingPolicy, TimeTrackingPolicyAssociation } from '../../business/models/timeTrackingPolicy';
import { TimeTrackingPolicyDAO } from '../dataAccessObjects/policy';
import { TimeTrackingPolicyUsersDAO } from '../dataAccessObjects/policyUsers';

export const mapPolicyDAOToModel = (policyDAO: TimeTrackingPolicyDAO): TimeTrackingPolicy => {
    const userIds = new Set(policyDAO.users?.map((users) => users.userId) || []);
    return {
        allUsers: policyDAO.allUsers,
        autoCloseFixedHoursValue: policyDAO.autoCloseFixedHoursValue ?? null,
        autoCloseFixedMinutesValue: policyDAO.autoCloseFixedMinutesValue ?? null,
        autoCloseLimitHours: policyDAO.autoCloseLimitHours,
        autoCloseMode: policyDAO.autoCloseMode as TimeTrackingPolicyAutoCloseMode,
        clockingInAppEnabled: policyDAO.clockingInAppEnabled,
        clockingInKioskEnabled: policyDAO.clockingInKioskEnabled,
        clockingInWebEnabled: policyDAO.clockingInWebEnabled,
        description: policyDAO.description,
        enableRemoteWork: policyDAO.enableRemoteWork,
        saveRemoteWorkLocation: policyDAO.saveRemoteWorkLocation,
        faceRecognitionBlocking: policyDAO.faceRecognitionBlocking,
        faceRecognitionEnabled: policyDAO.faceRecognitionEnabled,
        generatePolicyHistoryFromDate: policyDAO.generatePolicyHistoryFromDate,
        geofencing: policyDAO.geofencing,
        id: policyDAO.id,
        instanceId: policyDAO.instanceId,
        latenessTolerance: policyDAO.latenessTolerance,
        name: policyDAO.name,
        nightShift: policyDAO.nightShift,
        notificationsEnabled: policyDAO.notificationsEnabled,
        userIds: Array.from(userIds),
    };
};

export const mapPolicyModelToDAO = (policy: TimeTrackingPolicy): TimeTrackingPolicyDAO => {
    const policyDAO = new TimeTrackingPolicyDAO({
        autoCloseFixedHoursValue: policy.autoCloseFixedHoursValue,
        autoCloseFixedMinutesValue: policy.autoCloseFixedMinutesValue,
        autoCloseLimitHours: policy.autoCloseLimitHours,
        autoCloseMode: policy.autoCloseMode,
        clockingInAppEnabled: policy.clockingInAppEnabled,
        clockingInKioskEnabled: policy.clockingInKioskEnabled,
        clockingInWebEnabled: policy.clockingInWebEnabled,
        description: policy.description,
        enableRemoteWork: policy.enableRemoteWork,
        saveRemoteWorkLocation: policy.saveRemoteWorkLocation,
        faceRecognitionBlocking: policy.faceRecognitionBlocking,
        faceRecognitionEnabled: policy.faceRecognitionEnabled,
        geofencing: policy.geofencing,
        instanceId: policy.instanceId,
        latenessTolerance: policy.latenessTolerance,
        name: policy.name,
        nightShift: policy.nightShift,
        notificationsEnabled: policy.notificationsEnabled,
    });
    if (policy.id) {
        policyDAO.id = policy.id;
        policyDAO.isNewRecord = false;
    }
    return policyDAO;
};

export const mapPolicyAssociationDAOToModel = (policyAssociationDAO: TimeTrackingPolicyUsersDAO): TimeTrackingPolicyAssociation => {
    return {
        fromDate: policyAssociationDAO.fromDate,
        policy: policyAssociationDAO.policy ? mapPolicyDAOToModel(policyAssociationDAO.policy) : undefined,
        toDate: policyAssociationDAO.toDate ?? undefined,
        userId: policyAssociationDAO.userId,
    };
};
