/**
 * Monitor Chrome extension connections to the remote server
 */

import WebSocket from 'ws';

const CHROME_ENDPOINT = 'ws://localhost:3001/chrome';

function monitorConnections() {
  console.log('🔍 Monitoring Chrome extension connections...');
  console.log('📍 Endpoint:', CHROME_ENDPOINT);
  console.log('');
  console.log('Instructions:');
  console.log('1. Load the Chrome extension from: app/chrome-extension/.output/chrome-mv3');
  console.log('2. Open the extension popup to check connection status');
  console.log('3. Watch this monitor for connection events');
  console.log('');

  const ws = new WebSocket(CHROME_ENDPOINT);

  ws.on('open', () => {
    console.log('✅ Connected to Chrome extension endpoint');
    console.log('⏳ Waiting for Chrome extension to connect...');
  });

  ws.on('message', (data) => {
    try {
      const message = JSON.parse(data.toString());
      console.log('📨 Received message from Chrome extension:', JSON.stringify(message, null, 2));
    } catch (error) {
      console.log('📨 Received raw message:', data.toString());
    }
  });

  ws.on('error', (error) => {
    console.error('❌ WebSocket error:', error);
  });

  ws.on('close', () => {
    console.log('🔌 Connection closed');
  });

  // Keep the connection alive
  setInterval(() => {
    if (ws.readyState === WebSocket.OPEN) {
      console.log('💓 Connection still alive, waiting for Chrome extension...');
    }
  }, 10000);
}

monitorConnections();
