import { test, expect } from '@playwright/test';
import { STAGING_URL } from './config/staging-config';

/**
 * Basic test to verify the site is accessible with the new domain
 */
test.describe('Domain Verification - Basic', () => {
  test('Site loads and contains HVAC content', async ({ page }) => {
    console.log('Step 1: Verifying site loads with new domain');
    
    // Navigate to the main site
    await page.goto(STAGING_URL);
    await page.waitForLoadState('networkidle');
    
    // Take screenshot of homepage
    await page.screenshot({ path: 'test-results/domain-verification/homepage.png' });
    
    // Verify the page loads
    const title = await page.title();
    console.log(`Homepage title: ${title}`);
    expect(title).toBeTruthy();
    
    // Check if the page contains HVAC-related content
    const pageContent = await page.textContent('body');
    console.log(`Page contains "HVAC": ${pageContent?.includes('HVAC')}`);
    console.log(`Page contains "training": ${pageContent?.includes('training')}`);
    console.log(`Page contains "events": ${pageContent?.includes('events')}`);
    
    // Check navigation links
    console.log('Step 2: Checking key navigation elements');
    
    // Look for login link
    const loginLinks = [
      'a:has-text("Login")',
      'a:has-text("Log In")',
      'a[href*="login"]',
      'a[href*="wp-admin"]'
    ];
    
    for (const linkSelector of loginLinks) {
      const link = page.locator(linkSelector);
      const count = await link.count();
      if (count > 0) {
        console.log(`Found login link using selector: ${linkSelector}`);
        break;
      }
    }
    
    // Check if WordPress admin is accessible
    console.log('Step 3: Checking WordPress admin accessibility');
    await page.goto(`${STAGING_URL}/wp-admin/`);
    await page.waitForLoadState('networkidle');
    
    // Take screenshot of admin redirect
    await page.screenshot({ path: 'test-results/domain-verification/admin-redirect.png' });
    
    // Should redirect to login page or show login form
    const currentUrl = page.url();
    console.log(`Admin URL: ${currentUrl}`);
    const isLoginPage = currentUrl.includes('wp-login.php') || currentUrl.includes('login');
    console.log(`Redirects to login: ${isLoginPage}`);
    
    // We expect to be redirected to login which indicates the admin panel exists
    expect(isLoginPage).toBeTruthy();
    
    console.log('Basic domain verification completed successfully');
  });
  
  test('Check for HVAC plugin presence', async ({ page }) => {
    console.log('Step 1: Checking for HVAC plugin indicators');
    
    // Go to homepage first
    await page.goto(STAGING_URL);
    await page.waitForLoadState('networkidle');
    
    // Check for plugin-specific content in the page source
    const htmlContent = await page.content();
    
    // Look for HVAC plugin indicators
    const hvacIndicators = [
      'hvac-community-events',
      'hvac-dashboard',
      'hvac-registration',
      'trainer-profile',
      'community-login'
    ];
    
    let foundIndicators = 0;
    for (const indicator of hvacIndicators) {
      if (htmlContent.includes(indicator)) {
        console.log(`Found HVAC indicator: ${indicator}`);
        foundIndicators++;
      }
    }
    
    console.log(`Found ${foundIndicators} HVAC plugin indicators`);
    
    // Check for WordPress plugin assets that might be loading
    const cssLinks = await page.$$eval('link[rel="stylesheet"]', links => 
      links.map(link => link.href).filter(href => href.includes('hvac'))
    );
    
    const jsLinks = await page.$$eval('script[src]', scripts => 
      scripts.map(script => script.src).filter(src => src.includes('hvac'))
    );
    
    console.log(`HVAC CSS files: ${cssLinks.length}`);
    console.log(`HVAC JS files: ${jsLinks.length}`);
    
    if (cssLinks.length > 0) {
      console.log('HVAC CSS files found:', cssLinks);
    }
    
    if (jsLinks.length > 0) {
      console.log('HVAC JS files found:', jsLinks);
    }
    
    // Take screenshot for reference
    await page.screenshot({ path: 'test-results/domain-verification/plugin-check.png' });
    
    console.log('Plugin presence check completed');
  });
});