import { expect, test } from '@playwright/test';
import { loginThroughAdminUi } from './helpers/admin-auth';

test('check dashboard metrics', async ({ page }) => {
  const baseUrl = (process.env.E2E_BASE_URL || 'https://www.taxbaik.com').replace(/\/$/, '');
  const username = process.env.E2E_ADMIN_USERNAME;
  const password = process.env.E2E_ADMIN_PASSWORD;

  test.skip(!username || !password, 'E2E_ADMIN_USERNAME and E2E_ADMIN_PASSWORD are required.');

  await loginThroughAdminUi(page, baseUrl, username, password);
  await expect(page).toHaveURL(/\/admin\/dashboard$/);

  // Wait for page load
  await page.waitForSelector('.admin-page-hero', { timeout: 5000 });
  await page.waitForTimeout(2000);

  // Check for metric cards
  const metrics = page.locator('.admin-metric-card');
  const metricCount = await metrics.count();
  console.log(`\nFound ${metricCount} metric cards`);

  // Log each metric value
  for (let i = 0; i < Math.min(metricCount, 4); i++) {
    const metric = metrics.nth(i);
    const text = await metric.textContent();
    console.log(`  Metric ${i + 1}: ${text?.trim().slice(0, 100)}`);
  }

  // Check for data in main content area
  const contentText = await page.locator('body').textContent();
  const has총문의 = contentText?.includes('총 문의') || false;
  const has신규문의 = contentText?.includes('신규 문의') || false;
  const has활성공지 = contentText?.includes('활성 공지') || false;
  const has예정세무신고 = contentText?.includes('예정 세무신고') || false;

  console.log(`\nDashboard content check:`);
  console.log(`  - 총 문의: ${has총문의}`);
  console.log(`  - 신규 문의: ${has신규문의}`);
  console.log(`  - 활성 공지: ${has활성공지}`);
  console.log(`  - 예정 세무신고: ${has예정세무신고}`);

  // Try to get text content from specific areas
  const dashContent = await page.locator('.admin-content').textContent();
  if (dashContent) {
    console.log(`\nDashboard content length: ${dashContent.length}`);
    // Check if there are numbers
    const numbers = dashContent.match(/\d+/g) || [];
    console.log(`Numbers found: ${numbers.slice(0, 10).join(', ')}`);
  }
});
