/**
 * Test suite for Location Validator
 * Run with: node server/utils/locationValidator.test.js
 */

const { validateBookingLocations, isUKAirport, isInServiceArea } = require('./locationValidator');

console.log('🧪 Testing Ash Transfer Location Validator\n');

const tests = [
    // Valid scenarios
    {
        name: '✅ Manchester to Heathrow Airport',
        pickup: 'Manchester M1 1AA',
        dropoff: 'Heathrow Airport',
        expected: true
    },
    {
        name: '✅ Manchester Airport to Liverpool',
        pickup: 'Manchester Airport',
        dropoff: 'Liverpool L1 8JQ',
        expected: true
    },
    {
        name: '✅ Chester to Liverpool',
        pickup: 'Chester CH1 2AN',
        dropoff: 'Liverpool L3 9AG',
        expected: true
    },
    {
        name: '✅ Piccadilly Manchester to Manchester Airport',
        pickup: 'Piccadilly Manchester M1',
        dropoff: 'Manchester Airport MAN',
        expected: true
    },
    {
        name: '✅ Liverpool to London Gatwick',
        pickup: 'Liverpool Lime Street',
        dropoff: 'Gatwick Airport',
        expected: true
    },
    {
        name: '✅ Birmingham Airport to Manchester',
        pickup: 'Birmingham Airport BHX',
        dropoff: 'Manchester Piccadilly',
        expected: true
    },
    {
        name: '✅ Salford to Stockport',
        pickup: 'Salford M50',
        dropoff: 'Stockport SK1',
        expected: true
    },
    {
        name: '✅ Bolton to Leeds Bradford Airport',
        pickup: 'Bolton BL1',
        dropoff: 'Leeds Bradford Airport LBA',
        expected: true
    },
    
    // Invalid scenarios
    {
        name: '❌ London to Birmingham (no service area, no airport)',
        pickup: 'London SW1A 1AA',
        dropoff: 'Birmingham B1 1AA',
        expected: false
    },
    {
        name: '❌ Leeds to Sheffield (no service area, no airport)',
        pickup: 'Leeds LS1',
        dropoff: 'Sheffield S1',
        expected: false
    },
    {
        name: '❌ Edinburgh to Glasgow (Scotland, no service area)',
        pickup: 'Edinburgh EH1',
        dropoff: 'Glasgow G1',
        expected: false
    },
    {
        name: '❌ Bristol to Cardiff (Wales, no service area)',
        pickup: 'Bristol BS1',
        dropoff: 'Cardiff CF1',
        expected: false
    }
];

let passed = 0;
let failed = 0;

tests.forEach(test => {
    const result = validateBookingLocations(test.pickup, test.dropoff);
    const success = result.valid === test.expected;
    
    if (success) {
        passed++;
        console.log(`${test.name}`);
        console.log(`   Pickup: ${test.pickup}`);
        console.log(`   Dropoff: ${test.dropoff}`);
        console.log(`   Result: ${result.message}`);
        console.log(`   ✓ PASS\n`);
    } else {
        failed++;
        console.log(`${test.name}`);
        console.log(`   Pickup: ${test.pickup}`);
        console.log(`   Dropoff: ${test.dropoff}`);
        console.log(`   Expected: ${test.expected ? 'VALID' : 'INVALID'}`);
        console.log(`   Got: ${result.valid ? 'VALID' : 'INVALID'}`);
        console.log(`   Message: ${result.message}`);
        console.log(`   ✗ FAIL\n`);
    }
});

console.log('━'.repeat(60));
console.log(`📊 Test Results: ${passed} passed, ${failed} failed out of ${tests.length} tests`);
console.log('━'.repeat(60));

// Additional unit tests
console.log('\n🔍 Unit Tests:\n');

console.log('Airport Detection:');
console.log(`  "Manchester Airport" → ${isUKAirport('Manchester Airport') ? '✓' : '✗'} Airport`);
console.log(`  "Heathrow" → ${isUKAirport('Heathrow') ? '✓' : '✗'} Airport`);
console.log(`  "Manchester City Centre" → ${isUKAirport('Manchester City Centre') ? '✗' : '✓'} Not Airport`);

console.log('\nService Area Detection:');
console.log(`  "Manchester M1 1AA" → ${isInServiceArea('Manchester M1 1AA') ? '✓' : '✗'} In Area`);
console.log(`  "Liverpool L1" → ${isInServiceArea('Liverpool L1') ? '✓' : '✗'} In Area`);
console.log(`  "Chester CH1" → ${isInServiceArea('Chester CH1') ? '✓' : '✗'} In Area`);
console.log(`  "London SW1A" → ${isInServiceArea('London SW1A') ? '✗' : '✓'} Not In Area`);

console.log('\n✅ Location Validator Testing Complete!');
