import { TimeTrackingEntry, TimeTrackingEntryPrototype } from '../../business/models/timeTrackingEntry';
import { TimeTrackingEntryType } from '../../business/types/entry';
import { TimeTrackingEntrySource } from '../../business/types/source';
import { LocationVC } from '../../presentation/validationClasses/locationValidationClass';
import { TimeTrackingEntryCreationAttributes, TimeTrackingEntryDAO } from '../dataAccessObjects/entry';
import { mapKioskDAOToModel } from './timeTrackingKioskMapper';

export const mapDaoToModel = (dao: TimeTrackingEntryDAO): TimeTrackingEntry => {
    let location: LocationVC | undefined = undefined;
    if (dao && dao.latitude && dao.longitude) {
        location = { latitude: dao.latitude, longitude: dao.longitude };
    }
    return new TimeTrackingEntry({
        autoCloseFallback: dao.autoCloseFallback ?? null,
        comment: dao.comment,
        createdAt: dao.createdAt,
        createdBy: dao.createdBy,
        deletedBy: dao.deletedBy,
        desertEagleEntryId: dao.desertEagleEntryId ?? undefined,
        eventTime: dao.eventTime,
        facialRecognitionSkipped: dao.facialRecognitionSkipped,
        id: dao.id,
        instanceId: dao.instanceId,
        isRemoteWork: dao.isRemoteWork,
        kiosk: dao.kiosk ? mapKioskDAOToModel(dao.kiosk) : undefined,
        kioskId: dao.kioskId,
        location: location,
        locationIncidence: undefined,
        siteId: dao.siteId,
        source: dao.source as TimeTrackingEntrySource,
        summaryId: dao.summaryId,
        time: dao.time,
        type: dao.type as TimeTrackingEntryType,
        updatedAt: dao.updatedAt,
        updatedBy: dao.updatedBy,
        userId: dao.userId,
    });
};

export const mapModelToDaoCreation = (model: TimeTrackingEntryPrototype): TimeTrackingEntryCreationAttributes => {
    return {
        autoCloseFallback: model.autoCloseFallback,
        comment: model.comment,
        createdBy: model.createdBy,
        desertEagleEntryId: model.desertEagleEntryId ?? undefined,
        eventTime: model.eventTime,
        facialRecognitionSkipped: model.facialRecognitionSkipped,
        instanceId: model.instanceId,
        isRemoteWork: model.isRemoteWork,
        kioskId: model.kioskId,
        latitude: model.location?.latitude,
        longitude: model.location?.longitude,
        siteId: model.siteId,
        source: model.source,
        summaryId: model.summaryId,
        time: model.time,
        type: model.type,
        updatedBy: model.updatedBy,
        userId: model.userId,
    };
};
