(function() {
if (window.__syntrixNovaContentLoaded) return;
window.__syntrixNovaContentLoaded = true;

chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
  if (msg.type === 'SYNTRIX_PING') {
    sendResponse({ pong: true, url: window.location.href, title: document.title });
    return true;
  }

  if (msg.type === 'SYNTRIX_READ_PAGE') {
    try {
      if (msg.selector) {
        const el = document.querySelector(msg.selector);
        sendResponse({
          success: !!el,
          content: el ? (msg.includeHtml ? el.innerHTML : el.innerText) : null,
          error: el ? null : `Element nicht gefunden: ${msg.selector}`
        });
      } else {
        sendResponse({
          success: true,
          title: document.title,
          url: window.location.href,
          content: document.body.innerText.substring(0, 50000)
        });
      }
    } catch (e) {
      sendResponse({ success: false, error: e.message });
    }
    return true;
  }

  if (msg.type === 'SYNTRIX_READ_SELECTION') {
    const selection = window.getSelection()?.toString() || '';
    sendResponse({ selection });
    return true;
  }

  if (msg.type === 'SYNTRIX_WRITE_DOM') {
    try {
      const el = document.querySelector(msg.selector);
      if (!el) { sendResponse({ success: false, error: `Element nicht gefunden: ${msg.selector}` }); return true; }
      if (msg.mode === 'replace') el.innerHTML = msg.content;
      else if (msg.mode === 'prepend') el.insertAdjacentHTML('afterbegin', msg.content);
      else el.insertAdjacentHTML('beforeend', msg.content);
      sendResponse({ success: true });
    } catch (e) {
      sendResponse({ success: false, error: e.message });
    }
    return true;
  }

  if (msg.type === 'SYNTRIX_EXECUTE_JS') {
    try {
      const result = (0, eval)(msg.code);
      sendResponse({ success: true, result });
    } catch (e) {
      sendResponse({ success: false, error: e.message });
    }
    return true;
  }

  if (msg.type === 'SYNTRIX_FILL_FORM') {
    try {
      const el = document.querySelector(msg.selector);
      if (!el) { sendResponse({ success: false, error: `Element nicht gefunden: ${msg.selector}` }); return true; }
      const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')?.set
        || Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value')?.set;
      if (setter) {
        setter.call(el, msg.value);
        el.dispatchEvent(new Event('input', { bubbles: true }));
        el.dispatchEvent(new Event('change', { bubbles: true }));
      } else {
        el.value = msg.value;
      }
      sendResponse({ success: true });
    } catch (e) {
      sendResponse({ success: false, error: e.message });
    }
    return true;
  }

  if (msg.type === 'SYNTRIX_CLICK') {
    try {
      const el = document.querySelector(msg.selector);
      if (!el) { sendResponse({ success: false, error: `Element nicht gefunden: ${msg.selector}` }); return true; }
      el.click();
      sendResponse({ success: true });
    } catch (e) {
      sendResponse({ success: false, error: e.message });
    }
    return true;
  }
})();
