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

const username = process.env.E2E_ADMIN_USERNAME ?? 'admin';
const password = process.env.E2E_ADMIN_PASSWORD;
const baseUrl = (process.env.E2E_BASE_URL ?? 'http://178.104.200.7/taxbaik').replace(/\/$/, '');

test.describe('admin smoke', () => {
  test('navigates the main admin menus without circuit errors', async ({ page }) => {
    test.skip(!password, 'E2E_ADMIN_PASSWORD is required.');

    const consoleErrors: string[] = [];
    page.on('console', message => {
      if (message.type() === 'error') {
        consoleErrors.push(message.text());
      }
    });
    page.on('pageerror', error => {
      consoleErrors.push(error.message);
    });

    await loginThroughAdminUi(page, baseUrl, username, password);

    const menuChecks = [
      { path: '/admin/dashboard', content: /이번달 문의/ },
      { path: '/admin/blog', content: /전체 포스트/ },
      { path: '/admin/inquiries', content: /문의 관리/ },
      { path: '/admin/settings', content: /계정 관리/ },
    ];

    for (const check of menuChecks) {
      await navigateInBlazor(page, `${baseUrl}${check.path}`);
      await expect(page).toHaveURL(new RegExp(`${check.path}$`));
      await expect(page.locator('.mud-main-content').getByText(check.content).first()).toBeVisible({ timeout: 20_000 });
    }

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