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

test.describe('Camera Management E2E Tests', () => {
  const username = process.env.TEST_USERNAME || 'admin'
  const password = process.env.TEST_PASSWORD || '123456'

  async function login(page: any) {
    await page.goto('/login')
    await page.getByPlaceholder('请输入用户名').fill(username)
    await page.getByPlaceholder('请输入密码').fill(password)
    await page.getByRole('button', { name: '登 录' }).click()
    await expect(page).not.toHaveURL(/\/login/, { timeout: 10000 })
  }

  test.describe('Camera List Page', () => {
    test.skip('should display camera management page', async ({ page }) => {
      await login(page)
      await page.goto('/camera')

      await expect(page.getByRole('button', { name: '新增摄像头' })).toBeVisible()
      await expect(page.getByRole('button', { name: '刷新列表' })).toBeVisible()
      await expect(page.locator('.el-table')).toBeVisible()
    })

    test.skip('should have correct table columns', async ({ page }) => {
      await login(page)
      await page.goto('/camera')

      await expect(page.getByText('摄像头ID')).toBeVisible()
      await expect(page.getByText('名称')).toBeVisible()
      await expect(page.getByText('IP地址')).toBeVisible()
      await expect(page.getByText('所属机器')).toBeVisible()
      await expect(page.getByText('状态')).toBeVisible()
    })

    test.skip('should have machine filter', async ({ page }) => {
      await login(page)
      await page.goto('/camera')

      await expect(page.getByPlaceholder('请选择机器')).toBeVisible()
    })
  })

  test.describe('Add Camera Dialog', () => {
    test.skip('should open add dialog when clicking add button', async ({ page }) => {
      await login(page)
      await page.goto('/camera')

      await page.getByRole('button', { name: '新增摄像头' }).click()

      await expect(page.getByRole('dialog')).toBeVisible()
      await expect(page.getByText('新增摄像头')).toBeVisible()
      await expect(page.getByPlaceholder('请输入摄像头ID')).toBeVisible()
      await expect(page.getByPlaceholder('请输入IP地址')).toBeVisible()
    })

    test.skip('should validate required fields', async ({ page }) => {
      await login(page)
      await page.goto('/camera')

      await page.getByRole('button', { name: '新增摄像头' }).click()
      await page.getByRole('dialog').getByRole('button', { name: '确定' }).click()

      await expect(page.getByText('请输入摄像头ID')).toBeVisible()
      await expect(page.getByText('请输入名称')).toBeVisible()
      await expect(page.getByText('请输入IP地址')).toBeVisible()
    })

    test.skip('should validate IP address format', async ({ page }) => {
      await login(page)
      await page.goto('/camera')

      await page.getByRole('button', { name: '新增摄像头' }).click()
      await page.getByPlaceholder('请输入IP地址').fill('invalid-ip')
      await page.getByPlaceholder('请输入IP地址').blur()

      await expect(page.getByText('请输入正确的IP地址')).toBeVisible()
    })
  })

  test.describe('Navigation', () => {
    test.skip('should have camera menu item in sidebar', async ({ page }) => {
      await login(page)

      await expect(page.getByText('摄像头管理')).toBeVisible()
    })

    test.skip('should navigate to camera page from sidebar', async ({ page }) => {
      await login(page)

      await page.getByText('摄像头管理').click()
      await expect(page).toHaveURL(/\/camera/)
    })
  })
})
