import { expect, test } from '@playwright/test';
import { getAdminToken, installAdminToken } from './helpers/admin-auth';
import { captureEvidence } from './helpers/evidence';

const username = process.env.E2E_ADMIN_USERNAME ?? 'test_admin';
const password = process.env.E2E_ADMIN_PASSWORD ?? 'TestAdmin@123456';
const baseUrl = (process.env.E2E_BASE_URL ?? 'https://www.taxbaik.com').replace(/\/$/, '');

test.describe('admin smoke', () => {
  test('@smoke logs in and lands on dashboard without circuit errors', async ({ page }) => {
    test.skip(!password, 'E2E_ADMIN_PASSWORD is required.');

    const consoleErrors: string[] = [];
    page.on('console', message => {
      if (message.type() === 'error') {
        const text = message.text();
        if (
          text.includes('Failed to load resource: the server responded with a status of 404') ||
          text.includes('Failed to load resource: the server responded with a status of 401') ||
          text.includes('Blocked: pdb') ||
          text.includes('mono_download_assets') ||
          text.includes('.pdb') ||
          text.includes('Fetch API cannot load') ||
          text.includes('Failed to fetch') ||
          text.includes('instantiate_wasm_module') ||
          text.includes('resource-collection') ||
          text.includes("The value 'get' is not a function") ||
          text.includes('download \'http://localhost:5001/admin/_framework/') ||
          text.includes('failed 404 Not Found')
        ) {
          return;
        }

        consoleErrors.push(text);
      }
    });
    page.on('pageerror', error => {
      const text = error.message;
      if (
        text.includes('Blocked: pdb') ||
        text.includes('mono_download_assets') ||
        text.includes('.pdb') ||
        text.includes('Failed to fetch') ||
        text.includes('status of 401') ||
        text.includes('resource-collection') ||
        text.includes("The value 'get' is not a function") ||
        text.includes('download \'http://localhost:5001/admin/_framework/') ||
        text.includes('failed 404 Not Found')
      ) {
        return;
      }

      consoleErrors.push(text);
    });

    const token = await getAdminToken(page.context().request, baseUrl, username, password);
    await installAdminToken(page, token);
    await page.goto(`${baseUrl}/admin/dashboard`);
    await expect(page).toHaveURL(/\/admin\/dashboard$/, { timeout: 20_000 });

    await page.waitForLoadState('networkidle', { timeout: 30_000 });

    await expect(page.locator('body')).toContainText('세무 운영 콘솔', { timeout: 60_000 });
    await expect(page.getByRole('banner').getByRole('link', { name: /로그아웃/ })).toBeVisible({ timeout: 30_000 });
    await expect(page.locator('.admin-shell')).toBeVisible({ timeout: 30_000 });
    await expect(page.locator('.admin-drawer')).toBeVisible({ timeout: 30_000 });
    await expect(page.locator('.admin-content')).toContainText(/대시보드|세무 운영 콘솔/, { timeout: 30_000 });
    await captureEvidence(page, test.info(), 'admin-dashboard-smoke');

    expect(consoleErrors, 'browser console/page errors').toEqual([]);
  });
});
