import { toWorkday } from '@humand-packages/common';
import { DEFAULT_AUTO_CLOSE_MODE } from '../../business/constants/timeTrackingPolicy';
import { TimeTrackingDaySummary } from '../../business/models/daySummary';
import { TimeTrackingDaySummaryCreationAttributes, TimeTrackingDaySummaryDAO } from '../dataAccessObjects/daySummary';
import { mapHolidayAssociationDaoToModel } from './daySummaryHolidayMapper';
import { mapTimeOffAssociationDaoToModel } from './daySummaryTimeOffMapper';
import { mapDaoToModel } from './timeTrackingToTimeTrackingDao';

export const mapSummaryDaoToModel = (dao: TimeTrackingDaySummaryDAO): TimeTrackingDaySummary => {
    return new TimeTrackingDaySummary({
        autoCloseEntryTime: dao.autoCloseEntryTime,
        autoCloseMode: dao.autoCloseMode,
        autoCloseTime: dao.autoCloseTime,
        createPolicyHistory: undefined,
        date: dao.date,
        dateString: dao.dateString,
        dayChange: dao.dayChange,
        estimatedHours: dao.estimatedHours,
        extraHours: dao.estimatedHours ? Math.max(dao.workedHours - dao.estimatedHours, 0) : 0, // transient
        holidays: dao.holidays?.map(mapHolidayAssociationDaoToModel) ?? [],
        id: dao.id,
        incidences: dao.incidences,
        instanceId: dao.instanceId,
        isDeleted: dao.deletedAt !== null,
        isWorkday: dao.isWorkday,
        latenessTolerance: dao.policyHistory?.latenessTolerance,
        nightShift: dao.nightShift,
        scheduleSource: dao.scheduleSource,
        scheduledHours: dao.scheduledHours ?? dao.estimatedHours, // TODO remove fallback once it is mandatory
        shiftAssignedDayId: dao.shiftAssignedDayId,
        state: dao.state,
        timeOffHours: dao.timeOffHours ?? 0,
        timeOffRequests: dao.timeOffRequests?.map(mapTimeOffAssociationDaoToModel) ?? [],
        timeSlots: dao.timeSlots ?? undefined,
        timeTrackingEntries: dao.timeTrackingEntries?.map(mapDaoToModel) ?? [],
        userId: dao.userId,
        weekday: dao.weekday,
        workScheduleId: dao.workScheduleId,
        workedHours: dao.workedHours,
    });
};

export const mapSummaryModelToDao = (model: Partial<TimeTrackingDaySummary>): TimeTrackingDaySummaryCreationAttributes => {
    return {
        autoCloseEntryTime: model.autoCloseEntryTime,
        autoCloseMode: model.autoCloseMode ?? DEFAULT_AUTO_CLOSE_MODE,
        autoCloseTime: model.autoCloseTime,
        date: model.date!,
        dateString: model.dateString!,
        dayChange: model.dayChange!,
        estimatedHours: model.estimatedHours!,
        incidences: model.incidences!,
        instanceId: model.instanceId!,
        isWorkday: model.isWorkday!,
        nightShift: model.nightShift ?? false,
        scheduledHours: model.scheduledHours ?? model.estimatedHours!,
        state: model.state!,
        timeOffHours: model.timeOffHours ?? null,
        timeSlots: model.timeSlots ?? null,
        userId: model.userId!,
        weekday: model.weekday ?? toWorkday(new Date(model.dateString!)),
        workScheduleId: model.workScheduleId,
        workedHours: model.workedHours!,
    };
};
