#!/usr/bin/env tsx
import { createClient } from '@libsql/client';
import { config } from 'dotenv';

config();

const db = createClient({
  url: process.env.TURSO_URL!,
  authToken: process.env.TURSO_AUTH_TOKEN!,
});

async function investigate() {
  console.log('\n🔍 Investigating failed tests\n');

  // Check 1: Composite memories indexes
  console.log('1. Composite memories indexes:');
  console.log('   Expected: idx_memories_user_type, idx_memories_user_importance, idx_memories_user_archived');
  const memIdxs = await db.execute(`
    SELECT name FROM sqlite_master
    WHERE type='index' AND tbl_name='memories'
    ORDER BY name
  `);
  console.log('   Found:');
  memIdxs.rows.forEach(row => console.log(`     - ${(row as any).name}`));

  // Check 2: FTS tables
  console.log('\n2. FTS tables:');
  console.log('   Expected: memories_fts, entities_fts');
  const ftsTables = await db.execute(`
    SELECT name FROM sqlite_master
    WHERE type='table' AND (name='memories_fts' OR name='entities_fts')
  `);
  console.log('   Found:');
  ftsTables.rows.forEach(row => console.log(`     - ${(row as any).name}`));

  // Check 3: FTS triggers
  console.log('\n3. FTS triggers:');
  console.log('   Expected: 6 triggers (3 for memories_fts, 3 for entities_fts)');
  const triggers = await db.execute(`
    SELECT name, tbl_name FROM sqlite_master
    WHERE type='trigger'
    ORDER BY name
  `);
  console.log('   Found:');
  triggers.rows.forEach(row => console.log(`     - ${(row as any).name} (${(row as any).tbl_name})`));

  console.log('\n');
}

investigate().catch(console.error);
