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

test.beforeEach(async ({ page }) => {
  await page.addInitScript(() => {
    window.localStorage.setItem('dlbdi:staffToken', 'dev-admin-token')
  })
})

test('admin business workbenches render core operation surfaces', async ({ page }) => {
  test.setTimeout(60_000)
  const pages = [
    { path: '/admin/orders', heading: '订单流水' },
    { path: '/admin/invoices', heading: '发票中心' },
    { path: '/admin/events', heading: '活动管理' },
    { path: '/admin/data-service/requests', heading: '数据服务需求' },
    { path: '/admin/consulting-requests', heading: /咨询需求|咨询服务需求|规划咨询需求/ },
    { path: '/admin/tasks', heading: '交付任务' },
    { path: '/admin/expert-bookings', heading: '专家预约管理' },
    { path: '/admin/memberships', heading: '会员订阅' },
  ]

  for (const item of pages) {
    await page.goto(item.path, { waitUntil: 'domcontentloaded' })
    await expect(page.getByRole('heading', { name: item.heading })).toBeVisible()
    await expect(page.locator('body')).not.toContainText('Application error')
  }
})

test('admin checkin page exposes passport/manual registration flow', async ({ page }) => {
  await page.goto('/admin/events/checkin', { waitUntil: 'domcontentloaded' })
  await expect(page.getByText('通行证补录')).toBeVisible()
  await expect(page.getByPlaceholder(/通行证/)).toBeVisible()
  await expect(page.getByRole('button', { name: '查询' }).first()).toBeVisible()
  await expect(page.getByRole('button', { name: /加入名单|加入并核销/ }).first()).toBeVisible()
})

test('legacy registrations entry redirects to checkin workbench', async ({ page }) => {
  await page.goto('/admin/registrations', { waitUntil: 'domcontentloaded' })
  await expect(page).toHaveURL(/\/admin\/events\/checkin/)
  await expect(page.getByRole('heading', { name: '现场核销工作台' })).toBeVisible()
})

test('admin membership pages distinguish subscriptions from quota accounts', async ({ page }) => {
  await page.goto('/admin/memberships', { waitUntil: 'domcontentloaded' })
  await expect(page.getByRole('heading', { name: '会员订阅' })).toBeVisible()
  await expect(page.getByRole('link', { name: '权益额度账户' }).first()).toBeVisible()

  await page.goto('/admin/membership/entitlements', { waitUntil: 'domcontentloaded' })
  await expect(page.getByRole('heading', { name: '权益额度账户' })).toBeVisible()
  await expect(page.getByText(/只管理“次数\/额度型”的会员权益/)).toBeVisible()
})
