import { test, expect, Page } from '@playwright/test';

type RoleKey = 'admin' | 'school' | 'teacher' | 'parent';

const ROLE_CONFIG: Record<
  RoleKey,
  {
    label: string;
    account: string;
    password: string;
    dashboardPath: string;
  }
> = {
  admin: {
    label: '超管',
    account: 'admin',
    password: '123456',
    dashboardPath: '/admin/dashboard',
  },
  school: {
    label: '学校',
    account: 'school',
    password: '123456',
    dashboardPath: '/school/dashboard',
  },
  teacher: {
    label: '教师',
    account: 'teacher1',
    password: '123456',
    dashboardPath: '/teacher/dashboard',
  },
  parent: {
    label: '家长',
    account: 'parent1',
    password: '123456',
    dashboardPath: '/parent/dashboard',
  },
};

async function loginAsRole(page: Page, role: RoleKey) {
  const cfg = ROLE_CONFIG[role];

  await page.goto('/login');

  await page.getByText(cfg.label).click();

  await page.getByPlaceholder('请输入账号').fill(cfg.account);
  await page.getByPlaceholder('请输入密码').fill(cfg.password);

  await page.getByRole('button', { name: '登录' }).click();

  await page.waitForURL(`**${cfg.dashboardPath}*`);

  await expect(page).toHaveURL(new RegExp(`${cfg.dashboardPath}`));
  await expect(page).toHaveTitle(/少儿智慧阅读/);
}

test.describe('从登录开始的一键全角色流程', () => {
  for (const role of Object.keys(ROLE_CONFIG) as RoleKey[]) {
    test(`角色：${ROLE_CONFIG[role].label} 登录并进入首页`, async ({ page }) => {
      await loginAsRole(page, role);

      await expect(page.getByText('数据看板').or(page.getByText('首页'))).toBeVisible({
        timeout: 10_000,
      });
    });
  }
});

