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

// All pages to test
const pages = [
  { path: '/', name: 'Home' },
  { path: '/about', name: 'About' },
  { path: '/services', name: 'Services' },
  { path: '/portfolio', name: 'Portfolio' },
  { path: '/blog', name: 'Blog' },
  { path: '/contact', name: 'Contact' },
  { path: '/privacy', name: 'Privacy Policy' },
  { path: '/terms', name: 'Terms of Service' },
  { path: '/sitemap', name: 'Sitemap' },
];

test.describe('Page Load Tests', () => {
  for (const page of pages) {
    test(`${page.name} page loads successfully`, async ({ page: browserPage }) => {
      const response = await browserPage.goto(page.path);

      // Check response status
      expect(response?.status()).toBeLessThan(400);

      // Verify page has content
      await expect(browserPage.locator('body')).not.toBeEmpty();

      // Check no console errors
      const errors: string[] = [];
      browserPage.on('console', msg => {
        if (msg.type() === 'error') {
          errors.push(msg.text());
        }
      });

      // Wait for page to fully load
      await browserPage.waitForLoadState('networkidle');
    });
  }
});

test.describe('Console Error Checks', () => {
  for (const page of pages) {
    test(`${page.name} page has no console errors`, async ({ page: browserPage }) => {
      const errors: string[] = [];

      browserPage.on('console', msg => {
        if (msg.type() === 'error') {
          // Ignore common non-critical errors
          const text = msg.text();
          if (!text.includes('favicon') && !text.includes('404')) {
            errors.push(text);
          }
        }
      });

      await browserPage.goto(page.path);
      await browserPage.waitForLoadState('networkidle');

      expect(errors).toHaveLength(0);
    });
  }
});
