#!/usr/bin/env npx tsx
/**
 * Test the actual chat API endpoint with sports queries
 */
require('dotenv').config({ path: '.env.local' });
require('dotenv').config({ path: '.env' });

const API_BASE = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000';

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

  try {
    const response = await fetch(`${API_BASE}/api/chat`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        message,
        conversationHistory: [],
      }),
    });

    if (!response.ok) {
      console.log(`\n❌ HTTP Error: ${response.status} ${response.statusText}`);
      const text = await response.text();
      console.log(`Response: ${text.substring(0, 500)}`);
      return;
    }

    const data = await response.json();
    if (data.reply) {
      console.log(`\n✅ SUCCESS - Reply length: ${data.reply.length}`);
      console.log('\nReply preview (first 500 chars):');
      console.log(data.reply.substring(0, 500));
      if (data.reply.length > 500) {
        console.log('...[truncated]');
      }
    } else {
      console.log('\n❌ No reply in response');
      console.log(JSON.stringify(data).substring(0, 500));
    }
  } catch (error) {
    console.log(`\n❌ ERROR: ${error}`);
  }
}

async function main() {
  console.log('Testing Chat API Endpoint\n');
  console.log(`API Base: ${API_BASE}`);

  // Test each sport
  await testChatEndpoint('What NHL games are on today?', 'NHL Games');
  await testChatEndpoint('MLB schedule this week', 'MLB Games');
  await testChatEndpoint('NBA standings', 'NBA Standings');
  await testChatEndpoint('NFL games this weekend', 'NFL Games');

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

main().catch(console.error);
