import { Injectable } from '@nestjs/common';
import { WorkTime } from '@prisma/client';
import { Interval } from 'date-fns';
import { PrismaService } from 'src/core/database/prisma.service';

@Injectable()
export class WorkTimeRepository {
  constructor(private readonly prisma: PrismaService) {}

  async findById(id: string): Promise<WorkTime> {
    return await this.prisma.workTime.findUnique({
      where: { id },
    });
  }

  async findByPersonnelNumberInInterval(
    personnelNumber: string,
    interval: Interval,
  ): Promise<WorkTime> {
    return await this.prisma.workTime.findFirst({
      where: {
        userId: personnelNumber,
        AND: [
          {
            startDate: {
              gte: new Date(interval.start),
            },
          },
          {
            startDate: {
              lte: new Date(interval.end),
            },
          },
        ],
      },
    });
  }
}
