import { BaseAdapter } from '@humand-packages/common';
import { Service } from 'typedi';
import { PortsDI } from '../../../../../config/portsDI';
import { Instance } from '../../../../instances/business/models/instance';
import { TimeTrackingRepositoryPort } from '../../../infrastructure/ports/TimeTrackingRepositoryPort';
import { GridFilterRequest } from '../../interfaces/shiftManagement/gridFiltersInterface';
import { TimeTrackingDaySummary } from '../../models/daySummary';
import TimeTrackingService from '../timeTracking';

@Service()
export class TimeTrackingForShiftService extends BaseAdapter {
    constructor(
        @PortsDI(TimeTrackingRepositoryPort) private readonly timeTrackingRepository: TimeTrackingRepositoryPort,
        @PortsDI(TimeTrackingService) private readonly timeTrackingService: TimeTrackingService,
    ) {
        super();
    }

    public getTimeTrackingDaySummaries = async (
        instance: Instance,
        userIds: number[],
        fromDate: string,
        toDate: string,
    ): Promise<TimeTrackingDaySummary[]> => {
        await this.timeTrackingService.createMissingSummariesForUsers(userIds, fromDate, toDate, instance);
        return this.timeTrackingRepository.getDaySummariesByUsers(userIds, fromDate, toDate);
    };

    public deleteTimeTrackingDaySummaries = async (instanceId: number, filters: GridFilterRequest): Promise<void> => {
        await this.timeTrackingRepository.deleteDaySummariesByGridFilters(instanceId, filters);
    };

    public createMissingDaySummariesForUsers = async (instance: Instance, userIds: number[], fromDate: string, toDate: string): Promise<void> => {
        await this.timeTrackingService.createMissingSummariesForUsers(userIds, fromDate, toDate, instance);
    };

    public deleteDaySummariesByShiftAssignedDayId = async (instanceId: number, shiftAssignedDayId: number): Promise<void> => {
        await this.timeTrackingRepository.deleteDaySummariesByShiftAssignedDayId(instanceId, shiftAssignedDayId);
    };
}
