import { vi } from 'vitest';

/**
 * 创建模拟的用户服务
 */
export function createMockUserService() {
  return {
    getUserById: vi.fn().mockResolvedValue(null),
    getUserByUsername: vi.fn().mockResolvedValue(null),
    getUserByEmail: vi.fn().mockResolvedValue(null),
    createUser: vi.fn().mockResolvedValue({ id: 1, username: 'testuser' }),
    updateUser: vi.fn().mockResolvedValue({ affected: 1 }),
    deleteUser: vi.fn().mockResolvedValue({ affected: 1 }),
    changePassword: vi.fn().mockResolvedValue({ affected: 1 }),
    verifyPassword: vi.fn().mockResolvedValue(true),
    assignRole: vi.fn().mockResolvedValue({ affected: 1 }),
    removeRole: vi.fn().mockResolvedValue({ affected: 1 })
  };
}

/**
 * 创建模拟的认证服务
 */
export function createMockAuthService() {
  return {
    login: vi.fn().mockResolvedValue({
      token: 'mock-jwt-token',
      user: { id: 1, username: 'testuser' }
    }),
    logout: vi.fn().mockResolvedValue(undefined),
    register: vi.fn().mockResolvedValue({ id: 1, username: 'newuser' }),
    verifyToken: vi.fn().mockResolvedValue({ id: 1, username: 'testuser' }),
    refreshToken: vi.fn().mockResolvedValue({ token: 'new-mock-jwt-token' }),
    forgotPassword: vi.fn().mockResolvedValue({ success: true }),
    resetPassword: vi.fn().mockResolvedValue({ success: true })
  };
}

/**
 * 创建模拟的角色服务
 */
export function createMockRoleService() {
  return {
    getRoles: vi.fn().mockResolvedValue([]),
    getRoleById: vi.fn().mockResolvedValue(null),
    createRole: vi.fn().mockResolvedValue({ id: 1, name: 'admin' }),
    updateRole: vi.fn().mockResolvedValue({ affected: 1 }),
    deleteRole: vi.fn().mockResolvedValue({ affected: 1 }),
    assignPermission: vi.fn().mockResolvedValue({ affected: 1 }),
    removePermission: vi.fn().mockResolvedValue({ affected: 1 })
  };
}

/**
 * 创建模拟的通用CRUD服务
 */
export function createMockCrudService() {
  return {
    findAll: vi.fn().mockResolvedValue([]),
    findOne: vi.fn().mockResolvedValue(null),
    create: vi.fn().mockResolvedValue({ id: 1 }),
    update: vi.fn().mockResolvedValue({ affected: 1 }),
    delete: vi.fn().mockResolvedValue({ affected: 1 }),
    count: vi.fn().mockResolvedValue(0),
    exists: vi.fn().mockResolvedValue(false)
  };
}

/**
 * 创建模拟的邮件服务
 */
export function createMockEmailService() {
  return {
    sendWelcomeEmail: vi.fn().mockResolvedValue({ success: true }),
    sendPasswordResetEmail: vi.fn().mockResolvedValue({ success: true }),
    sendNotification: vi.fn().mockResolvedValue({ success: true }),
    verifyEmail: vi.fn().mockResolvedValue({ success: true })
  };
}

/**
 * 创建模拟的文件服务
 */
export function createMockFileService() {
  return {
    upload: vi.fn().mockResolvedValue({ url: 'https://example.com/file.jpg' }),
    download: vi.fn().mockResolvedValue(Buffer.from('test content')),
    delete: vi.fn().mockResolvedValue({ success: true }),
    getSignedUrl: vi.fn().mockResolvedValue('https://example.com/signed-url')
  };
}

/**
 * 服务stub工具类
 */
export class ServiceStubManager {
  private stubs = new Map<string, any>();

  /**
   * 创建服务stub
   */
  createStub<T>(serviceName: string, stubImplementation: Partial<T>): T {
    const stub = { ...stubImplementation } as T;
    this.stubs.set(serviceName, stub);
    return stub;
  }

  /**
   * 获取服务stub
   */
  getStub<T>(serviceName: string): T | undefined {
    return this.stubs.get(serviceName);
  }

  /**
   * 重置所有stub
   */
  resetAll(): void {
    this.stubs.clear();
  }

  /**
   * 重置特定stub
   */
  resetStub(serviceName: string): void {
    this.stubs.delete(serviceName);
  }
}

/**
 * 全局服务stub管理器
 */
export const serviceStubs = new ServiceStubManager();

/**
 * 设置服务mock
 */
export function setupServiceMocks() {
  // 用户服务mock
  vi.mock('@d8d/server/modules/users/user.service', () => ({
    UserService: vi.fn().mockImplementation(() => serviceStubs.createStub('UserService', createMockUserService()))
  }));

  // 认证服务mock
  vi.mock('@d8d/server/modules/auth/auth.service', () => ({
    AuthService: vi.fn().mockImplementation(() => serviceStubs.createStub('AuthService', createMockAuthService()))
  }));

  // 角色服务mock
  vi.mock('@d8d/server/modules/roles/role.service', () => ({
    RoleService: vi.fn().mockImplementation(() => serviceStubs.createStub('RoleService', createMockRoleService()))
  }));

  // 通用CRUD服务mock
  vi.mock('@d8d/server/utils/generic-crud.service', () => ({
    GenericCRUDService: vi.fn().mockImplementation(() => serviceStubs.createStub('GenericCRUDService', createMockCrudService()))
  }));
}