/**
 * Debug: check if value dropdown is populated in filter builder
 */
import { chromium } from 'file:///C:/Users/Zjz/AppData/Roaming/npm/node_modules/@playwright/mcp/node_modules/playwright/index.mjs';

const BASE = 'http://127.0.0.1:8000';

async function main() {
  const browser = await chromium.launch({ headless: true });
  const page = await browser.newPage({ viewport: { width: 1280, height: 900 } });

  page.on('console', msg => {
    if (msg.type() === 'error') console.log('  🐛', msg.text().substring(0, 150));
  });
  page.on('pageerror', err => console.log('  ❌', err.message));

  // 1. Go to simple analysis page
  await page.goto(`${BASE}/simple/`, { waitUntil: 'networkidle' });
  console.log('✅ Page loaded');

  // 2. Upload test CSV
  const fileInput = page.locator('#saFileInput');
  await fileInput.setInputFiles('C:\\Users\\Zjz\\Desktop\\tianxuan\\天璇\\data\\test_small.csv');
  await page.waitForResponse(r => r.url().includes('/simple/upload/'), { timeout: 15000 });
  await new Promise(r => setTimeout(r, 2000));
  console.log('✅ Upload done');

  // 3. Check preset panel - does it have a condition row?
  const presetConds = await page.locator('#saPresetConditions .sa-filter-row').count();
  console.log(`Preset conditions: ${presetConds}`);

  if (presetConds > 0) {
    // Select a column in the preset
    const presetCol = page.locator('#saPresetConditions .sa-filter-col-select').first();
    const opts = await presetCol.locator('option').all();
    console.log(`Preset column options: ${opts.length}`);
    if (opts.length > 1) {
      const val = await opts[1].getAttribute('value');
      await presetCol.selectOption(val);
      console.log(`Selected column: ${val}`);
      await new Promise(r => setTimeout(r, 3000));

      // Check value dropdown in preset
      const presetVal = page.locator('#saPresetConditions .sa-filter-val');
      const valOpts = await presetVal.locator('option').all();
      console.log(`Preset value options: ${valOpts.length}`);
      for (const opt of valOpts) {
        const v = await opt.getAttribute('value');
        if (v) console.log(`  value: "${v}"`);
      }
    }
  }

  // 4. Go to Step 2
  await page.evaluate(() => { if (typeof goStep === 'function') goStep(2); });
  await new Promise(r => setTimeout(r, 1500));

  // Check step 2 has condition row
  const step2Conds = await page.locator('#saFilterConditions .sa-filter-row').count();
  console.log(`Step 2 conditions: ${step2Conds}`);

  if (step2Conds > 0) {
    const step2Col = page.locator('#saFilterConditions .sa-filter-col-select').first();
    const opts2 = await step2Col.locator('option').all();
    console.log(`Step 2 column options: ${opts2.length}`);
    if (opts2.length > 1) {
      const val2 = await opts2[1].getAttribute('value');
      await step2Col.selectOption(val2);
      console.log(`Selected column: ${val2}`);
      await new Promise(r => setTimeout(r, 3000));

      // Check value dropdown
      const step2Val = page.locator('#saFilterConditions .sa-filter-val');
      const valOpts2 = await step2Val.locator('option').all();
      console.log(`Step 2 value options: ${valOpts2.length}`);
      for (const opt of valOpts2) {
        const v = await opt.getAttribute('value');
        if (v) console.log(`  value: "${v}"`);
      }
    }
  }

  await browser.close();
}

main().catch(e => { console.error(e); process.exit(1); });
