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

import { ConfigService } from '@nestjs/config';
import * as path from 'path';

@Injectable()
export class AuditService {
  constructor(
    private prisma: PrismaService,
    private readonly config: ConfigService,
    @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
  ) {
    this.logger = this.logger.child({
      file: path.basename(__filename),
    });
  }

  async createOne(auditCreateArgs: Prisma.AuditCreateArgs) {
    return await this.prisma.audit.create(auditCreateArgs);
  }

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

  async findMany(
    auditFindManyArgs: Prisma.AuditFindManyArgs,
  ): Promise<{ itemCount: number; entities: Audit[] }> {
    const itemCount = await this.prisma.audit.count({
      where: auditFindManyArgs.where,
    });
    const entities = await this.prisma.audit.findMany(auditFindManyArgs);
    return { itemCount, entities };
  }

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

  async updateMany(auditUpdateManyArgs: Prisma.AuditUpdateManyArgs) {
    return await this.prisma.audit.updateMany(auditUpdateManyArgs);
  }

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

  async deleteMany(auditDeleteManyArgs: Prisma.AuditDeleteManyArgs) {
    return await this.prisma.audit.deleteMany(auditDeleteManyArgs);
  }

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

  async count(auditCountArgs?: Prisma.AuditCountArgs) {
    return await this.prisma.audit.count(auditCountArgs);
  }

  async groupBy(auditGroupByArgs: Prisma.AuditGroupByArgs) {
    return await this.prisma.audit.groupBy(auditGroupByArgs as any);
  }

  async aggregate(auditAggregateArgs: Prisma.AuditAggregateArgs) {
    return await this.prisma.audit.aggregate(auditAggregateArgs);
  }
}
