const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch({ headless: true });
  const context = await browser.newContext({
    userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36'
  });
  const page = await context.newPage();
  
  console.log('🦀 Logging into OpenWebUI...');
  
  // Login
  await page.goto('https://openweb.kangaroo-eel.ts.net/auth');
  await page.waitForTimeout(2000);
  await page.fill('input[type="email"]', 'anthonymau@gmail.com');
  await page.fill('input[type="password"]', 'RecOvery2026!');
  await page.click('button[type="submit"]');
  await page.waitForTimeout(5000);
  
  console.log('✅ Logged in');
  
  // Try to access the API directly for memories
  const apiEndpoints = [
    '/api/v1/memories',
    '/api/v1/users/me/memories',
    '/api/memories',
    '/api/v1/memory'
  ];
  
  for (const endpoint of apiEndpoints) {
    const response = await page.goto(`https://openweb.kangaroo-eel.ts.net${endpoint}`);
    console.log(`\n📡 ${endpoint}:`, response.status());
    const text = await page.evaluate(() => document.body.innerText);
    console.log('Response:', text.substring(0, 500));
  }
  
  // Check if memories are stored in localStorage
  await page.goto('https://openweb.kangaroo-eel.ts.net/');
  await page.waitForTimeout(3000);
  
  const localStorage = await page.evaluate(() => {
    const items = {};
    for (let i = 0; i < localStorage.length; i++) {
      const key = localStorage.key(i);
      items[key] = localStorage.getItem(key);
    }
    return items;
  });
  
  console.log('\n💾 LocalStorage keys:', Object.keys(localStorage));
  
  // Look for memory-related keys
  const memoryKeys = Object.keys(localStorage).filter(k => 
    k.toLowerCase().includes('memory')
  );
  
  console.log('\n🧠 Memory keys found:', memoryKeys);
  
  if (memoryKeys.length > 0) {
    memoryKeys.forEach(k => {
      console.log(`\n${k}:`);
      console.log(localStorage[k].substring(0, 1000));
    });
  }
  
  await browser.close();
})();
