import re

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

# Add postcode handling logic after the location type change handlers
postcode_logic = '''
    // Pickup method selector (for service areas only)
    const pickupMethod = document.getElementById('pickupMethod');
    const pickupPostcode = document.getElementById('pickupPostcode');
    const pickupPostcodeNote = document.getElementById('pickupPostcodeNote');
    
    pickupMethod.addEventListener('change', function() {
      if (this.value === 'postcode') {
        pickup.style.display = 'none';
        pickup.removeAttribute('required');
        pickupPostcode.style.display = 'block';
        pickupPostcode.setAttribute('required', 'required');
        pickupPostcodeNote.style.display = 'block';
      } else {
        pickup.style.display = 'block';
        pickup.setAttribute('required', 'required');
        pickupPostcode.style.display = 'none';
        pickupPostcode.removeAttribute('required');
        pickupPostcodeNote.style.display = 'none';
      }
    });

    // Dropoff method selector (for service areas only)
    const dropoffMethod = document.getElementById('dropoffMethod');
    const dropoffPostcode = document.getElementById('dropoffPostcode');
    const dropoffPostcodeNote = document.getElementById('dropoffPostcodeNote');
    
    dropoffMethod.addEventListener('change', function() {
      if (this.value === 'postcode') {
        dropoff.style.display = 'none';
        dropoff.removeAttribute('required');
        dropoffPostcode.style.display = 'block';
        dropoffPostcode.setAttribute('required', 'required');
        dropoffPostcodeNote.style.display = 'block';
      } else {
        dropoff.style.display = 'block';
        dropoff.setAttribute('required', 'required');
        dropoffPostcode.style.display = 'none';
        dropoffPostcode.removeAttribute('required');
        dropoffPostcodeNote.style.display = 'none';
      }
    });
'''

# Insert after the dropoffType change handler
content = re.sub(
    r'(    dropoffType\.addEventListener\(\'change\', function\(\) \{[^}]+\}\);)',
    r'\1' + postcode_logic,
    content
)

# Update pickupType handler to enable pickupMethod for service areas
pickup_type_update = '''    pickupType.addEventListener('change', function() {
      pickup.disabled = false;
      pickupMethod.disabled = false;
      pickupMethod.value = 'select';
      pickup.style.display = 'block';
      pickupPostcode.style.display = 'none';
      pickupPostcodeNote.style.display = 'none';
      pickup.innerHTML = '<option value="">Select location...</option>';
      
      const locations = this.value === 'airport' ? UK_AIRPORTS : SERVICE_AREAS;
      
      // Only show method selector for service areas
      if (this.value === 'service-area') {
        pickupMethod.style.display = 'block';
      } else {
        pickupMethod.style.display = 'none';
        pickupMethod.disabled = true;
      }
      
      locations.forEach(loc => {
        const option = document.createElement('option');
        option.value = loc.value;
        option.textContent = loc.label;
        pickup.appendChild(option);
      });

      // Auto-set dropoff type based on business rules
      if (this.value === 'service-area') {
        dropoffType.value = 'airport';
        dropoffType.dispatchEvent(new Event('change'));
      } else if (this.value === 'airport') {
        dropoffType.value = 'service-area';
        dropoffType.dispatchEvent(new Event('change'));
      }
    });'''

content = re.sub(
    r'    pickupType\.addEventListener\(\'change\', function\(\) \{[^}]+\}\);',
    pickup_type_update,
    content,
    count=1
)

# Update dropoffType handler similarly
dropoff_type_update = '''    dropoffType.addEventListener('change', function() {
      dropoff.disabled = false;
      dropoffMethod.disabled = false;
      dropoffMethod.value = 'select';
      dropoff.style.display = 'block';
      dropoffPostcode.style.display = 'none';
      dropoffPostcodeNote.style.display = 'none';
      dropoff.innerHTML = '<option value="">Select location...</option>';
      
      const locations = this.value === 'airport' ? UK_AIRPORTS : SERVICE_AREAS;
      
      // Only show method selector for service areas
      if (this.value === 'service-area') {
        dropoffMethod.style.display = 'block';
      } else {
        dropoffMethod.style.display = 'none';
        dropoffMethod.disabled = true;
      }
      
      locations.forEach(loc => {
        const option = document.createElement('option');
        option.value = loc.value;
        option.textContent = loc.label;
        dropoff.appendChild(option);
      });
    });'''

content = re.sub(
    r'    dropoffType\.addEventListener\(\'change\', function\(\) \{[^}]+\}\);',
    dropoff_type_update,
    content,
    count=1
)

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

print("✅ Added postcode method selector logic")
print("✅ Updated pickup/dropoff type handlers")
print("✅ Service areas now support postcode input")
