/**
 * Frontend Location Validator for Ash Transfer
 * Client-side validation that matches server-side rules
 */

// UK airports with their common names/codes
const UK_AIRPORTS = [
    'manchester airport', 'man', 'ringway',
    'liverpool airport', 'ljla', 'john lennon',
    'heathrow', 'lhr', 'london heathrow',
    'gatwick', 'lgw', 'london gatwick',
    'stansted', 'stn', 'london stansted',
    'luton', 'ltn', 'london luton',
    'city airport', 'lcy', 'london city',
    'birmingham airport', 'bhx',
    'east midlands airport', 'ema',
    'leeds bradford', 'lba',
    'newcastle airport', 'ncl',
    'edinburgh airport', 'edi',
    'glasgow airport', 'gla',
    'aberdeen airport', 'abz',
    'belfast airport', 'bfs', 'bhd',
    'bristol airport', 'brs',
    'cardiff airport', 'cwr',
    'southampton airport', 'sou',
    'exeter airport', 'ext',
    'bournemouth airport', 'boh',
    'doncaster sheffield', 'dsa',
    'inverness airport', 'inv',
    'prestwick', 'pik',
    'airport', 'terminal'
];

// Service area postcodes
const SERVICE_AREA_POSTCODES = [
    'm1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8', 'm9',
    'm11', 'm12', 'm13', 'm14', 'm15', 'm16', 'm17', 'm18', 'm19',
    'm20', 'm21', 'm22', 'm23', 'm24', 'm25', 'm26', 'm27', 'm28', 'm29',
    'm30', 'm31', 'm32', 'm33', 'm34', 'm35', 'm38', 'm40', 'm41', 'm43', 'm44', 'm45', 'm46', 'm50', 'm60', 'm90',
    'wn', 'bl', 'ol', 'sk', 'wa',
    'ch1', 'ch2', 'ch3', 'ch4', 'ch65', 'ch66',
    'l1', 'l2', 'l3', 'l4', 'l5', 'l6', 'l7', 'l8', 'l9',
    'l10', 'l11', 'l12', 'l13', 'l14', 'l15', 'l16', 'l17', 'l18', 'l19',
    'l20', 'l21', 'l22', 'l23', 'l24', 'l25', 'l26', 'l27', 'l28', 'l29',
    'l30', 'l31', 'l32', 'l33', 'l34', 'l35', 'l36', 'l37', 'l38', 'l39', 'l40', 'l70', 'l75', 'l80'
];

const SERVICE_AREA_TOWNS = [
    'manchester', 'salford', 'stockport', 'bolton', 'bury', 'oldham',
    'rochdale', 'wigan', 'trafford', 'altrincham', 'sale', 'urmston',
    'stretford', 'ashton-under-lyne', 'stalybridge', 'hyde', 'denton',
    'leigh', 'eccles', 'swinton', 'radcliffe', 'prestwich', 'whitefield',
    'middleton', 'failsworth', 'chadderton', 'shaw', 'heywood', 'ramsbottom',
    'chester', 'ellesmere port', 'neston', 'frodsham',
    'liverpool', 'bootle', 'crosby', 'formby', 'maghull', 'kirkby',
    'huyton', 'prescot', 'knowsley', 'halewood', 'speke', 'garston',
    'allerton', 'childwall', 'woolton', 'wavertree', 'mossley hill',
    'west derby', 'anfield', 'walton', 'aintree', 'fazakerley',
    'birkenhead', 'wallasey', 'bebington', 'heswall', 'wirral'
];

function isUKAirport(location) {
    if (!location) return false;
    const normalized = location.toLowerCase().trim();
    return UK_AIRPORTS.some(airport => normalized.includes(airport));
}

function isInServiceArea(location) {
    if (!location) return false;
    const normalized = location.toLowerCase().trim();
    
    // Check postcode patterns
    const postcodeMatch = normalized.match(/([a-z]{1,2}\d{1,2})/i);
    if (postcodeMatch) {
        const postcode = postcodeMatch[1].toLowerCase();
        if (SERVICE_AREA_POSTCODES.some(pc => postcode.startsWith(pc))) {
            return true;
        }
    }
    
    // Check town names
    return SERVICE_AREA_TOWNS.some(town => normalized.includes(town));
}

function validateBookingLocations(pickup, dropoff) {
    if (!pickup || !dropoff) {
        return {
            valid: false,
            message: 'Both pickup and dropoff locations are required'
        };
    }
    
    const pickupIsAirport = isUKAirport(pickup);
    const dropoffIsAirport = isUKAirport(dropoff);
    const pickupInArea = isInServiceArea(pickup);
    const dropoffInArea = isInServiceArea(dropoff);
    
    const airportInvolved = pickupIsAirport || dropoffIsAirport;
    const oneLocationInArea = pickupInArea || dropoffInArea;
    
    if (airportInvolved && oneLocationInArea) {
        return { valid: true, message: 'Valid airport transfer' };
    }
    
    if (pickupInArea && dropoffInArea) {
        return { valid: true, message: 'Valid local transfer' };
    }
    
    if (oneLocationInArea && !airportInvolved) {
        return { valid: true, message: 'Transfer from/to service area' };
    }
    
    return {
        valid: false,
        message: '⚠️ Sorry, we only accept bookings where the pickup or dropoff is in the Greater Manchester, Chester, or Liverpool area.\n\n' +
                 '✅ However, we provide transfers to/from ANY UK airport.\n\n' +
                 '📍 Service Areas:\n' +
                 '   • Greater Manchester (all areas)\n' +
                 '   • Chester and surrounding areas\n' +
                 '   • Liverpool and Merseyside\n' +
                 '   • Any UK airport\n\n' +
                 '📞 Need help? Call +44 7860 261578'
    };
}

// Export for use in other scripts
window.AshTransferLocationValidator = {
    isUKAirport,
    isInServiceArea,
    validateBookingLocations
};
