import { rawQuery } from '../src/db.js';
import dotenv from 'dotenv';

dotenv.config();

async function testDateParsing() {
  console.log('Testing date normalization and parsing...\n');

  try {
    const result = await rawQuery(`
      SELECT 
        to_date('12/8/2025', 'MM/DD/YYYY')::text as normalized_date,
        '12/8/2025' || ' 9:43:09 AM' as test_concatenation,
        to_timestamp(to_date('12/8/2025', 'MM/DD/YYYY')::text || ' 9:43:09 AM', 'YYYY-MM-DD HH12:MI:SS AM') as parsed_ts
      LIMIT 1;
    `);

    console.log('Result:', JSON.stringify(result[0], null, 2));
    
    // Test with actual data
    const actualData = await rawQuery(`
      SELECT 
        "CreatedDate",
        "CreatedTime",
        to_date("CreatedDate"::text, 'MM/DD/YYYY')::text as normalized_date,
        to_timestamp(to_date("CreatedDate"::text, 'MM/DD/YYYY')::text || ' ' || btrim("CreatedTime"::text), 'YYYY-MM-DD HH12:MI:SS AM') as parsed_ts
      FROM "OptionsFlow_monthly"
      WHERE "CreatedDate" = '12/8/2025'
        AND "CreatedTime" = '9:43:09 AM'
      LIMIT 1;
    `);

    console.log('\nActual data test:');
    console.log(JSON.stringify(actualData[0], null, 2));

  } catch (error) {
    console.error('Error:', error.message);
    console.error(error);
  }
}

testDateParsing()
  .then(() => process.exit(0))
  .catch((e) => { console.error(e); process.exit(1); });

