/**
 * Integration test for BODS API
 * 
 * To run this test with a real API key:
 * BODS_API_KEY=your-api-key bun run src/test/integration.test.ts
 */

import { BODSClient } from '../index.js';

const API_KEY = process.env.BODS_API_KEY;

if (!API_KEY) {
  console.log('⏭️ Skipping integration tests - BODS_API_KEY not provided');
  console.log('To run integration tests: BODS_API_KEY=your-api-key bun run src/test/integration.test.ts');
  process.exit(0);
}

const client = new BODSClient({
  apiKey: API_KEY,
  timeout: 10000
});

async function testTimetables() {
  console.log('🔍 Testing Timetables API...');
  
  try {
    const timetables = await client.timetables.search({ limit: 5 });
    console.log(`✅ Found ${timetables.count} timetables`);
    
    if (timetables.results.length > 0) {
      const first = timetables.results[0]!;
      console.log(`   - ${first.operatorName}: ${first.name}`);
      
      const detailed = await client.timetables.getById(first.id);
      console.log(`✅ Retrieved detailed timetable: ${detailed.name}`);
    }
  } catch (error) {
    console.error('❌ Timetables test failed:', error);
  }
}

async function testFares() {
  console.log('🔍 Testing Fares API...');
  
  try {
    const fares = await client.fares.search({ limit: 3 });
    console.log(`✅ Found ${fares.count} fare datasets`);
    
    if (fares.results.length > 0) {
      const first = fares.results[0]!;
      console.log(`   - ${first.operatorName}: ${first.numOfFareZones} zones`);
    }
  } catch (error) {
    console.error('❌ Fares test failed:', error);
  }
}

async function testAVL() {
  console.log('🔍 Testing AVL API...');
  
  try {
    const vehicles = await client.avl.getSIRIVM({ 
      boundingBox: [-2.930, 53.374, -3.085, 53.453] // Liverpool area
    });
    console.log(`✅ Retrieved SIRI-VM data: ${vehicles.xmlData.length} characters`);
    
    const gtfsVehicles = await client.avl.getGTFSRT({
      boundingBox: [-2.930, 53.374, -3.085, 53.453]
    });
    console.log(`✅ Retrieved GTFS-RT data: ${gtfsVehicles.protobufData.byteLength} bytes`);
  } catch (error) {
    console.error('❌ AVL test failed:', error);
  }
}

async function testDisruptions() {
  console.log('🔍 Testing Disruptions API...');
  
  try {
    const disruptions = await client.disruptions.getCurrent();
    console.log(`✅ Retrieved disruptions: ${disruptions.xmlData.length} characters`);
    
    const parsed = await client.disruptions.getCurrentParsed();
    console.log(`✅ Parsed ${parsed.length} disruptions`);
    
    if (parsed.length > 0) {
      const first = parsed[0]!;
      console.log(`   - ${first.participantRef}: ${first.summary}`);
    }
  } catch (error) {
    console.error('❌ Disruptions test failed:', error);
  }
}

async function testConnectivity() {
  console.log('🔍 Testing API connectivity...');
  
  try {
    const isConnected = await client.testConnection();
    console.log(`✅ API connectivity: ${isConnected ? 'SUCCESS' : 'FAILED'}`);
  } catch (error) {
    console.error('❌ Connectivity test failed:', error);
  }
}

async function runIntegrationTests() {
  console.log('🚌 BODS Integration Tests');
  console.log('='.repeat(30));
  
  await testConnectivity();
  await testTimetables();
  await testFares();
  await testAVL();
  await testDisruptions();
  
  console.log('\n✅ Integration tests completed!');
}

if (import.meta.main) {
  runIntegrationTests().catch(console.error);
}
