import re

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

# New form submission data handling
new_form_data = '''      // Get form data
      const formData = new FormData(quoteForm);
      const pickupTypeValue = formData.get('pickupType');
      const dropoffTypeValue = formData.get('dropoffType');
      
      // Get actual location values
      let pickupValue, dropoffValue;
      
      if (pickupTypeValue === 'custom-address') {
        const address = formData.get('pickupAddress');
        if (!address || address.trim().length < 10) {
          errorAlert.textContent = '❌ Please enter a complete pickup address with postcode';
          errorAlert.classList.add('show');
          return;
        }
        pickupValue = 'address:' + address.trim();
      } else {
        pickupValue = formData.get('pickup');
        if (!pickupValue) {
          errorAlert.textContent = '❌ Please select a pickup location';
          errorAlert.classList.add('show');
          return;
        }
      }
      
      if (dropoffTypeValue === 'custom-address') {
        const address = formData.get('dropoffAddress');
        if (!address || address.trim().length < 10) {
          errorAlert.textContent = '❌ Please enter a complete dropoff address with postcode';
          errorAlert.classList.add('show');
          return;
        }
        dropoffValue = 'address:' + address.trim();
      } else {
        dropoffValue = formData.get('dropoff');
        if (!dropoffValue) {
          errorAlert.textContent = '❌ Please select a dropoff location';
          errorAlert.classList.add('show');
          return;
        }
      }
      
      const vehicleType = formData.get('vehicle');
      const meetGreet = formData.get('meetGreet');
      const childSeat = formData.get('childSeat');
      const returnJourney = formData.get('returnJourney');'''

# Replace old form data handling (find the pattern)
old_pattern = r'      // Get form data[\s\S]*?const returnJourney = formData\.get\(\'returnJourney\'\);'
content = re.sub(old_pattern, new_form_data, content, count=1)

# Update distance calculation to handle custom addresses
new_distance = '''      // Calculate distance
      let estimatedMiles;
      
      // Handle custom addresses (use default 25 miles for custom addresses)
      if (pickupValue.startsWith('address:') || dropoffValue.startsWith('address:')) {
        if (pickupValue.startsWith('address:') && dropoffValue.startsWith('address:')) {
          // Both custom addresses - default 25 miles
          estimatedMiles = 25;
        } else if (pickupValue.startsWith('address:')) {
          // Pickup is custom, dropoff is known location
          const dropoffDist = LOCATION_DISTANCES[dropoffValue] || 25;
          estimatedMiles = dropoffDist;
        } else {
          // Dropoff is custom, pickup is known location
          const pickupDist = LOCATION_DISTANCES[pickupValue] || 25;
          estimatedMiles = pickupDist;
        }
      } else {
        // Both are known locations
        const pickupDist = LOCATION_DISTANCES[pickupValue] || 0;
        const dropoffDist = LOCATION_DISTANCES[dropoffValue] || 0;
        
        if (pickupTypeValue === 'service-area' && dropoffTypeValue === 'service-area') {
          estimatedMiles = Math.abs(pickupDist - dropoffDist);
          estimatedMiles = Math.max(estimatedMiles, 10);
        } else if (pickupTypeValue === 'airport' || dropoffTypeValue === 'airport') {
          if (pickupTypeValue === 'airport') {
            estimatedMiles = Math.abs(pickupDist - dropoffDist);
          } else {
            estimatedMiles = Math.abs(dropoffDist - pickupDist);
          }
          estimatedMiles = Math.max(estimatedMiles, 10);
        } else {
          estimatedMiles = 30;
        }
      }'''

# Replace distance calculation
old_dist = r'      // Calculate distance between pickup and dropoff[\s\S]*?      \}'
content = re.sub(old_dist, new_distance, content, count=1)

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

print("✅ Updated form submission for custom addresses")
print("✅ Added address validation (min 10 characters)")
print("✅ Updated distance calculation for custom addresses")
print("✅ Default 25 miles for custom address transfers")
