import re

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

# Find the form submission section and update it to handle postcodes
new_form_submission = '''      // Get form data
      const formData = new FormData(quoteForm);
      const pickupTypeValue = formData.get('pickupType');
      const dropoffTypeValue = formData.get('dropoffType');
      const pickupMethodValue = formData.get('pickupMethod') || 'select';
      const dropoffMethodValue = formData.get('dropoffMethod') || 'select';
      
      // Get actual location values (either from select or postcode)
      let pickupValue, dropoffValue;
      
      if (pickupMethodValue === 'postcode' && pickupTypeValue === 'service-area') {
        const postcode = formData.get('pickupPostcode');
        // Validate postcode format (basic)
        if (!postcode || postcode.trim().length < 4) {
          errorAlert.textContent = '❌ Please enter a valid pickup postcode';
          errorAlert.classList.add('show');
          return;
        }
        pickupValue = 'postcode:' + postcode.trim().toUpperCase();
      } else {
        pickupValue = formData.get('pickup');
      }
      
      if (dropoffMethodValue === 'postcode' && dropoffTypeValue === 'service-area') {
        const postcode = formData.get('dropoffPostcode');
        // Validate postcode format (basic)
        if (!postcode || postcode.trim().length < 4) {
          errorAlert.textContent = '❌ Please enter a valid dropoff postcode';
          errorAlert.classList.add('show');
          return;
        }
        dropoffValue = 'postcode:' + postcode.trim().toUpperCase();
      } else {
        dropoffValue = formData.get('dropoff');
      }
      
      const vehicleType = formData.get('vehicle');
      const meetGreet = formData.get('meetGreet');
      const childSeat = formData.get('childSeat');
      const returnJourney = formData.get('returnJourney');'''

# Replace the old form data section
old_pattern = r'      // Get form data\n      const formData = new FormData\(quoteForm\);\n      const pickupTypeValue = formData\.get\(\'pickupType\'\);\n      const dropoffTypeValue = formData\.get\(\'dropoffType\'\);\n      const pickupValue = formData\.get\(\'pickup\'\);\n      const dropoffValue = formData\.get\(\'dropoff\'\);\n      const vehicleType = formData\.get\(\'vehicle\'\);\n      const meetGreet = formData\.get\(\'meetGreet\'\);\n      const childSeat = formData\.get\(\'childSeat\'\);\n      const returnJourney = formData\.get\(\'returnJourney\'\);'

content = re.sub(
    old_pattern,
    new_form_submission,
    content
)

# Update distance calculation to handle postcodes
new_distance_with_postcode = '''      // Calculate distance between pickup and dropoff
      let estimatedMiles;
      
      // Handle postcode inputs (estimate 20 miles average for postcode)
      if (pickupValue.startsWith('postcode:') || dropoffValue.startsWith('postcode:')) {
        if (pickupValue.startsWith('postcode:') && dropoffValue.startsWith('postcode:')) {
          // Both postcodes - default 20 miles
          estimatedMiles = 20;
        } else if (pickupValue.startsWith('postcode:')) {
          // Pickup is postcode, dropoff is known location
          const dropoffDist = LOCATION_DISTANCES[dropoffValue] || 20;
          estimatedMiles = dropoffDist;
        } else {
          // Dropoff is postcode, pickup is known location
          const pickupDist = LOCATION_DISTANCES[pickupValue] || 20;
          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') {
          // 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 distance calculation
old_dist_pattern = r'      // Calculate distance between pickup and dropoff\n      let estimatedMiles;[\s\S]+?      \}'

content = re.sub(
    old_dist_pattern,
    new_distance_with_postcode,
    content,
    count=1
)

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

print("✅ Updated form submission to handle postcodes")
print("✅ Added postcode validation")
print("✅ Updated distance calculation for postcode inputs")
