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

const baseUrl = process.env.E2E_BASE_URL || 'https://www.taxbaik.com';
const username = process.env.E2E_ADMIN_USERNAME || 'test_admin';
const password = process.env.E2E_ADMIN_PASSWORD || 'TestAdmin@123456';

test.describe('Admin Pages E2E (Manual)', () => {
  test('Login page loads', async ({ page }) => {
    await page.goto(`${baseUrl}/admin/login`);
    await expect(page.locator('text=관리자 로그인')).toBeVisible();
    await expect(page.locator('#Username')).toBeVisible();
  });

  test('Dashboard page loads', async ({ page }) => {
    await page.goto(`${baseUrl}/admin/login`);
    await page.fill('#Username', username);
    await page.fill('#Password', password);
    await page.click('button[type="submit"]');
    await navigateInBlazor(page, `${baseUrl}/admin/dashboard`);

    // Check if dashboard loaded
    const content = await page.content();
    expect(content.includes('대시보드') || content.includes('세무')).toBeTruthy();

    // Screenshot
    await captureEvidence(page, test.info(), 'dashboard-manual');
  });

  test('Blog page loads', async ({ page }) => {
    await page.goto(`${baseUrl}/admin/login`);
    await page.fill('#Username', username);
    await page.fill('#Password', password);
    await page.click('button[type="submit"]');
    await navigateInBlazor(page, `${baseUrl}/admin/dashboard`);
    await navigateInBlazor(page, `${baseUrl}/admin/blog`);

    const content = await page.content();
    const loaded = content.includes('블로그') || page.url().includes('/admin/blog');
    console.log(`Blog page loaded: ${loaded}, URL: ${page.url()}`);

    await captureEvidence(page, test.info(), 'blog-manual');
  });

  test('Inquiry page loads', async ({ page }) => {
    await page.goto(`${baseUrl}/admin/login`);
    await page.fill('#Username', username);
    await page.fill('#Password', password);
    await page.click('button[type="submit"]');
    await navigateInBlazor(page, `${baseUrl}/admin/dashboard`);
    await navigateInBlazor(page, `${baseUrl}/admin/inquiries`);

    const content = await page.content();
    const loaded = content.includes('문의') || page.url().includes('/admin/inquiries');
    console.log(`Inquiry page loaded: ${loaded}, URL: ${page.url()}`);

    await captureEvidence(page, test.info(), 'inquiries-manual');
  });

  test('CRM pages load', async ({ page }) => {
    await page.goto(`${baseUrl}/admin/login`);
    await page.fill('#Username', username);
    await page.fill('#Password', password);
    await page.click('button[type="submit"]');
    await navigateInBlazor(page, `${baseUrl}/admin/dashboard`);
    const pages = [
      '/admin/tax-profiles',
      '/admin/contracts',
      '/admin/consulting-activities'
    ];

    for (const adminPage of pages) {
      await navigateInBlazor(page, `${baseUrl}${adminPage}`);

      const url = page.url();
      console.log(`✅ Page loaded: ${adminPage} (URL: ${url})`);

      await captureEvidence(page, test.info(), adminPage.replace(/\//g, '_'));
    }
  });
});
