import { Inject, Injectable } from '@nestjs/common';
import { Prisma, RolePages } 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 { createSafeLogData } from '../../lib/safe-log.util';

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

  async createOne(rolePagesCreateArgs: Prisma.RolePagesCreateArgs) {
    this.logger.info(
      '[RolePages] Creating role page',
      createSafeLogData({
        operation: 'CREATE',
        table: 'rolePages',
        data: rolePagesCreateArgs.data,
      }),
    );
    const result = await this.prisma.rolePages.create(rolePagesCreateArgs);
    this.logger.info(
      '[RolePages] Role page created successfully',
      createSafeLogData({
        operation: 'CREATE',
        table: 'rolePages',
        result: result,
      }),
    );
    return result;
  }

  async createMany(data: Prisma.RolePagesCreateManyArgs['data']) {
    this.logger.info(
      '[RolePages] Creating multiple role pages',
      createSafeLogData({
        operation: 'CREATE_MANY',
        table: 'rolePages',
        count: Array.isArray(data) ? data.length : 1,
        data: data,
      }),
    );
    const result = await this.prisma.rolePages.createMany({
      data,
      skipDuplicates: true,
    });
    this.logger.info(
      '[RolePages] Multiple role pages created successfully',
      createSafeLogData({
        operation: 'CREATE_MANY',
        table: 'rolePages',
        count: result.count,
      }),
    );
    return result;
  }

  async findMany(
    rolePagesFindManyArgs: Prisma.RolePagesFindManyArgs,
  ): Promise<{ itemCount: number; entities: RolePages[] }> {
    const itemCount = await this.prisma.rolePages.count({
      where: rolePagesFindManyArgs.where,
    });
    const entities = await this.prisma.rolePages.findMany(
      rolePagesFindManyArgs,
    );
    return { itemCount, entities };
  }

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

  async updateMany(rolePagesUpdateManyArgs: Prisma.RolePagesUpdateManyArgs) {
    this.logger.info(
      '[RolePages] Updating multiple role pages',
      createSafeLogData({
        operation: 'UPDATE_MANY',
        table: 'rolePages',
        where: rolePagesUpdateManyArgs.where,
        data: rolePagesUpdateManyArgs.data,
      }),
    );
    const result = await this.prisma.rolePages.updateMany(
      rolePagesUpdateManyArgs,
    );
    this.logger.info(
      '[RolePages] Multiple role pages updated successfully',
      createSafeLogData({
        operation: 'UPDATE_MANY',
        table: 'rolePages',
        count: result.count,
      }),
    );
    return result;
  }

  async updateUnique(id: number, data: Prisma.RolePagesUpdateManyArgs['data']) {
    const oldData = await this.prisma.rolePages.findUnique({ where: { id } });
    this.logger.info(
      '[RolePages] Updating role page',
      createSafeLogData({
        operation: 'UPDATE',
        table: 'rolePages',
        id: id,
        oldData: oldData,
        newData: data,
      }),
    );
    const result = await this.prisma.rolePages.update({
      where: { id: id },
      data,
    });
    this.logger.info(
      '[RolePages] Role page updated successfully',
      createSafeLogData({
        operation: 'UPDATE',
        table: 'rolePages',
        id: id,
        result: result,
      }),
    );
    return result;
  }

  async deleteMany(rolePagesDeleteManyArgs: Prisma.RolePagesDeleteManyArgs) {
    const oldData = await this.prisma.rolePages.findMany({
      where: rolePagesDeleteManyArgs.where,
    });
    this.logger.info(
      '[RolePages] Deleting multiple role pages',
      createSafeLogData({
        operation: 'DELETE_MANY',
        table: 'rolePages',
        where: rolePagesDeleteManyArgs.where,
        oldData: oldData,
      }),
    );
    const result = await this.prisma.rolePages.deleteMany(
      rolePagesDeleteManyArgs,
    );
    this.logger.info(
      '[RolePages] Multiple role pages deleted successfully',
      createSafeLogData({
        operation: 'DELETE_MANY',
        table: 'rolePages',
        count: result.count,
      }),
    );
    return result;
  }

  async deleteUnique(id: number) {
    const oldData = await this.prisma.rolePages.findUnique({ where: { id } });
    this.logger.info(
      '[RolePages] Deleting role page',
      createSafeLogData({
        operation: 'DELETE',
        table: 'rolePages',
        id: id,
        oldData: oldData,
      }),
    );
    await this.prisma.rolePages.delete({
      where: { id: id },
    });
    this.logger.info(
      '[RolePages] Role page deleted successfully',
      createSafeLogData({
        operation: 'DELETE',
        table: 'rolePages',
        id: id,
      }),
    );
    return { count: 1 };
  }

  async count(rolePagesCountArgs?: Prisma.RolePagesCountArgs) {
    return await this.prisma.rolePages.count(rolePagesCountArgs);
  }

  async groupBy(rolePagesGroupByArgs: Prisma.RolePagesGroupByArgs) {
    return await this.prisma.rolePages.groupBy(rolePagesGroupByArgs as any);
  }

  async aggregate(rolePagesAggregateArgs: Prisma.RolePagesAggregateArgs) {
    return await this.prisma.rolePages.aggregate(rolePagesAggregateArgs);
  }
}
