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

test('production: verify all admin pages load correctly', async ({ page }) => {
  const baseUrl = 'http://178.104.200.7/taxbaik';

  // Login
  console.log('🔐 Logging in...');
  await page.goto(`${baseUrl}/admin/login`);
  await page.fill('input[placeholder="사용자명"]', 'test_admin');
  await page.fill('input[placeholder="비밀번호"]', 'TestAdmin@123456');
  await page.click('button[type="submit"]');
  await page.waitForURL(/admin\/dashboard/);
  console.log('✓ Login successful\n');

  const pageHero = page.locator('.admin-page-hero').first();
  const loadingOverlay = page.locator('#blazor-loading');

  // List of all admin pages to test (using direct URLs)
  const pages = [
    { name: '📊 Dashboard', url: `${baseUrl}/admin/dashboard`, hasData: false },
    { name: '👥 Clients', url: `${baseUrl}/admin/clients`, hasData: true },
    { name: '📅 Tax Filings', url: `${baseUrl}/admin/tax-filings`, hasData: true },
    { name: '📢 Announcements', url: `${baseUrl}/admin/announcements`, hasData: false },
    { name: '❓ FAQs', url: `${baseUrl}/admin/faqs`, hasData: true },
    { name: '📝 Blog', url: `${baseUrl}/admin/blog`, hasData: true },
    { name: '🎭 Season Simulator', url: `${baseUrl}/admin/season-simulator`, hasData: false },
    { name: '❔ Inquiries', url: `${baseUrl}/admin/inquiries`, hasData: true },
    { name: '⚙️ Settings', url: `${baseUrl}/admin/settings`, hasData: false },
  ];

  for (const pageInfo of pages) {
    console.log(`${'─'.repeat(60)}`);
    console.log(`Testing: ${pageInfo.name}`);
    console.log(`URL: ${pageInfo.url}`);
    console.log(`${'─'.repeat(60)}`);

    const startTime = Date.now();

    try {
      // Navigate to page
      await page.goto(pageInfo.url);

      // Wait for page hero or basic element
      try {
        await pageHero.waitFor({ state: 'visible', timeout: 3000 });
        console.log(`  ✓ Page hero visible`);
      } catch {
        // Some pages might not have page hero, that's OK
      }

      // Check if page loaded successfully by looking for content
      const pageContent = page.locator('body').first();
      await pageContent.waitFor({ state: 'visible', timeout: 5000 });

      // Wait for data if expected
      if (pageInfo.hasData) {
        try {
          // Try to find table rows
          await page.waitForSelector('tbody tr', { timeout: 8000 });
          const rowCount = await page.locator('tbody tr').count();
          if (rowCount > 0) {
            console.log(`  ✓ Data loaded: ${rowCount} rows`);
          } else {
            console.log(`  ⚠️  Table found but no rows`);
          }
        } catch {
          console.log(`  ℹ️  No table data (may not have table)`);
        }
      }

      // Verify overlay is hidden
      const overlayShown = await loadingOverlay.evaluate((el: HTMLElement) =>
        el.classList.contains('show')
      ).catch(() => false);

      if (!overlayShown) {
        console.log(`  ✓ Loading overlay hidden`);
      } else {
        console.log(`  ⚠️  Loading overlay still visible`);
      }

      const totalTime = Date.now() - startTime;
      console.log(`  ⏱️  Load time: ${totalTime}ms`);
      console.log(`  ✅ PAGE LOADED SUCCESSFULLY\n`);
    } catch (error) {
      const totalTime = Date.now() - startTime;
      console.log(`  ❌ FAILED: ${error}`);
      console.log(`  ⏱️  Time: ${totalTime}ms\n`);
      throw error;
    }
  }

  console.log(`${'═'.repeat(60)}`);
  console.log('✅ ALL PAGES VERIFIED SUCCESSFULLY');
  console.log(`${'═'.repeat(60)}`);
});
