// Zeq Network Interception - Page Context Script
// This file is injected into the page context (not isolated content script context)
// to intercept fetch and XHR requests

(function() {
  'use strict';

  console.log('🚀 Zeq [Page Context]: Network interception script executing...');

  // Debug: Listen for ALL ZEQ messages to verify communication is working
  window.addEventListener('message', (event) => {
    if (event.data && event.data.type && event.data.type.startsWith('ZEQ_')) {
      console.log('📨 Zeq [Page Context]: Heard message type:', event.data.type, 'requestId:', event.data.requestId);
    }
  });

  // Intercept fetch in page context
  const originalFetch = window.fetch;
  window.fetch = function(...args) {
    const url = args[0];
    const options = args[1] || {};

    // DO NOT intercept WebAssembly (.wasm) files or binary data - let them pass through immediately
    if (typeof url === 'string') {
      // Skip .wasm files
      if (url.endsWith('.wasm') || url.includes('.wasm?') || url.includes('.wasm&')) {
        return originalFetch.apply(this, args);
      }
      // Skip if Content-Type indicates WebAssembly
      if (options.headers && (options.headers['Content-Type']?.includes('application/wasm') || options.headers['content-type']?.includes('application/wasm'))) {
        return originalFetch.apply(this, args);
      }
      // Skip binary data
      if (options.body instanceof ArrayBuffer || options.body instanceof Blob) {
        return originalFetch.apply(this, args);
      }
    }

    // Intercept ALL fetch requests to catch DeepSeek's API calls
    if (typeof url === 'string' && options.body) {
      // Log all fetch requests for debugging
      if (url.includes('deepseek.com') || url.includes('/api/') || url.includes('/v1/')) {
        console.log('🔍 Zeq [Page Context]: Fetch detected:', url.substring(0, 100));
      }

      // Skip non-chat API endpoints that shouldn't be processed
      if (
        url.includes('/create_pow_challenge') ||
        url.includes('/pow') ||
        url.includes('/auth') ||
        url.includes('/login') ||
        url.includes('/token') ||
        url.includes('/refresh') ||
        url.includes('/health') ||
        url.includes('/config')
      ) {
        // Let proof-of-work and auth requests pass through unchanged
        return originalFetch.apply(this, args);
      }

      // Check if this is a chat/message API request (only completion endpoints)
      if (
        (url.includes('/chat/completion') && !url.includes('pow')) ||
        (url.includes('/v1/chat/completions') && !url.includes('pow')) ||
        (url.includes('/api/v0/chat/completion') && !url.includes('pow'))
      ) {
        console.log('🎯 Zeq [Page Context]: Intercepted chat API fetch:', url);

        try {
          const bodyData = typeof options.body === 'string' ? JSON.parse(options.body) : options.body;
          let needsProcessing = false;
          let originalContent = '';

          // EXACTLY like index.html line 16928: Find the last user message
          if (bodyData.messages && Array.isArray(bodyData.messages)) {
            const lastUserMessage = bodyData.messages.filter(m => m.role === 'user' || m.role === 'User').pop();
            if (lastUserMessage) {
              originalContent = lastUserMessage.content || lastUserMessage.text || '';
              // Only process if it's not already a JSON prompt (starts with '{')
              if (originalContent && !originalContent.trim().startsWith('{') && originalContent.length > 0) {
                needsProcessing = true;
                console.log('📝 Zeq [Page Context]: Found user message to process:', originalContent.substring(0, 50));
              } else {
                console.log('⏭️ Zeq [Page Context]: Message already processed or empty');
              }
            }
          } else if (bodyData.prompt || bodyData.query || bodyData.message) {
            originalContent = bodyData.prompt || bodyData.query || bodyData.message;
            if (originalContent && !originalContent.trim().startsWith('{') && originalContent.length > 0) {
              needsProcessing = true;
            }
          }

          if (needsProcessing) {
            console.log('🔄 Zeq [Page Context]: Message needs processing, original:', originalContent.substring(0, 50));

            // Request processing from content script
            const requestId = Date.now() + Math.random();
            window.postMessage({
              type: 'ZEQ_PROCESS_REQUEST',
              requestId: requestId,
              originalContent: originalContent,
              bodyData: bodyData
            }, '*');

            console.log('📤 Zeq [Page Context]: Sent processing request, ID:', requestId);

            // Wait for response (with timeout)
            return new Promise((resolve, reject) => {
              let responded = false;
              const timeout = setTimeout(() => {
                if (!responded) {
                  responded = true;
                  console.warn('⏱️ Zeq [Page Context]: Processing timeout (3s), using original message');
                  resolve(originalFetch.apply(this, args));
                }
              }, 3000);

              const handler = (event) => {
                // Only process messages from same window
                if (event.source !== window) return;

                if (event.data && event.data.type === 'ZEQ_PROCESSED_RESPONSE' && event.data.requestId === requestId) {
                  if (responded) return;
                  responded = true;

                  clearTimeout(timeout);
                  window.removeEventListener('message', handler);

                  console.log('✅ Zeq [Page Context]: Received processed message, length:', event.data.processedContent.length);

                  // EXACTLY like index.html line 16931: Replace last user message content with zeqMathematicalPrompt
                  if (bodyData.messages) {
                    // Find and replace the LAST user message (like index.html does)
                    for (let i = bodyData.messages.length - 1; i >= 0; i--) {
                      if (bodyData.messages[i].role === 'user' || bodyData.messages[i].role === 'User') {
                        // Replace content with mathematical prompt
                        bodyData.messages[i].content = event.data.processedContent;
                        bodyData.messages[i].text = event.data.processedContent;
                        console.log('✅ Zeq [Page Context]: Replaced user message at index', i, 'with mathematical prompt');
                        break; // Only replace the last one
                      }
                    }
                  } else {
                    if (bodyData.prompt) bodyData.prompt = event.data.processedContent;
                    if (bodyData.query) bodyData.query = event.data.processedContent;
                    if (bodyData.message) bodyData.message = event.data.processedContent;
                  }

                  options.body = typeof options.body === 'string' ? JSON.stringify(bodyData) : bodyData;
                  console.log('🚀 Zeq [Page Context]: Sending processed message to DeepSeek');
                  resolve(originalFetch.apply(this, args));
                }
              };

              window.addEventListener('message', handler);
            });
          } else {
            console.log('⏭️ Zeq [Page Context]: Message does not need processing (already processed or empty)');
          }
        } catch (e) {
          console.error('Zeq [Page Context]: Fetch error', e);
        }
      }
    }

    return originalFetch.apply(this, args);
  };

  // Intercept XHR in page context
  const originalXHROpen = XMLHttpRequest.prototype.open;
  const originalXHRSend = XMLHttpRequest.prototype.send;

  XMLHttpRequest.prototype.open = function(method, url, ...rest) {
    this._zeqUrl = url;
    this._zeqMethod = method;
    return originalXHROpen.apply(this, [method, url, ...rest]);
  };

  XMLHttpRequest.prototype.send = function(data) {
    const url = this._zeqUrl;
    const xhr = this;

    // Intercept ALL XHR requests to catch DeepSeek's API calls
    if (url && data && typeof data === 'string') {
      // Log all XHR requests for debugging
      if (url.includes('deepseek.com') || url.includes('/api/') || url.includes('/v1/')) {
        console.log('🔍 Zeq [Page Context]: XHR detected:', url.substring(0, 100));
      }

      // Skip non-chat API endpoints that shouldn't be processed
      if (
        url.includes('/create_pow_challenge') ||
        url.includes('/pow') ||
        url.includes('/auth') ||
        url.includes('/login') ||
        url.includes('/token') ||
        url.includes('/refresh') ||
        url.includes('/health') ||
        url.includes('/config')
      ) {
        // Let proof-of-work and auth requests pass through unchanged
        return originalXHRSend.apply(this, [data]);
      }

      // Check if this is a chat/message API request (only completion endpoints)
      if (
        (url.includes('/chat/completion') && !url.includes('pow')) ||
        (url.includes('/v1/chat/completions') && !url.includes('pow')) ||
        (url.includes('/api/v0/chat/completion') && !url.includes('pow'))
      ) {
        console.log('🎯 Zeq [Page Context]: Intercepted chat API XHR:', url);

        try {
          const bodyData = JSON.parse(data);
          let needsProcessing = false;
          let originalContent = '';

          // EXACTLY like index.html: Replace the LAST user message with zeqMathematicalPrompt
          if (bodyData.messages && Array.isArray(bodyData.messages)) {
            const lastUserMessage = bodyData.messages.filter(m => m.role === 'user' || m.role === 'User').pop();
            if (lastUserMessage) {
              originalContent = lastUserMessage.content || lastUserMessage.text || '';
              if (originalContent && !originalContent.trim().startsWith('{') && originalContent.length > 0) {
                needsProcessing = true;
              }
            }
          } else if (bodyData.prompt || bodyData.query || bodyData.message) {
            originalContent = bodyData.prompt || bodyData.query || bodyData.message;
            if (originalContent && !originalContent.trim().startsWith('{') && originalContent.length > 0) {
              needsProcessing = true;
            }
          }

          if (needsProcessing) {
            const requestId = Date.now() + Math.random();
            let responded = false;
            let timeoutId = null;

            window.postMessage({
              type: 'ZEQ_PROCESS_REQUEST',
              requestId: requestId,
              originalContent: originalContent,
              bodyData: bodyData
            }, '*');

            // Wait for response
            const handler = (event) => {
              if (responded) return; // Prevent double processing
              if (event.data && event.data.type === 'ZEQ_PROCESSED_RESPONSE' && event.data.requestId === requestId) {
                responded = true;
                // Clear timeout FIRST to prevent race condition
                if (timeoutId) {
                  clearTimeout(timeoutId);
                  timeoutId = null;
                }
                window.removeEventListener('message', handler);

                // EXACTLY like index.html: Replace last user message content
                if (bodyData.messages) {
                  for (let i = bodyData.messages.length - 1; i >= 0; i--) {
                    if (bodyData.messages[i].role === 'user' || bodyData.messages[i].role === 'User') {
                      bodyData.messages[i].content = event.data.processedContent;
                      bodyData.messages[i].text = event.data.processedContent;
                      break;
                    }
                  }
                } else {
                  if (bodyData.prompt) bodyData.prompt = event.data.processedContent;
                  if (bodyData.query) bodyData.query = event.data.processedContent;
                  if (bodyData.message) bodyData.message = event.data.processedContent;
                }

                const processedData = JSON.stringify(bodyData);
                console.log('✅ Zeq [Page Context]: Using processed message for XHR');
                originalXHRSend.call(xhr, processedData);
              }
            };

            window.addEventListener('message', handler);

            // Timeout fallback - store ID so we can clear it
            timeoutId = setTimeout(() => {
              if (responded) return; // Already processed, don't send again
              responded = true;
              timeoutId = null;
              window.removeEventListener('message', handler);
              console.warn('⏱️ Zeq [Page Context]: XHR processing timeout, using original');
              originalXHRSend.call(xhr, data);
            }, 3000);

            return;
          }
        } catch (e) {
          console.error('Zeq [Page Context]: XHR error', e);
        }
      }
    }

    return originalXHRSend.apply(this, [data]);
  };

  console.log('✅ Zeq [Page Context]: Network interception installed successfully!');
})();
