import re

# Read the file
with open('quotation.html', 'r', encoding='utf-8') as f:
    content = f.read()

# New improved location distances (in miles) from Manchester city center
LOCATION_DISTANCES = '''    // Location distances from Manchester (approximate miles)
    const LOCATION_DISTANCES = {
      // Service Areas
      'manchester': 0,
      'salford': 3,
      'stockport': 7,
      'bolton': 12,
      'bury': 9,
      'oldham': 8,
      'rochdale': 13,
      'wigan': 17,
      'chester': 40,
      'liverpool': 35,
      'wirral': 45,
      'warrington': 20,
      
      // Airports
      'manchester-airport': 10,
      'liverpool-airport': 38,
      'heathrow': 200,
      'gatwick': 220,
      'stansted': 180,
      'luton': 170,
      'london-city': 210,
      'birmingham': 85,
      'east-midlands': 70,
      'leeds-bradford': 45,
      'newcastle': 140,
      'edinburgh': 215,
      'glasgow': 220,
      'aberdeen': 340,
      'bristol': 160,
      'cardiff': 160,
      'belfast': 210,
      'southampton': 230,
      'exeter': 250
    };'''

# Replace SERVICE_AREAS definition to start with location distances
content = re.sub(
    r'    // Service Areas\n    const SERVICE_AREAS = \[',
    LOCATION_DISTANCES + '\n\n    // Service Areas\n    const SERVICE_AREAS = [',
    content
)

# New improved distance calculation
new_distance_calc = '''      // Calculate distance between pickup and dropoff
      let estimatedMiles;
      const pickupDist = LOCATION_DISTANCES[pickupValue] || 0;
      const dropoffDist = LOCATION_DISTANCES[dropoffValue] || 0;
      
      if (pickupTypeValue === 'service-area' && dropoffTypeValue === 'service-area') {
        // Both service areas - calculate distance between them
        estimatedMiles = Math.abs(pickupDist - dropoffDist);
        // Minimum 10 miles for local transfers
        estimatedMiles = Math.max(estimatedMiles, 10);
      } else if (pickupTypeValue === 'airport' || dropoffTypeValue === 'airport') {
        // One is airport - calculate from service area to airport
        if (pickupTypeValue === 'airport') {
          estimatedMiles = Math.abs(pickupDist - dropoffDist);
        } else {
          estimatedMiles = Math.abs(dropoffDist - pickupDist);
        }
        // Minimum 10 miles for airport transfers
        estimatedMiles = Math.max(estimatedMiles, 10);
      } else {
        estimatedMiles = 30; // Default
      }'''

# Replace old distance calculation
old_distance_pattern = r'      // Estimate distance \(simplified - in production use routing API\)\n      let estimatedMiles;\n      if \(pickupTypeValue === \'airport\' \|\| dropoffTypeValue === \'airport\'\) \{\n        estimatedMiles = 200; // Airport transfer average\n      \} else \{\n        estimatedMiles = 30; // Local transfer average\n      \}'

content = re.sub(
    old_distance_pattern,
    new_distance_calc,
    content
)

# Write back
with open('quotation.html', 'w', encoding='utf-8') as f:
    f.write(content)

print("✅ Fixed distance calculation logic")
print("✅ Added location distances dictionary")
print("✅ Improved quote calculation accuracy")
