import { BaseAdapter } from '@humand-packages/common';
import { Service } from 'typedi';
import { PortsDI } from '../../../../config/portsDI';
import { TimeOffAssignedWorkSchedule, TimeTrackingWorkScheduleDataPort } from '../../../timeOff/business/ports/timeTrackingWorkScheduleDataPort';
import WorkScheduleServicePort from '../../business/ports/workScheduleServicePort';

@Service()
export class TimeTrackingWorkScheduleDataAdapter extends BaseAdapter implements TimeTrackingWorkScheduleDataPort {
    constructor(@PortsDI(WorkScheduleServicePort) private readonly workScheduleService: WorkScheduleServicePort) {
        super();
    }

    async getWorkScheduleByUserIds(userIds: number[], instanceId: number, timezone: string): Promise<Record<number, TimeOffAssignedWorkSchedule>> {
        const result = await this.workScheduleService.getWorkScheduleByUserIds(userIds, instanceId, timezone);
        const mapped: Record<number, TimeOffAssignedWorkSchedule> = {};
        for (const [userId, schedule] of Object.entries(result)) {
            mapped[Number(userId)] = {
                scheduledDays: schedule.scheduledDays.map((d) => ({ dayOfWeek: d.dayOfWeek })),
                startDate: schedule.startDate,
                endDate: schedule.endDate,
            };
        }
        return mapped;
    }

    async getAssignedWorkSchedulesByUser(
        userId: number,
        instanceId: number,
        fromDate: string,
        toDate: string,
    ): Promise<TimeOffAssignedWorkSchedule[]> {
        const schedules = await this.workScheduleService.getAssignedWorkSchedulesByUser(userId, instanceId, fromDate, toDate);
        return schedules.map((s) => ({
            scheduledDays: s.scheduledDays.map((d) => ({ dayOfWeek: d.dayOfWeek })),
            startDate: s.startDate,
            endDate: s.endDate,
        }));
    }
}
