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();
  
  // 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);
  
  // Go to settings and click Personalization
  await page.goto('https://openweb.kangaroo-eel.ts.net/');
  await page.waitForTimeout(2000);
  await page.click('button:has-text("User menu")');
  await page.waitForTimeout(1000);
  await page.click('text=Settings');
  await page.waitForTimeout(3000);
  await page.click('text=Personalization');
  await page.waitForTimeout(3000);
  
  // Click the Manage button
  await page.click('button:has-text("Manage")');
  await page.waitForTimeout(3000);
  
  console.log('📍 URL:', page.url());
  
  // Get all the memory content
  const text = await page.evaluate(() => document.body.innerText);
  console.log('\n📝 All content:');
  console.log(text);
  
  // Look for memory items
  const memoryItems = await page.$$eval('[class*="memory"], .memory-item, [data-testid*="memory"], .card, .item', 
    items => items.map(i => ({
      text: i.textContent.trim(),
      html: i.innerHTML.substring(0, 500)
    }))
  );
  
  console.log('\n🧠 Memory items found:', memoryItems.length);
  memoryItems.forEach((item, i) => {
    console.log(`\n--- Memory ${i + 1} ---`);
    console.log(item.text);
  });
  
  // Also try to get raw API response
  const response = await page.evaluate(async () => {
    try {
      const res = await fetch('/api/v1/memories');
      return await res.json();
    } catch (e) {
      return { error: e.message };
    }
  });
  
  console.log('\n📡 API Response:');
  console.log(JSON.stringify(response, null, 2));
  
  await page.screenshot({ path: '/tmp/memories.png', fullPage: true });
  
  await browser.close();
})();
