// Minimal test preload
const { contextBridge, ipcRenderer } = require('electron');
const fs = require('fs');
const path = require('path');
const os = require('os');

const logFile = path.join(os.homedir(), 'Library', 'Logs', 'Instructure Agent', 'preload-test.log');

function log(message) {
  const timestamp = new Date().toISOString();
  const logMessage = `${timestamp} - ${message}\n`;
  console.log(message); // Also try console
  try {
    fs.appendFileSync(logFile, logMessage);
  } catch (e) {
    // Ignore file write errors
  }
}

log('TEST PRELOAD: Script is executing');
log(`TEST PRELOAD: contextBridge available: ${!!contextBridge}`);

try {
  contextBridge.exposeInMainWorld('testAPI', {
    test: () => 'Hello from preload!'
  });
  log('TEST PRELOAD: API exposed successfully');

  // Also expose a marker
  contextBridge.exposeInMainWorld('__TEST_PRELOAD_LOADED__', true);
  log('TEST PRELOAD: Marker exposed');
} catch (error) {
  log(`TEST PRELOAD: Error exposing API - ${error.message}`);
  log(`TEST PRELOAD: Error stack - ${error.stack}`);
}
