/**
 * Full-site UI acceptance — every SPA route + primary controls (no destructive push).
 * Run from frontend/: NEXUS_E2E_USER=admin NEXUS_E2E_PASSWORD=*** npm run test:e2e
 */
import { test, expect } from './fixtures.mjs'
import { APP, USER, login, nav } from './helpers.mjs'

test.describe.configure({ mode: 'serial' })

test('01 login + dashboard stats', async ({ page }) => {
  await login(page)
  await nav(page, '/')
  await expect(page.locator('.v-card').first()).toBeVisible({ timeout: 20000 })
})

test('02 servers: list, batch select, add dialog', async ({ page }) => {
  await login(page)
  await nav(page, '/servers')
  await page.getByRole('button', { name: '添加', exact: true }).click()
  await expect(page.getByText('添加服务器')).toBeVisible()
  await page.getByRole('button', { name: '取消' }).click()
  const cbs = page.locator('table tbody tr input[type="checkbox"]')
  if (await cbs.count() >= 2) {
    await cbs.nth(0).check()
    await cbs.nth(1).check()
    await expect(page.getByText(/已选择\s*2\s*台/)).toBeVisible()
  }
})

test('03 dashboard nav drawer', async ({ page }) => {
  await login(page)
  await nav(page, '/')
  await expect(page.locator('.v-card').first()).toBeVisible({ timeout: 20000 })
  await expect(page.getByRole('link', { name: '服务器' })).toBeVisible()
})

test('04 terminal page', async ({ page }) => {
  await login(page)
  await nav(page, '/terminal')
  await expect(page.getByText(/终端|服务器|连接/).first()).toBeVisible({ timeout: 20000 })
})

test('05 files page', async ({ page }) => {
  await login(page)
  await nav(page, '/files')
  await expect(page.getByText(/文件|远程|服务器/).first()).toBeVisible({ timeout: 20000 })
})

test('06 push: preview only', async ({ page }) => {
  await login(page)
  await nav(page, '/push')
  await page.getByRole('button', { name: '预览' }).click()
  await page.waitForTimeout(1500)
})

test('07 scripts: new dialog cancel', async ({ page }) => {
  await login(page)
  await nav(page, '/scripts')
  await page.getByRole('button', { name: '新建脚本' }).click()
  await expect(page.getByText('新建脚本').first()).toBeVisible()
  await page.getByRole('button', { name: '取消' }).click()
})

test('08 credentials: tabs', async ({ page }) => {
  await login(page)
  await nav(page, '/servers')
  await page.getByRole('button', { name: '凭据' }).click()
  const dialog = page.getByRole('dialog')
  await expect(dialog.getByText('凭据管理')).toBeVisible({ timeout: 20000 })
  await page.getByRole('button', { name: 'SSH 密钥' }).click()
  await page.getByRole('button', { name: '数据库凭据' }).click()
  await page.getByRole('button', { name: '密码预设' }).click()
  await expect(page.getByRole('dialog').getByRole('button', { name: '添加', exact: true })).toBeVisible()
})

test('09 schedules: new dialog cancel', async ({ page }) => {
  await login(page)
  await nav(page, '/schedules')
  await page.getByRole('button', { name: '新建调度' }).click()
  await expect(page.getByText('新建调度').first()).toBeVisible()
  await page.getByRole('button', { name: '取消' }).click()
})

test('10 retries list', async ({ page }) => {
  await login(page)
  await nav(page, '/retries')
  await expect(page.getByText(/重试|队列/).first()).toBeVisible({ timeout: 20000 })
})

test('11 commands: command, session and push log views', async ({ page }) => {
  await login(page)
  await nav(page, '/commands')
  await expect(page.locator('main .v-card-title').filter({ hasText: '命令日志' })).toBeVisible({
    timeout: 20000,
  })
  await page.locator('.v-card-title .v-select').first().click()
  await page.locator('.v-overlay-container').getByText('推送日志').click()
  await expect(page.locator('table').first()).toBeVisible()
  await page.locator('.v-card-title .v-select').first().click()
  await page.locator('.v-overlay-container').getByText('会话视图').click()
  await expect(page.locator('table').first()).toBeVisible()
})

test('12 alerts filters', async ({ page }) => {
  await login(page)
  await nav(page, '/alerts')
  await expect(page.getByText(/告警/).first()).toBeVisible({ timeout: 20000 })
})

test('13 audit pagination', async ({ page }) => {
  await login(page)
  await nav(page, '/audit')
  await expect(page.getByText(/审计/).first()).toBeVisible({ timeout: 20000 })
})

test('14 settings sections', async ({ page }) => {
  await login(page)
  await nav(page, '/settings')
  await expect(page.getByText(/系统|监控|Telegram|安全/).first()).toBeVisible({ timeout: 20000 })
})

test('15 theme toggle + logout', async ({ page }) => {
  await login(page)
  await nav(page, '/')
  await page.getByText(USER, { exact: false }).first().click()
  await page.getByText('切换主题').click()
  await page.getByText(USER, { exact: false }).first().click()
  await page.getByText('退出登录').click()
  await expect(page).toHaveURL(/#\/login/, { timeout: 15000 })
})
