import { PaginatedSerializationClass } from '@humand-packages/common';
import { Site } from '../../../regions/business/models/site';
import { mapShiftAssociations } from '../../business/mappers/shiftManagement/mapShiftCalendar';
import { EmployeeSummary } from '../../business/models/employeeSummary';
import { ShiftAssignedDay } from '../../business/models/shiftManagement/shiftAssignedDay';
import { WorkSchedule } from '../../business/models/workSchedule';
import { TimeTrackingAppGrant } from '../constants/timeTrackingGrants';
import { TimeTrackingDaySummaryExtended } from '../dtos/daySummaryExtended';
import { EmployeeSummaryWithSiteData } from '../dtos/employeeSummaryWithSiteData';
import { TimeTrackingEntryExtended } from '../dtos/timeTrackingEntryExtended';

export class EmployeeTableSC extends PaginatedSerializationClass<EmployeeSummary, EmployeeSummaryWithSiteData> {
    serialize(
        serializableObject: EmployeeSummary,
        extraData: {
            schedules: Map<number, WorkSchedule>;
            sites: Map<number, Site>;
            shiftAssignedDays: Map<number, ShiftAssignedDay>;
            grants: Map<number, TimeTrackingAppGrant[]>;
        },
    ): EmployeeSummaryWithSiteData {
        const daySummariesExtended = serializableObject.daySummaries.map((daySummary) => {
            const entriesExtended = daySummary.timeTrackingEntries?.map((entry) => {
                const site = entry.siteId ? extraData.sites.get(entry.siteId) : undefined;
                return { ...entry, siteName: site ? site.getName() : undefined } as TimeTrackingEntryExtended;
            });
            const workSchedule = daySummary.workScheduleId ? extraData.schedules.get(daySummary.workScheduleId) : undefined;
            const shiftAssignedDay = daySummary.shiftAssignedDayId ? extraData.shiftAssignedDays.get(daySummary.shiftAssignedDayId) : undefined;
            return {
                ...daySummary,
                shiftAssignedDayId: daySummary.shiftAssignedDayId,
                shiftAssociations: shiftAssignedDay ? mapShiftAssociations(shiftAssignedDay) : undefined,
                timeTrackingEntries: entriesExtended,
                workScheduleDescription: workSchedule?.description,
                workScheduleName: workSchedule?.name,
            } as TimeTrackingDaySummaryExtended;
        });
        return {
            currentDate: serializableObject.currentDate,
            daySummaries: daySummariesExtended,
            differenceHours: serializableObject.differenceHours,
            employeeInternalId: serializableObject.employeeInternalId,
            estimatedHours: serializableObject.estimatedHours,
            extraHours: serializableObject.extraHours,
            fullName: serializableObject.fullName,
            grants: extraData.grants.get(serializableObject.userId) ?? [],
            incidencesTagsPresent: serializableObject.incidencesTagsPresent,
            profilePicture: serializableObject.profilePicture,
            status: serializableObject.status,
            userAccountStatus: serializableObject.userAccountStatus,
            userId: serializableObject.userId,
            workedHours: serializableObject.workedHours,
        };
    }
}
