/**
 * Check most recent records in OptionsFlow_monthly
 */
import { rawQuery } from '../src/db.js';
import dotenv from 'dotenv';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

dotenv.config({ path: join(__dirname, '..', '.env') });

async function checkRecentRecords() {
  console.log('🔍 Checking most recent records...\n');

  try {
    // Get most recent records
    const recent = await rawQuery(`
      SELECT "CreatedDate", "CreatedTime", "Symbol", "CallPut", "Premium", "Strike"
      FROM "OptionsFlow_monthly"
      ORDER BY "CreatedDate" DESC, "CreatedTime" DESC
      LIMIT 20
    `);
    
    console.log(`📊 Most recent ${recent.length} records:\n`);
    recent.forEach((r, i) => {
      console.log(`${i + 1}. ${r.CreatedDate} ${r.CreatedTime || ''} | ${r.Symbol} ${r.CallPut} ${r.Strike || ''} | Premium: ${r.Premium}`);
    });

    // Get count by date
    const byDate = await rawQuery(`
      SELECT "CreatedDate", COUNT(*) as count
      FROM "OptionsFlow_monthly"
      GROUP BY "CreatedDate"
      ORDER BY "CreatedDate" DESC
      LIMIT 10
    `);
    
    console.log('\n📅 Records by date (most recent first):');
    byDate.forEach(r => {
      console.log(`   ${r.CreatedDate}: ${r.count} records`);
    });

    // Get total count
    const total = await rawQuery('SELECT COUNT(*) as count FROM "OptionsFlow_monthly"');
    console.log(`\n📊 Total records: ${total[0].count}`);

  } catch (error) {
    console.error('❌ Error:', error.message);
    process.exit(1);
  }
}

checkRecentRecords();

