import { Inject, Injectable } from '@nestjs/common';
import { Game, 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 GameService {
  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(gameCreateArgs: Prisma.GameCreateArgs) {
    return await this.prisma.game.create(gameCreateArgs);
  }

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

  async findMany(
    gameFindManyArgs: Prisma.GameFindManyArgs,
  ): Promise<{ itemCount: number; entities: Game[] }> {
    const itemCount = await this.prisma.game.count({
      where: gameFindManyArgs.where,
    });
    const entities = await this.prisma.game.findMany(gameFindManyArgs);
    return { itemCount, entities };
  }

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

  async updateMany(gameUpdateManyArgs: Prisma.GameUpdateManyArgs) {
    return await this.prisma.game.updateMany(gameUpdateManyArgs);
  }

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

  async deleteMany(gameDeleteManyArgs: Prisma.GameDeleteManyArgs) {
    return await this.prisma.game.deleteMany(gameDeleteManyArgs);
  }

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

  async count(gameCountArgs?: Prisma.GameCountArgs) {
    return await this.prisma.game.count(gameCountArgs);
  }

  async groupBy(gameGroupByArgs: Prisma.GameGroupByArgs) {
    return await this.prisma.game.groupBy(gameGroupByArgs as any);
  }

  async aggregate(gameAggregateArgs: Prisma.GameAggregateArgs) {
    return await this.prisma.game.aggregate(gameAggregateArgs);
  }
}
