#!/usr/bin/env npx tsx
/**
 * Test chat API sports queries to verify SportsDB fallback works
 */
require('dotenv').config({ path: '.env.local' });
require('dotenv').config({ path: '.env' });

// Enable debug logging
process.env.DEBUG_SPORTS_CHAT = 'true';

// Import the function directly
import { getSportsDataForQuery } from '../lib/chat';

async function testQuery(query: string, description: string) {
  console.log(`\n${'='.repeat(60)}`);
  console.log(`TEST: ${description}`);
  console.log(`Query: "${query}"`);
  console.log('='.repeat(60));

  try {
    const result = await getSportsDataForQuery(query);
    if (result) {
      console.log(`\n✅ SUCCESS - Context length: ${result.context.length}`);
      console.log('\nContext preview (first 500 chars):');
      console.log(result.context.substring(0, 500));
      if (result.context.length > 500) {
        console.log('...[truncated]');
      }
    } else {
      console.log('\n❌ No result returned');
    }
  } catch (error) {
    console.log(`\n❌ ERROR: ${error}`);
  }
}

async function main() {
  console.log('Testing Chat Sports Queries with SportsDB Fallback\n');
  console.log(`Environment: DEBUG_SPORTS_CHAT=${process.env.DEBUG_SPORTS_CHAT}`);

  // Test NHL query (should hit BallDontLie 401 and fallback to SportsDB)
  await testQuery('What NHL games are on today?', 'NHL Games Query');

  // Test MLB query (off-season, should show off-season message)
  await testQuery('What MLB games are scheduled?', 'MLB Games Query');

  // Test NBA query (should work with BallDontLie)
  await testQuery('What are the NBA standings?', 'NBA Standings Query');

  // Test NFL query
  await testQuery('NFL games this week', 'NFL Games Query');

  console.log('\n' + '='.repeat(60));
  console.log('Tests complete');
}

main().catch(console.error);
