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

test.describe('Stats/Dashboard 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('Dashboard Page', () => {
    test('should display dashboard after login', async ({ page }) => {
      await login(page)

      // Default redirect should be dashboard
      await expect(page).toHaveURL(/\/dashboard/)
    })

    test('should display stats cards', async ({ page }) => {
      await login(page)
      await page.goto('/dashboard')

      // Check for stat card elements
      await expect(page.locator('.dashboard__stats')).toBeVisible()
    })

    test('should show machine statistics', async ({ page }) => {
      await login(page)
      await page.goto('/dashboard')

      // Look for machine related text
      await expect(page.getByText('机器总数')).toBeVisible()
    })

    test('should show camera statistics', async ({ page }) => {
      await login(page)
      await page.goto('/dashboard')

      // Look for camera related text
      await expect(page.getByText('摄像头总数')).toBeVisible()
    })
  })

  test.describe('Navigation', () => {
    test('should redirect to dashboard after login', async ({ page }) => {
      await login(page)

      await expect(page).toHaveURL(/\/dashboard/)
    })
  })
})
