/**
 * Simple Epic 3 connectivity test
 * Tests basic module loading and ScenePresetManager availability
 */

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

const CHROME_DEVTOOLS_URL = 'ws://localhost:9222/devtools/browser/1aeaf428-412f-4e20-9f2d-c13533d031ae';
const FOUNDRY_URL = 'https://localhost:31000/game';

test('Connect to Chrome DevTools and verify Foundry page', async () => {
  const browser = await chromium.connect({
    wsEndpoint: CHROME_DEVTOOLS_URL
  });
  
  const pages = await browser.pages();
  console.log('Available pages:', pages.length);
  
  const foundryPage = pages.find(p => p.url().includes('localhost:31000/game'));
  expect(foundryPage).toBeDefined();
  
  await foundryPage.goto(FOUNDRY_URL);
  
  // Wait for Foundry to load
  await foundryPage.waitForFunction(() => window.game?.ready, { timeout: 30000 });
  
  // Verify module is active
  const isActive = await foundryPage.evaluate(() => {
    const module = game.modules.get('video-view-manager');
    return module?.active;
  });
  
  expect(isActive).toBe(true);
  console.log('✓ Module is active');
  
  // Verify ScenePresetManager exists
  const hasPresetManager = await foundryPage.evaluate(() => {
    const module = game.modules.get('video-view-manager');
    return module?.scenePresetManager !== undefined;
  });
  
  expect(hasPresetManager).toBe(true);
  console.log('✓ ScenePresetManager exists');
  
  // Verify DirectorsBoard exists
  const hasDirectorsBoard = await foundryPage.evaluate(() => {
    const module = game.modules.get('video-view-manager');
    return module?._directorsBoard !== undefined;
  });
  
  expect(hasDirectorsBoard).toBe(true);
  console.log('✓ DirectorsBoard exists');
  
  await browser.close();
});

test('Verify ConfirmationBar exists', async () => {
  const browser = await chromium.connect({
    wsEndpoint: CHROME_DEVTOOLS_URL
  });
  
  const pages = await browser.pages();
  const foundryPage = pages.find(p => p.url().includes('localhost:31000/game')) || pages[0];
  
  await foundryPage.goto(FOUNDRY_URL);
  await foundryPage.waitForFunction(() => window.game?.ready, { timeout: 30000 });
  
  const hasConfirmationBar = await foundryPage.evaluate(() => {
    const module = game.modules.get('video-view-manager');
    return module?._confirmationBar !== undefined;
  });
  
  expect(hasConfirmationBar).toBe(true);
  console.log('✓ ConfirmationBar exists');
  
  await browser.close();
});

test('Verify PresetImportExportManager exists', async () => {
  const browser = await chromium.connect({
    wsEndpoint: CHROME_DEVTOOLS_URL
  });
  
  const pages = await browser.pages();
  const foundryPage = pages.find(p => p.url().includes('localhost:31000/game')) || pages[0];
  
  await foundryPage.goto(FOUNDRY_URL);
  await foundryPage.waitForFunction(() => window.game?.ready, { timeout: 30000 });
  
  const hasImportExportManager = await foundryPage.evaluate(() => {
    const module = game.modules.get('video-view-manager');
    return module?.presetImportExportManager !== undefined;
  });
  
  expect(hasImportExportManager).toBe(true);
  console.log('✓ PresetImportExportManager exists');
  
  await browser.close();
});
