import { Injectable } from '@nestjs/common';
import {
  AttendanceRegistrationSource,
  EmployeeDispoLiveSource,
  EmployeeDyflexisSource,
  PatientSource,
  PlannedTimeSource,
  Prisma,
  TourSource,
  WorkTimeSource,
} from '@prisma/client';
import { PrismaService } from 'src/core/database/prisma.service';
import {
  AttendanceRegistrationRaw,
  EmployeeDispoLiveRaw,
  EmployeeDyflexisRaw,
  PatientRaw,
  PlannedTimeRaw,
  TourRaw,
  WorkTimeRaw,
} from './source.interface';

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

  clearSource(): Promise<Prisma.BatchPayload[]> {
    return Promise.all([
      this.prisma.tourSource.deleteMany(),
      this.prisma.employeeDispoLiveSource.deleteMany(),
      this.prisma.employeeDyflexisSource.deleteMany(),
      this.prisma.workTimeSource.deleteMany(),
      this.prisma.plannedTimeSource.deleteMany(),
      this.prisma.attendanceRegistrationSource.deleteMany(),
      this.prisma.patientSource.deleteMany(),
    ]);
  }

  createTours(
    tours: TourRaw[],
    jobId: string,
  ): Prisma.PrismaPromise<Prisma.BatchPayload> {
    return this.prisma.tourSource.createMany({
      data: tours.map((data) => ({ jobId, ...data })),
    });
  }

  createEmployeesDispoLive(
    employees: EmployeeDispoLiveRaw[],
    jobId: string,
  ): Prisma.PrismaPromise<Prisma.BatchPayload> {
    return this.prisma.employeeDispoLiveSource.createMany({
      data: employees.map((data) => ({ jobId, ...data })),
    });
  }

  createPatients(
    patients: PatientRaw[],
    jobId: string,
  ): Promise<Prisma.BatchPayload> {
    return this.prisma.patientSource.createMany({
      data: patients.map((data) => ({ jobId, ...data })),
    });
  }

  createEmployeesDyflexis(
    employees: EmployeeDyflexisRaw[],
    jobId: string,
  ): Prisma.PrismaPromise<Prisma.BatchPayload> {
    return this.prisma.employeeDyflexisSource.createMany({
      data: employees.map((data) => ({ jobId, ...data })),
    });
  }

  createAttendanceRegistrations(
    attendanceRegistrations: AttendanceRegistrationRaw[],
    jobId: string,
  ): Prisma.PrismaPromise<Prisma.BatchPayload> {
    return this.prisma.attendanceRegistrationSource.createMany({
      data: attendanceRegistrations.map((data) => ({ jobId, ...data })),
    });
  }

  createWorkTimes(
    workTimes: WorkTimeRaw[],
    jobId: string,
  ): Prisma.PrismaPromise<Prisma.BatchPayload> {
    return this.prisma.workTimeSource.createMany({
      data: workTimes.map((data) => ({ jobId, ...data })),
    });
  }

  createPlannedTimes(
    plannedTimes: PlannedTimeRaw[],
    jobId: string,
  ): Prisma.PrismaPromise<Prisma.BatchPayload> {
    return this.prisma.plannedTimeSource.createMany({
      data: plannedTimes.map((data) => ({ jobId, ...data })),
    });
  }

  findToursByJobId(jobId: string): Promise<TourSource[]> {
    return this.prisma.tourSource.findMany({
      where: {
        jobId,
      },
    });
  }

  findPatientsByJobId(jobId: string): Promise<PatientSource[]> {
    return this.prisma.patientSource.findMany({
      where: {
        jobId,
      },
    });
  }

  findPlannedTimesByJobId(jobId: string): Promise<PlannedTimeSource[]> {
    return this.prisma.plannedTimeSource.findMany({
      where: {
        jobId,
      },
    });
  }

  findWorkTimesByJobId(jobId: string): Promise<WorkTimeSource[]> {
    return this.prisma.workTimeSource.findMany({
      where: {
        jobId,
      },
    });
  }

  findAttendanceRegistrationsByJobId(
    jobId: string,
  ): Promise<AttendanceRegistrationSource[]> {
    return this.prisma.attendanceRegistrationSource.findMany({
      where: {
        jobId,
      },
    });
  }

  findEmployeesDyflexisByJobId(
    jobId: string,
  ): Promise<EmployeeDyflexisSource[]> {
    return this.prisma.employeeDyflexisSource.findMany({
      where: {
        jobId,
      },
    });
  }

  findEmployeesDispoLiveByJobId(
    jobId: string,
  ): Promise<EmployeeDispoLiveSource[]> {
    return this.prisma.employeeDispoLiveSource.findMany({
      where: {
        jobId,
      },
    });
  }
}
