import { PaginatedSerializationClass } from '@humand-packages/common';
import { TimeTrackingDaySummary } from '../../business/models/daySummary';
import { EmployeeSummary } from '../../business/models/employeeSummary';
import { WorkSchedule } from '../../business/models/workSchedule';
import { UserCalendarDTO } from '../dtos/userCalendarDTO';

export class ScheduleCalendarSC extends PaginatedSerializationClass<EmployeeSummary, UserCalendarDTO> {
    serialize(serializableObject: EmployeeSummary, extraData: { schedulesById: Record<number, object> }): UserCalendarDTO {
        const days = serializableObject.daySummaries.reduce((acc: Record<string, object>, daySummary) => {
            const id = daySummary.workScheduleId;
            const schedule = id && extraData.schedulesById[id] ? extraData.schedulesById[id] : ({ id } as WorkSchedule);
            const timeOff = daySummary.timeOffRequests && daySummary.timeOffRequests?.length > 0 ? daySummary.timeOffRequests[0] : null;
            const holiday = daySummary.holidays && daySummary.holidays?.length > 0 ? daySummary.holidays[0] : null;
            acc[daySummary.dateString] = { ...schedule, daySummary: mapDaySummaryForScheduleCalendar(daySummary), holiday, timeOff };
            return acc;
        }, {});
        return {
            days,
            employeeInternalId: serializableObject.employeeInternalId,
            fullName: serializableObject.fullName,
            profilePicture: serializableObject.profilePicture,
            userId: serializableObject.userId,
        };
    }
}

export const mapDaySummaryForScheduleCalendar = (daySummary: TimeTrackingDaySummary) => {
    const { scheduleSource, isWorkday, timeSlots, shiftAssignedDayId } = daySummary;
    return { isWorkday, scheduleSource, shiftAssignedDayId, timeSlots };
};
