import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import {
  IntegrationTestDatabase,
  setupIntegrationDatabaseHooksWithEntities
} from '@d8d/shared-test-util';
import { MiniAuthService } from '../../src/services/index';
import { SystemConfigService } from '@d8d/core-module/system-config-module';
import { SystemConfig } from '@d8d/core-module/system-config-module';
import { UserEntity, Role } from '@d8d/core-module/user-module';
import { File } from '@d8d/core-module/file-module';

// 设置集成测试钩子
setupIntegrationDatabaseHooksWithEntities([SystemConfig, UserEntity, Role, File])

/**
 * 认证模块系统配置集成测试
 * 验证MiniAuthService与SystemConfigService的集成
 */
describe('认证模块系统配置集成测试', () => {
  let miniAuthService: MiniAuthService;
  let systemConfigService: SystemConfigService;
  const testTenantId = 1;

  beforeEach(async () => {
    // 获取数据源
    const dataSource = await IntegrationTestDatabase.getDataSource();
    if (!dataSource) throw new Error('Database not initialized');

    miniAuthService = new MiniAuthService(dataSource);
    systemConfigService = new SystemConfigService(dataSource);

    // 清理测试配置
    await systemConfigService.deleteConfig('wx.mini.app.id');
    await systemConfigService.deleteConfig('wx.mini.app.secret');
  });

  afterEach(async () => {
    // 清理测试配置
    await systemConfigService.deleteConfig('wx.mini.app.id');
    await systemConfigService.deleteConfig('wx.mini.app.secret');
  });

  describe('系统配置读取功能', () => {
    it('应该从系统配置获取小程序AppID和Secret', async () => {
      // 设置测试配置
      await systemConfigService.setConfig('wx.mini.app.id', 'test-app-id', '测试小程序AppID');
      await systemConfigService.setConfig('wx.mini.app.secret', 'test-app-secret', '测试小程序AppSecret');

      // 验证配置读取
      const appId = await systemConfigService.getConfigByKey('wx.mini.app.id');
      const appSecret = await systemConfigService.getConfigByKey('wx.mini.app.secret');

      expect(appId).toBe('test-app-id');
      expect(appSecret).toBe('test-app-secret');
    });

    it('应该支持批量获取配置', async () => {
      // 设置测试配置
      await systemConfigService.setConfig('wx.mini.app.id', 'test-app-id', '测试小程序AppID');
      await systemConfigService.setConfig('wx.mini.app.secret', 'test-app-secret', '测试小程序AppSecret');

      // 批量获取配置
      const configs = await systemConfigService.getConfigsByKeys(
        ['wx.mini.app.id', 'wx.mini.app.secret']
      );

      expect(configs['wx.mini.app.id']).toBe('test-app-id');
      expect(configs['wx.mini.app.secret']).toBe('test-app-secret');
    });
  });

  // 租户配置隔离测试已移除，因为core-module是非多租户版本

  describe('配置缓存功能', () => {
    it('应该支持配置缓存', async () => {
      // 设置配置
      await systemConfigService.setConfig('wx.mini.app.id', 'cached-app-id', '缓存测试小程序AppID');

      // 第一次读取（应该写入缓存）
      const firstRead = await systemConfigService.getConfigByKey('wx.mini.app.id');
      expect(firstRead).toBe('cached-app-id');

      // 第二次读取（应该从缓存读取）
      const secondRead = await systemConfigService.getConfigByKey('wx.mini.app.id');
      expect(secondRead).toBe('cached-app-id');
    });

    it('应该支持空值缓存', async () => {
      // 读取不存在的配置（应该设置空值缓存）
      const nonExistentConfig = await systemConfigService.getConfigByKey('non.existent.key');
      expect(nonExistentConfig).toBeNull();

      // 再次读取（应该从空值缓存返回）
      const cachedRead = await systemConfigService.getConfigByKey('non.existent.key');
      expect(cachedRead).toBeNull();
    });
  });

  describe('MiniAuthService集成', () => {
    it('应该正确注入SystemConfigService依赖', () => {
      // 验证服务实例化
      expect(miniAuthService).toBeDefined();
      expect(miniAuthService).toBeInstanceOf(MiniAuthService);
    });

    it('应该支持租户ID参数', async () => {
      const method = miniAuthService.miniLogin;
      expect(typeof method).toBe('function');

      expect(method.length).toBeGreaterThanOrEqual(1);
    });
  });
});