import { expect, test } from '../fixtures/auth';

test.describe('系统管理 / 用户管理', () => {
  test.beforeEach(async ({ page }) => {
    await page.goto('/system/user');
    await page.getByRole('table').waitFor();
  });

  test('展示用户列表并支持筛选', async ({ page }) => {
    await expect(page.getByRole('button', { name: '新增用户' })).toBeVisible();
    await expect(page.getByText('admin', { exact: true })).toBeVisible();

    await page.getByPlaceholder('搜索用户名 / 邮箱 / 手机号').fill('viewer');
    await expect(page.getByText('viewer', { exact: true })).toBeVisible();
    await expect(page.getByText('admin', { exact: true })).toHaveCount(0);
  });

  test('可新增用户', async ({ page }) => {
    await page.getByRole('button', { name: '新增用户' }).click();
    const dialog = page.getByRole('dialog', { name: '新增用户' });

    await dialog.getByLabel('用户名').fill('tester');
    await dialog.getByLabel('昵称').fill('测试用户');
    await dialog.getByLabel('邮箱').fill('tester@chatone.ai');
    await dialog.getByLabel('手机号').fill('13800000009');
    await dialog.getByLabel('初始密码').fill('Test123456');
    await dialog.getByRole('button', { name: '保 存' }).click();

    await expect(page.getByText('tester', { exact: true })).toBeVisible();
    await expect(page.getByText('用户已添加')).toBeVisible();
  });

  test('可切换状态并重置密码', async ({ page }) => {
    const statusSwitch = page.getByRole('switch', { name: 'operator状态' });
    await expect(statusSwitch).toBeChecked();
    await statusSwitch.click();
    await expect(statusSwitch).not.toBeChecked();

    const row = page.getByRole('row').filter({ hasText: 'operator' });
    await row.getByRole('button', { name: '重置密码' }).click();
    const dialog = page.getByRole('dialog', { name: '重置密码 - operator' });
    await dialog.getByLabel('新密码').fill('NewPass123');
    await dialog.getByLabel('确认密码').fill('NewPass123');
    await dialog.getByRole('button', { name: '确认重置' }).click();

    await expect(page.getByText('密码已重置')).toBeVisible();
  });

  test('可编辑并删除用户', async ({ page }) => {
    const operatorRow = page.getByRole('row').filter({ hasText: 'operator' });
    await operatorRow.getByRole('button', { name: '编辑' }).click();
    const dialog = page.getByRole('dialog', { name: '编辑用户' });
    await dialog.getByLabel('昵称').fill('新运营');
    await dialog.getByLabel('备注').fill('负责日常运营');
    await dialog.getByRole('button', { name: '保 存' }).click();
    await expect(page.getByText('新运营', { exact: true })).toBeVisible();

    await operatorRow.getByRole('button', { name: '编辑' }).click();
    await expect(page.getByRole('dialog', { name: '编辑用户' }).getByLabel('备注')).toHaveValue('负责日常运营');
    await page.getByRole('dialog', { name: '编辑用户' }).getByRole('button', { name: '取 消' }).click();

    const viewerRow = page.getByRole('row').filter({ hasText: 'viewer' });
    await viewerRow.getByRole('button', { name: '删除' }).click();
    await page.getByRole('button', { name: '确 定' }).click();
    await expect(page.getByText('viewer', { exact: true })).toHaveCount(0);
  });

  test('可批量禁用用户', async ({ page }) => {
    const row = page.getByRole('row').filter({ hasText: 'operator' });
    await row.getByRole('checkbox').check();
    await page.getByRole('button', { name: '批量禁用' }).click();
    await page.getByRole('button', { name: '确 定' }).click();

    await expect(page.getByRole('switch', { name: 'operator状态' })).not.toBeChecked();
    await expect(page.getByText('已禁用 1 个用户')).toBeVisible();
  });
});
