import testUsers from './test-users.json' with { type: 'json' };
import roles from './roles.json' with { type: 'json' };

export interface TestUser {
  username: string;
  password: string;
  email: string;
  role: string;
}

export interface TestRole {
  name: string;
  permissions: string[];
  description: string;
}

export class TestData {
  static getAdminUser(): TestUser {
    return testUsers.admin;
  }

  static getRegularUser(): TestUser {
    return testUsers.regularUser;
  }

  static getInvalidUser(): TestUser {
    return testUsers.invalidUser;
  }

  static generateTestUser(role: string = 'user'): TestUser {
    const timestamp = Date.now();
    return {
      username: `testuser_${timestamp}`,
      password: `Test123!_${timestamp}`,
      email: `testuser_${timestamp}@example.com`,
      role: role
    };
  }

  static getRole(roleName: string): TestRole | undefined {
    return roles[roleName as keyof typeof roles];
  }

  static getAllRoles(): Record<string, TestRole> {
    return roles;
  }

  static getTestUserCredentials(username: string): TestUser | undefined {
    return Object.values(testUsers).find(user => user.username === username);
  }

  static validatePassword(password: string): boolean {
    // 密码强度验证：至少8个字符，包含大小写字母和数字
    const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/;
    return passwordRegex.test(password);
  }

  static generateRandomString(length: number = 10): string {
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    let result = '';
    for (let i = 0; i < length; i++) {
      result += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    return result;
  }

  static generateTestEmail(): string {
    return `test_${Date.now()}@example.com`;
  }

  static generateTestPhone(): string {
    // 生成随机手机号
    const prefix = '138';
    const suffix = Math.floor(10000000 + Math.random() * 90000000);
    return prefix + suffix.toString().substring(0, 8);
  }
}

export default TestData;