import { TimeTrackingConfigMessages } from '@humand/desert-eagle-grpc-ts-client';
import {
    TimeTrackingUserSetting as TimeTrackingUserSettingGrpc,
    FaceRecognitionConfiguration as FaceRecognitionConfigurationGrpc,
    TimeTrackingPolicy as TimeTrackingPolicyGrpc,
} from '../../../../../../api/grpc/generated/exposed/humand/timetracking/v1/timetracking_config_service';
import { FaceRecognitionConfiguration } from '../../../../facialRecognition/business/interfaces/faceRecognitionConfiguration';
import { TimeTrackingUserSetting } from '../../../business/models/timeTracking/timeTrackingUserSetting';
import { TimeTrackingPolicy } from '../../../business/models/timeTrackingPolicy';

/**
 * Dependency rule: `saveRemoteWorkLocation` is a sub-feature of `enableRemoteWork`.
 * When `enableRemoteWork` is false/null/undefined, `saveRemoteWorkLocation` must also be false.
 */
const resolveSaveRemoteWorkLocation = (policy: TimeTrackingPolicy): boolean => (policy.enableRemoteWork ?? false) && policy.saveRemoteWorkLocation;

export const timeTrackingPolicyToTimeTrackingPolicyGrpc = (policy: TimeTrackingPolicy): TimeTrackingConfigMessages.TimeTrackingPolicy => {
    return {
        allUsers: policy.allUsers,
        clockingInAppEnabled: policy.clockingInAppEnabled,
        clockingInKioskEnabled: policy.clockingInKioskEnabled,
        clockingInWebEnabled: policy.clockingInWebEnabled,
        description: policy.description,
        faceRecognitionBlocking: policy.faceRecognitionBlocking,
        faceRecognitionEnabled: policy.faceRecognitionEnabled,
        geofencing: policy.geofencing ?? false,
        enableRemoteWork: policy.enableRemoteWork ?? false,
        saveRemoteWorkLocation: resolveSaveRemoteWorkLocation(policy),
        id: policy.id,
        instanceId: policy.instanceId.toString(),
        latenessTolerance: policy.latenessTolerance || 0,
        name: policy.name,
        nightShift: policy.nightShift,
        notificationsEnabled: policy.notificationsEnabled,
    };
};

export const mapTimeTrackingUserSettingToTimeTrackingUserSettingGrpc = (setting: TimeTrackingUserSetting): TimeTrackingUserSettingGrpc => {
    return {
        kioskPin: setting.kioskPin ?? '',
        userId: setting.userId,
    };
};

export const mapFaceRecognitionConfigurationToFaceRecognitionConfigurationGrpc = (
    configuration: FaceRecognitionConfiguration,
): FaceRecognitionConfigurationGrpc => {
    return {
        referenceImage: configuration.referenceImage ?? '',
        userId: configuration.userId,
    };
};

export const mapTimeTrackingPoliciesToTimeTrackingPoliciesGrpc = (policies: TimeTrackingPolicy): TimeTrackingPolicyGrpc => {
    return {
        allUsers: policies.allUsers,
        clockingInAppEnabled: policies.clockingInAppEnabled,
        clockingInKioskEnabled: policies.clockingInKioskEnabled,
        clockingInWebEnabled: policies.clockingInWebEnabled,
        description: policies.description,
        faceRecognitionBlocking: policies.faceRecognitionBlocking,
        faceRecognitionEnabled: policies.faceRecognitionEnabled,
        geofencing: policies.geofencing ?? false,
        enableRemoteWork: policies.enableRemoteWork ?? false,
        saveRemoteWorkLocation: resolveSaveRemoteWorkLocation(policies),
        id: policies.id,
        instanceId: policies.instanceId,
        name: policies.name,
        nightShift: policies.nightShift,
        userIds: policies.userIds,
    };
};
