/// <reference types="jest" />
import { ConfigService } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';

// Mock nestjs-prisma 模块
jest.mock('nestjs-prisma', () => ({
  PrismaService: jest.fn(),
}));

import { PrismaService } from 'nestjs-prisma';
import { RoleResourceService } from './roleResource.service';

// Mock PrismaService
const mockPrismaService = {
  roleResource: {
    create: jest.fn(),
    createMany: jest.fn(),
    findMany: jest.fn(),
    findUnique: jest.fn(),
    update: jest.fn(),
    updateMany: jest.fn(),
    delete: jest.fn(),
    deleteMany: jest.fn(),
    count: jest.fn(),
    groupBy: jest.fn(),
    aggregate: jest.fn(),
  },
};

// Mock Logger
const mockLogger = {
  child: jest.fn().mockReturnThis(),
  info: jest.fn(),
  error: jest.fn(),
  warn: jest.fn(),
  debug: jest.fn(),
};

// Mock ConfigService
const mockConfigService = {
  get: jest.fn(),
};

describe('RoleResourceService', () => {
  let service: RoleResourceService;
  let prisma: typeof mockPrismaService;

  beforeEach(async () => {
    // 每个测试前重置所有 mock
    jest.clearAllMocks();

    const module: TestingModule = await Test.createTestingModule({
      providers: [
        RoleResourceService,
        { provide: PrismaService, useValue: mockPrismaService },
        { provide: ConfigService, useValue: mockConfigService },
        { provide: WINSTON_MODULE_PROVIDER, useValue: mockLogger },
      ],
    }).compile();

    service = module.get<RoleResourceService>(RoleResourceService);
    prisma = mockPrismaService;
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });

  describe('createOne', () => {
    it('should create a single roleResource record', async () => {
      const createArgs = {
        data: {
          actions: {},
          creator: 'test_creator',
        },
      } as any;
      const expectedResult = { id: 1, ...createArgs.data };

      prisma.roleResource.create.mockResolvedValue(expectedResult);

      const result = await service.createOne(createArgs);

      expect(prisma.roleResource.create).toHaveBeenCalledWith(createArgs);
      expect(prisma.roleResource.create).toHaveBeenCalledTimes(1);
      expect(result).toEqual(expectedResult);
    });

    it('should throw error when create fails', async () => {
      const createArgs = {
        data: {
          actions: {},
          creator: 'test_creator',
        },
      } as any;
      const error = new Error('Database connection failed');

      prisma.roleResource.create.mockRejectedValue(error);

      await expect(service.createOne(createArgs)).rejects.toThrow(
        'Database connection failed',
      );
    });
  });

  describe('createMany', () => {
    it('should create multiple roleResource records with skipDuplicates', async () => {
      const data = [
        {
          actions: {},
          creator: 'test_creator',
        },
        {
          actions: {},
          creator: 'test_creator',
        },
      ] as any;
      const expectedResult = { count: 2 };

      prisma.roleResource.createMany.mockResolvedValue(expectedResult);

      const result = await service.createMany(data);

      expect(prisma.roleResource.createMany).toHaveBeenCalledWith({
        data,
        skipDuplicates: true,
      });
      expect(result).toEqual(expectedResult);
    });
  });

  describe('findMany', () => {
    it('should return entities with itemCount', async () => {
      const findArgs = {
        where: {},
        take: 10,
        skip: 0,
      } as any;
      const mockEntities = [{ id: 1 }, { id: 2 }];

      prisma.roleResource.count.mockResolvedValue(2);
      prisma.roleResource.findMany.mockResolvedValue(mockEntities);

      const result = await service.findMany(findArgs);

      expect(prisma.roleResource.count).toHaveBeenCalledWith({
        where: findArgs.where,
      });
      expect(prisma.roleResource.findMany).toHaveBeenCalledWith(findArgs);
      expect(result).toEqual({
        itemCount: 2,
        entities: mockEntities,
      });
    });

    it('should return empty result when no records found', async () => {
      const findArgs = { where: {} } as any;

      prisma.roleResource.count.mockResolvedValue(0);
      prisma.roleResource.findMany.mockResolvedValue([]);

      const result = await service.findMany(findArgs);

      expect(result).toEqual({ itemCount: 0, entities: [] });
    });
  });

  describe('findUnique', () => {
    it('should find a single roleResource record by id', async () => {
      const id = 1;
      const select = { id: true };
      const expectedResult = { id: 1 };

      prisma.roleResource.findUnique.mockResolvedValue(expectedResult);

      const result = await service.findUnique(id, select);

      expect(prisma.roleResource.findUnique).toHaveBeenCalledWith({
        where: { id },
        select,
      });
      expect(result).toEqual(expectedResult);
    });

    it('should return null when record not found', async () => {
      prisma.roleResource.findUnique.mockResolvedValue(null);

      const result = await service.findUnique(1, { id: true });

      expect(result).toBeNull();
    });
  });

  describe('updateUnique', () => {
    it('should update a single roleResource record', async () => {
      const id = 1;
      const data = {
        actions: {},
        creator: 'test_creator',
      } as any;
      const expectedResult = { id: 1, ...data };

      prisma.roleResource.update.mockResolvedValue(expectedResult);

      const result = await service.updateUnique(id, data);

      expect(prisma.roleResource.update).toHaveBeenCalledWith({
        where: { id },
        data,
      });
      expect(result).toEqual(expectedResult);
    });
  });

  describe('updateMany', () => {
    it('should update multiple roleResource records', async () => {
      const updateArgs = {
        where: {},
        data: {
          actions: {},
          creator: 'test_creator',
        },
      } as any;
      const expectedResult = { count: 5 };

      prisma.roleResource.updateMany.mockResolvedValue(expectedResult);

      const result = await service.updateMany(updateArgs);

      expect(prisma.roleResource.updateMany).toHaveBeenCalledWith(updateArgs);
      expect(result).toEqual(expectedResult);
    });
  });

  describe('deleteUnique', () => {
    it('should delete a single roleResource record and return count', async () => {
      const id = 1;

      prisma.roleResource.delete.mockResolvedValue({ id: 1 });

      const result = await service.deleteUnique(id);

      expect(prisma.roleResource.delete).toHaveBeenCalledWith({
        where: { id },
      });
      expect(result).toEqual({ count: 1 });
    });
  });

  describe('deleteMany', () => {
    it('should delete multiple roleResource records', async () => {
      const deleteArgs = { where: {} } as any;
      const expectedResult = { count: 3 };

      prisma.roleResource.deleteMany.mockResolvedValue(expectedResult);

      const result = await service.deleteMany(deleteArgs);

      expect(prisma.roleResource.deleteMany).toHaveBeenCalledWith(deleteArgs);
      expect(result).toEqual(expectedResult);
    });
  });

  describe('count', () => {
    it('should count roleResource records with filter', async () => {
      const countArgs = { where: {} } as any;

      prisma.roleResource.count.mockResolvedValue(10);

      const result = await service.count(countArgs);

      expect(prisma.roleResource.count).toHaveBeenCalledWith(countArgs);
      expect(result).toBe(10);
    });

    it('should count all records when no filter provided', async () => {
      prisma.roleResource.count.mockResolvedValue(100);

      const result = await service.count();

      expect(prisma.roleResource.count).toHaveBeenCalledWith(undefined);
      expect(result).toBe(100);
    });
  });

  describe('groupBy', () => {
    it('should group roleResource records by specified field', async () => {
      const groupByArgs = {
        by: ['id'],
        _count: { id: true },
      } as any;
      const expectedResult = [{ id: 1, _count: { id: 50 } }];

      prisma.roleResource.groupBy.mockResolvedValue(expectedResult);

      const result = await service.groupBy(groupByArgs);

      expect(prisma.roleResource.groupBy).toHaveBeenCalledWith(groupByArgs);
      expect(result).toEqual(expectedResult);
    });
  });

  describe('aggregate', () => {
    it('should aggregate roleResource records', async () => {
      const aggregateArgs = {
        _count: true,
        _max: { id: true },
        _min: { id: true },
      } as any;
      const expectedResult = {
        _count: 100,
        _max: { id: 1 },
        _min: { id: 1 },
      };

      prisma.roleResource.aggregate.mockResolvedValue(expectedResult);

      const result = await service.aggregate(aggregateArgs);

      expect(prisma.roleResource.aggregate).toHaveBeenCalledWith(aggregateArgs);
      expect(result).toEqual(expectedResult);
    });
  });
});
