import { Site } from '../../../regions/business/models/site';
import { mapSiteModelToSiteBasicDTO } from '../../../regions/presentation/mappers/mapSite';
import { filterIncidencesForNonPastDays } from '../../business/helpers/incidenceHelper';
import { TimeTrackingDaySummary } from '../../business/models/daySummary';
import { TimeTrackingCategorizedHourWithCategoryData } from '../../business/models/timeTracking/timeTrackingCategorizedHour';
import { TimeTrackingEntry } from '../../business/models/timeTrackingEntry';
import {
    TimeTrackingDaySummaryDTO,
    TimeTrackingDaySummaryEntryDTO,
    TimeTrackingDaySummaryScheduleDTO,
    TimeTrackingDaySummaryUserDTO,
} from '../dtos/timeTrackingDaySummaryDTO';
import { mapHolidayToApiHoliday, mapTimeOffRequestToApiTimeOffRequest } from './mapDaySummaryToDaySummaryApi';

const mapEntryToDetailEntry = (entry: TimeTrackingEntry, site?: Site): TimeTrackingDaySummaryEntryDTO => {
    return {
        id: entry.id,
        type: entry.type,
        userId: entry.userId,
        time: entry.time,
        date: entry.time,
        source: entry.source,
        site: site ? mapSiteModelToSiteBasicDTO(site) : undefined,
        siteName: site?.getName(),
        coordinates: entry.location,
        comment: entry.comment,
        locationIncidence: entry.locationIncidence,
        isRemoteWork: entry.isRemoteWork,
        autoCloseFallback: entry.autoCloseFallback ?? null,
        createdAt: entry.createdAt,
        updatedAt: entry.updatedAt,
    };
};

export const mapDaySummaryToDetail = (
    daySummary: TimeTrackingDaySummary,
    extraData: {
        timezone: string;
        user: TimeTrackingDaySummaryUserDTO;
        schedule: TimeTrackingDaySummaryScheduleDTO;
        siteByIds: Map<number, Site>;
        categorizedHours: TimeTrackingCategorizedHourWithCategoryData[];
    },
): TimeTrackingDaySummaryDTO => {
    const incidences = filterIncidencesForNonPastDays(daySummary.incidences, daySummary.dateString, extraData.timezone);

    return {
        id: daySummary.id,
        date: daySummary.date,
        dateString: daySummary.dateString,
        userId: daySummary.userId,
        user: extraData.user,
        weekday: daySummary.weekday,
        isWorkday: daySummary.isWorkday,
        nightShift: daySummary.nightShift,
        schedule: extraData.schedule,
        hours: {
            worked: daySummary.workedHours,
            scheduled: daySummary.scheduledHours,
            estimated: daySummary.estimatedHours,
            extra: daySummary.extraHours,
            timeOff: daySummary.timeOffHours,
        },
        categorizedHours: extraData.categorizedHours.map((ch) => ({
            category: {
                name: ch.hourCategoryName,
                description: ch.hourCategoryDescription ?? null,
            },
            hours: ch.hours,
        })),
        timeSlots: daySummary.timeSlots ?? [],
        entries: daySummary.timeTrackingEntries
            ? daySummary.timeTrackingEntries.map((entry) =>
                  mapEntryToDetailEntry(entry, entry.siteId ? extraData.siteByIds.get(entry.siteId) : undefined),
              )
            : [],
        holidays: daySummary.holidays?.map(mapHolidayToApiHoliday) ?? [],
        timeOffRequests: daySummary.timeOffRequests?.map(mapTimeOffRequestToApiTimeOffRequest) ?? [],
        incidences,
    };
};
