import { test as base } from '@playwright/test';

export interface TestUser {
  username: string;
  password: string;
  email: string;
  phone?: string;
}

export interface TestRole {
  roleName: string;
  roleKey: string;
  roleSort?: string;
  status?: string;
  remark?: string;
}

export interface TestMenu {
  menuName: string;
  parentId: number;
  orderNum: number;
  menuType: string;
  component?: string;
  perms?: string;
  status?: number;
}

type TestData = {
  adminUser: TestUser;
  regularUser: TestUser;
  testRole: TestRole;
  testMenu: TestMenu;
  generateTestUser: () => TestUser;
  generateTestRole: () => TestRole;
  generateTestMenu: () => TestMenu;
};

export const test = base.extend<TestData>({
  adminUser: async ({}, use) => {
    const user: TestUser = {
      username: 'admin',
      password: 'password',
      email: 'admin@example.com',
      phone: '13800138000',
    };
    await use(user);
  },

  regularUser: async ({}, use) => {
    const user: TestUser = {
      username: 'testuser',
      password: 'Test123!@#',
      email: 'testuser@example.com',
      phone: '13800138001',
    };
    await use(user);
  },

  testRole: async ({}, use) => {
    const role: TestRole = {
      roleName: '测试角色',
      roleKey: 'test_role',
      roleSort: '1',
      status: '1',
      remark: '测试角色备注',
    };
    await use(role);
  },

  testMenu: async ({}, use) => {
    const menu: TestMenu = {
      menuName: '测试菜单',
      parentId: 0,
      orderNum: 1,
      menuType: 'M',
      component: 'test',
      perms: 'test:view',
      status: 1,
    };
    await use(menu);
  },

  generateTestUser: async ({}, use) => {
    const timestamp = Date.now();
    const user: TestUser = {
      username: `testuser_${timestamp}`,
      password: 'Test123!@#',
      email: `test_${timestamp}@example.com`,
      phone: `138${String(timestamp).slice(-8)}`,
    };
    await use(() => user);
  },

  generateTestRole: async ({}, use) => {
    const timestamp = Date.now();
    const role: TestRole = {
      roleName: `测试角色_${timestamp}`,
      roleKey: `test_role_${timestamp}`,
      roleSort: '1',
      status: '1',
      remark: `测试角色备注_${timestamp}`,
    };
    await use(() => role);
  },

  generateTestMenu: async ({}, use) => {
    const timestamp = Date.now();
    const menu: TestMenu = {
      menuName: `测试菜单_${timestamp}`,
      parentId: 0,
      orderNum: 1,
      menuType: 'M',
      component: `test_${timestamp}`,
      perms: `test:view_${timestamp}`,
      status: 1,
    };
    await use(() => menu);
  },
});
