import { test, expect } from './fixtures/auth';
import { CommonActions } from './utils/common-actions';

test('Debug loaded scripts', async ({ authenticatedPage: page }) => {
  test.setTimeout(30000);
  const actions = new CommonActions(page);

  // Navigate to templates page
  await actions.navigateAndWait('/communication-templates/');
  
  // Wait for scripts
  await page.waitForFunction(() => typeof HVACTemplates !== 'undefined');

  // Check what scripts are loaded
  const scriptInfo = await page.evaluate(() => {
    const scripts = Array.from(document.querySelectorAll('script[src]'));
    const hvacScripts = scripts.filter(script => script.src.includes('hvac') || script.src.includes('communication'));
    
    return {
      totalScripts: scripts.length,
      hvacScripts: hvacScripts.map(script => script.src),
      hasHVACTemplates: typeof HVACTemplates !== 'undefined',
      createNewTemplateFunction: HVACTemplates ? HVACTemplates.createNewTemplate.toString() : 'not found'
    };
  });

  console.log('Script analysis:', JSON.stringify(scriptInfo, null, 2));

  // Check if external JS file is also loading and overriding
  const jsFile = await page.evaluate(() => {
    const scripts = Array.from(document.querySelectorAll('script[src]'));
    return scripts.find(script => script.src.includes('communication-templates.js'))?.src;
  });

  console.log('External JS file found:', jsFile);

  await actions.screenshot('scripts-debug');
});