import { Injectable } from '@nestjs/common';
import {
  AttendanceRegistrationStaging,
  EmployeeDispoLiveStaging,
  EmployeeDyflexisStaging,
  PatientStaging,
  PlannedTimeStaging,
  Prisma,
  TourStaging,
  WorkTimeStaging,
} from '@prisma/client';
import { PrismaService } from 'src/core/database/prisma.service';
import {
  AttendanceRegistrationPreStaging,
  EmployeeDispoLivePreStaging,
  EmployeeDyflexisPreStaging,
  PatientPreStaging,
  PlannedTimePreStaging,
  TourPreStaging,
  WorkTimePreStaging,
} from './staging.interface';

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

  clearStaging(): Promise<Prisma.BatchPayload[]> {
    return Promise.all([
      this.prisma.tourStaging.deleteMany(),
      this.prisma.employeeDispoLiveStaging.deleteMany(),
      this.prisma.employeeDyflexisStaging.deleteMany(),
      this.prisma.workTimeStaging.deleteMany(),
      this.prisma.plannedTimeStaging.deleteMany(),
      this.prisma.attendanceRegistrationStaging.deleteMany(),
      this.prisma.patientStaging.deleteMany(),
    ]);
  }

  createPatients(patients: PatientPreStaging[]): Promise<Prisma.BatchPayload> {
    return this.prisma.patientStaging.createMany({
      data: patients,
    });
  }

  createPlannedTimes(
    plannedTimes: PlannedTimePreStaging[],
  ): Promise<Prisma.BatchPayload> {
    return this.prisma.plannedTimeStaging.createMany({
      data: plannedTimes,
    });
  }

  createWorkTimes(
    workTimes: WorkTimePreStaging[],
  ): Promise<Prisma.BatchPayload> {
    return this.prisma.workTimeStaging.createMany({
      data: workTimes,
    });
  }

  createAttendanceRegistrations(
    attendanceRegistrations: AttendanceRegistrationPreStaging[],
  ): Promise<Prisma.BatchPayload> {
    return this.prisma.attendanceRegistrationStaging.createMany({
      data: attendanceRegistrations,
    });
  }

  createTours(tours: TourPreStaging[]): Promise<Prisma.BatchPayload> {
    return this.prisma.tourStaging.createMany({
      data: tours,
    });
  }

  createDyflexisEmployees(
    employees: EmployeeDyflexisPreStaging[],
  ): Promise<Prisma.BatchPayload> {
    return this.prisma.employeeDyflexisStaging.createMany({
      data: employees,
    });
  }

  createDispoliveEmployees(
    employees: EmployeeDispoLivePreStaging[],
  ): Promise<Prisma.BatchPayload> {
    return this.prisma.employeeDispoLiveStaging.createMany({
      data: employees,
    });
  }

  findDyflexisEmployees(): Promise<EmployeeDyflexisStaging[]> {
    return this.prisma.employeeDyflexisStaging.findMany({
      orderBy: {
        surname: 'asc',
      },
    });
  }

  findDispoliveEmployees(): Promise<EmployeeDispoLiveStaging[]> {
    return this.prisma.employeeDispoLiveStaging.findMany({
      orderBy: {
        surname: 'asc',
      },
    });
  }

  findPatients(): Promise<PatientStaging[]> {
    return this.prisma.patientStaging.findMany({
      orderBy: {
        surname: 'asc',
      },
    });
  }

  findTours(): Promise<TourStaging[]> {
    return this.prisma.tourStaging.findMany({
      orderBy: {
        startDate: 'asc',
      },
    });
  }

  findAttendanceRegistrations(): Promise<AttendanceRegistrationStaging[]> {
    return this.prisma.attendanceRegistrationStaging.findMany({
      orderBy: {
        dateTime: 'asc',
      },
    });
  }

  findPlannedTimes(): Promise<PlannedTimeStaging[]> {
    return this.prisma.plannedTimeStaging.findMany({
      orderBy: {
        startDate: 'asc',
      },
    });
  }

  findWorkTimes(): Promise<WorkTimeStaging[]> {
    return this.prisma.workTimeStaging.findMany({
      orderBy: {
        startDate: 'asc',
      },
    });
  }
}
