export interface UserData {
  username: string;
  nickname: string;
  email: string;
  phone: string;
  password: string;
  confirmPassword: string;
}

export interface RoleData {
  roleName: string;
  roleKey: string;
  roleSort: number;
  status: string;
}

export interface MenuData {
  menuName: string;
  menuType?: string;
  path?: string;
  component?: string;
  permission?: string;
  sort?: number;
  visible?: string;
  status?: string;
}

export interface DictTypeData {
  dictName: string;
  dictType: string;
  status: string;
  remark?: string;
}

export interface DictDataData {
  dictLabel: string;
  dictValue: string;
  dictType: string;
  status: string;
  sort?: number;
}

export class TestDataFactory {
  static generateTimestamp(): string {
    return Date.now().toString();
  }

  static generateRandomString(length: number = 8): 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 generateValidEmail(username: string): string {
    return `${username}@example.com`;
  }

  static generateValidPhone(): string {
    const prefix = ['138', '139', '150', '151', '186', '188'];
    const selectedPrefix = prefix[Math.floor(Math.random() * prefix.length)];
    const suffix = Math.floor(Math.random() * 100000000).toString().padStart(8, '0');
    return selectedPrefix + suffix;
  }

  static generateValidPassword(): string {
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*';
    let password = '';
    for (let i = 0; i < 12; i++) {
      password += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    return password;
  }

  static createUser(suffix?: string): UserData {
    const timestamp = this.generateTimestamp();
    const uniqueSuffix = suffix || this.generateRandomString(4);
    
    return {
      username: `testuser_${uniqueSuffix}_${timestamp}`,
      nickname: `测试用户_${uniqueSuffix}_${timestamp}`,
      email: this.generateValidEmail(`testuser_${uniqueSuffix}_${timestamp}`),
      phone: this.generateValidPhone(),
      password: this.generateValidPassword(),
      confirmPassword: this.generateValidPassword()
    };
  }

  static createAdminUser(): UserData {
    return {
      username: 'admin',
      nickname: '管理员',
      email: 'admin@example.com',
      phone: '13800138000',
      password: 'admin123',
      confirmPassword: 'admin123'
    };
  }

  static createRole(suffix?: string): RoleData {
    const timestamp = this.generateTimestamp();
    const uniqueSuffix = suffix || this.generateRandomString(4);
    
    return {
      roleName: `testrole_${uniqueSuffix}_${timestamp}`,
      roleKey: `test_role_${uniqueSuffix}_${timestamp}`,
      roleSort: 1,
      status: '1'
    };
  }

  static createAdminRole(): RoleData {
    return {
      roleName: '管理员',
      roleKey: 'admin',
      roleSort: 1,
      status: '1'
    };
  }

  static createMenu(suffix?: string, parentId?: string): MenuData {
    const timestamp = this.generateTimestamp();
    const uniqueSuffix = suffix || this.generateRandomString(4);
    
    return {
      menuName: `测试菜单_${uniqueSuffix}_${timestamp}`,
      menuType: 'M',
      path: `/testmenu_${uniqueSuffix}_${timestamp}`,
      component: `TestMenu${uniqueSuffix}`,
      permission: `system:testmenu:${uniqueSuffix}:${timestamp}`,
      sort: 1,
      visible: '0',
      status: '0'
    };
  }

  static createSubMenu(parentId: string, suffix?: string): MenuData {
    const menuData = this.createMenu(suffix);
    menuData.menuType = 'C';
    menuData.path = `${menuData.path}/submenu`;
    return menuData;
  }

  static createDictType(suffix?: string): DictTypeData {
    const timestamp = this.generateTimestamp();
    const uniqueSuffix = suffix || this.generateRandomString(4);
    
    return {
      dictName: `测试字典类型_${uniqueSuffix}_${timestamp}`,
      dictType: `test_dict_type_${uniqueSuffix}_${timestamp}`,
      status: '0',
      remark: `测试字典类型备注_${uniqueSuffix}_${timestamp}`
    };
  }

  static createDictData(dictType: string, suffix?: string): DictDataData {
    const timestamp = this.generateTimestamp();
    const uniqueSuffix = suffix || this.generateRandomString(4);
    
    return {
      dictLabel: `测试字典数据_${uniqueSuffix}_${timestamp}`,
      dictValue: `test_dict_value_${uniqueSuffix}_${timestamp}`,
      dictType: dictType,
      status: '0',
      sort: 1
    };
  }

  static createBatchUsers(count: number): UserData[] {
    const users: UserData[] = [];
    for (let i = 0; i < count; i++) {
      users.push(this.createUser(`batch_${i}`));
    }
    return users;
  }

  static createBatchRoles(count: number): RoleData[] {
    const roles: RoleData[] = [];
    for (let i = 0; i < count; i++) {
      roles.push(this.createRole(`batch_${i}`));
    }
    return roles;
  }

  static createBatchMenus(count: number): MenuData[] {
    const menus: MenuData[] = [];
    for (let i = 0; i < count; i++) {
      menus.push(this.createMenu(`batch_${i}`));
    }
    return menus;
  }

  static createBatchDictTypes(count: number): DictTypeData[] {
    const dictTypes: DictTypeData[] = [];
    for (let i = 0; i < count; i++) {
      dictTypes.push(this.createDictType(`batch_${i}`));
    }
    return dictTypes;
  }

  static createBatchDictData(dictType: string, count: number): DictDataData[] {
    const dictData: DictDataData[] = [];
    for (let i = 0; i < count; i++) {
      dictData.push(this.createDictData(dictType, `batch_${i}`));
    }
    return dictData;
  }

  static createInvalidUser(): UserData {
    return {
      username: '',
      nickname: '',
      email: 'invalid-email',
      phone: 'invalid-phone',
      password: 'weak',
      confirmPassword: 'different'
    };
  }

  static createInvalidRole(): RoleData {
    return {
      roleName: '',
      roleKey: '',
      roleSort: -1,
      status: 'invalid'
    };
  }

  static createInvalidMenu(): MenuData {
    return {
      menuName: '',
      menuType: 'invalid',
      path: '',
      component: '',
      permission: '',
      sort: -1,
      visible: 'invalid',
      status: 'invalid'
    };
  }

  static createLongString(length: number = 1000): string {
    return this.generateRandomString(length);
  }

  static createSpecialCharsString(): string {
    return '!@#$%^&*()_+-=[]{}|;:,.<>?/~`';
  }

  static createUnicodeString(): string {
    return '测试中文🎉🚀';
  }
}
