import { Site } from '../../../regions/business/models/site';
import { filterIncidencesForNonPastDays } from '../../business/helpers/incidenceHelper';
import { TimeTrackingDaySummary, TimeTrackingDaySummaryApi } from '../../business/models/daySummary';
import { TimeTrackingDaySummaryHours } from '../../business/models/daySummaryHours';
import { TimeTrackingCategorizedHourWithCategoryData } from '../../business/models/timeTracking/timeTrackingCategorizedHour';
import { TimeTrackingDaySummaryHoliday, TimeTrackingDaySummaryHolidayApi } from '../../business/models/timeTrackingDaySummaryHoliday';
import { TimeTrackingDaySummaryTimeOff, TimeTrackingDaySummaryTimeOffApi } from '../../business/models/timeTrackingDaySummaryTimeOff';
import { DaySummaryScheduleSource } from '../../business/types/scheduleSource';
import { mapModelEntryToApiEntry } from '../../infrastructure/mappers/timeTrackingEntryApiMapper';

export const mapDaySummaryToDaySummaryApi = (
    daySummary: TimeTrackingDaySummary,
    extraData: {
        employeeId: string;
        timezone: string;
        siteByIds: Map<number, Site>;
        categorizedHours: TimeTrackingCategorizedHourWithCategoryData[];
    },
): TimeTrackingDaySummaryApi => {
    const incidences = filterIncidencesForNonPastDays(daySummary.incidences, daySummary.dateString, extraData.timezone);

    return {
        categorizedHours: extraData.categorizedHours.map((categorizedHour) => ({
            category: { name: categorizedHour.hourCategoryName },
            hours: categorizedHour.hours,
        })),
        date: daySummary.date,
        employeeId: extraData.employeeId,
        entries: daySummary.timeTrackingEntries
            ? daySummary.timeTrackingEntries.map((entry) =>
                  mapModelEntryToApiEntry(entry, {
                      employeeId: extraData.employeeId,
                      referenceDate: daySummary.dateString,
                      site: entry.siteId ? extraData.siteByIds.get(entry.siteId) : undefined,
                  }),
              )
            : [],
        hasSchedule: daySummary.scheduleSource !== DaySummaryScheduleSource.NO_SCHEDULE,
        holidays: daySummary.holidays?.map(mapHolidayToApiHoliday),
        hours: new TimeTrackingDaySummaryHours({
            estimated: daySummary.estimatedHours,
            scheduled: daySummary.scheduledHours,
            timeOff: daySummary.timeOffHours,
            worked: daySummary.workedHours,
        }),
        id: daySummary.id,
        incidences,
        isWorkday: daySummary.isWorkday,
        referenceDate: daySummary.dateString,
        timeOffRequests: daySummary.timeOffRequests?.map(mapTimeOffRequestToApiTimeOffRequest) ?? [],
        timeSlots: daySummary.timeSlots,
        userId: daySummary.userId,
        weekday: daySummary.weekday,
    };
};

export const mapTimeOffRequestToApiTimeOffRequest = (timeOffRequest: TimeTrackingDaySummaryTimeOff): TimeTrackingDaySummaryTimeOffApi => {
    return {
        activityType: timeOffRequest.activityType,
        consumptionType: timeOffRequest.consumptionType,
        fromDate: timeOffRequest.fromDate,
        fromTime: timeOffRequest.fromTime,
        hours: timeOffRequest.hours,
        id: timeOffRequest.id,
        name: timeOffRequest.timeOffPolicyTypeName,
        toDate: timeOffRequest.toDate,
        toTime: timeOffRequest.toTime,
    };
};

export const mapHolidayToApiHoliday = (holiday: TimeTrackingDaySummaryHoliday): TimeTrackingDaySummaryHolidayApi => {
    return {
        id: holiday.id,
        name: holiday.holidayName,
    };
};
