import { Inject, Injectable } from '@nestjs/common';
import { Prisma, WorkflowEventOutbox } from '@prisma/client';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { PrismaService } from 'nestjs-prisma';
import { Logger } from 'winston';

import { ConfigService } from '@nestjs/config';

@Injectable()
export class WorkflowEventOutboxService {
  constructor(
    private prisma: PrismaService,
    private readonly config: ConfigService,
    @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
  ) {}

  async createOne(
    workflowEventOutboxCreateArgs: Prisma.WorkflowEventOutboxCreateArgs,
  ) {
    return await this.prisma.workflowEventOutbox.create(
      workflowEventOutboxCreateArgs,
    );
  }

  async createMany(data: Prisma.WorkflowEventOutboxCreateManyArgs['data']) {
    return await this.prisma.workflowEventOutbox.createMany({
      data,
      skipDuplicates: true,
    });
  }

  async findMany(
    workflowEventOutboxFindManyArgs: Prisma.WorkflowEventOutboxFindManyArgs,
  ): Promise<{ itemCount: number; entities: WorkflowEventOutbox[] }> {
    const itemCount = await this.prisma.workflowEventOutbox.count({
      where: workflowEventOutboxFindManyArgs.where,
    });
    const entities = await this.prisma.workflowEventOutbox.findMany(
      workflowEventOutboxFindManyArgs,
    );
    return { itemCount, entities };
  }

  async findUnique(
    id: number,
    select: Prisma.WorkflowEventOutboxFindUniqueArgs['select'],
  ) {
    return await this.prisma.workflowEventOutbox.findUnique({
      where: { id: id },
      select,
    });
  }

  async updateMany(
    workflowEventOutboxUpdateManyArgs: Prisma.WorkflowEventOutboxUpdateManyArgs,
  ) {
    return await this.prisma.workflowEventOutbox.updateMany(
      workflowEventOutboxUpdateManyArgs,
    );
  }

  async updateUnique(
    id: number,
    data: Prisma.WorkflowEventOutboxUpdateManyArgs['data'],
  ) {
    return await this.prisma.workflowEventOutbox.update({
      where: { id: id },
      data,
    });
  }

  async deleteMany(
    workflowEventOutboxDeleteManyArgs: Prisma.WorkflowEventOutboxDeleteManyArgs,
  ) {
    return await this.prisma.workflowEventOutbox.deleteMany(
      workflowEventOutboxDeleteManyArgs,
    );
  }

  async deleteUnique(id: number) {
    await this.prisma.workflowEventOutbox.delete({
      where: { id: id },
    });
    return { count: 1 };
  }

  async count(
    workflowEventOutboxCountArgs?: Prisma.WorkflowEventOutboxCountArgs,
  ) {
    return await this.prisma.workflowEventOutbox.count(
      workflowEventOutboxCountArgs,
    );
  }

  async groupBy(
    workflowEventOutboxGroupByArgs: Prisma.WorkflowEventOutboxGroupByArgs,
  ) {
    return await this.prisma.workflowEventOutbox.groupBy(
      workflowEventOutboxGroupByArgs as any,
    );
  }

  async aggregate(
    workflowEventOutboxAggregateArgs: Prisma.WorkflowEventOutboxAggregateArgs,
  ) {
    return await this.prisma.workflowEventOutbox.aggregate(
      workflowEventOutboxAggregateArgs,
    );
  }
}
