export default defineContentScript({
  matches: ['<all_urls>'],
  main() {
    // Content script is now properly configured for all URLs
    // The actual functionality is handled by dynamically injected scripts
    // This ensures the content script context is available for communication
    console.log('Chrome MCP Extension content script loaded');

    // Make user ID available globally on any page
    setupUserIdAccess();
  },
});

async function setupUserIdAccess() {
  try {
    // Get user ID from background script
    const response = await chrome.runtime.sendMessage({ type: 'getCurrentUserId' });

    if (response && response.success && response.userId) {
      // Make user ID available globally on the page
      (window as any).chromeExtensionUserId = response.userId;

      // Also store in a custom event for pages that need it
      window.dispatchEvent(
        new CustomEvent('chromeExtensionUserIdReady', {
          detail: { userId: response.userId },
        }),
      );

      // Store in sessionStorage for easy access
      try {
        sessionStorage.setItem('chromeExtensionUserId', response.userId);
      } catch (e) {
        // Ignore storage errors (some sites block this)
      }

      console.log('Chrome Extension User ID available:', response.userId);
    } else {
      console.log('Chrome Extension: No user ID available (not connected to server)');
    }
  } catch (error) {
    console.error('Chrome Extension: Failed to get user ID:', error);
  }
}
