import re

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

# New pickup type handler
new_pickup_handler = '''    pickupType.addEventListener('change', function() {
      const pickupAddress = document.getElementById('pickupAddress');
      const pickupNote = document.getElementById('pickupNote');
      
      if (this.value === 'custom-address') {
        // Show address input
        pickup.style.display = 'none';
        pickup.disabled = true;
        pickup.removeAttribute('required');
        pickupAddress.style.display = 'block';
        pickupAddress.setAttribute('required', 'required');
        pickupNote.style.display = 'block';
      } else if (this.value === 'service-area' || this.value === 'airport') {
        // Show dropdown
        pickup.style.display = 'block';
        pickup.disabled = false;
        pickup.setAttribute('required', 'required');
        pickupAddress.style.display = 'none';
        pickupAddress.removeAttribute('required');
        pickupNote.style.display = 'none';
        
        pickup.innerHTML = '<option value="">Select location...</option>';
        const locations = this.value === 'airport' ? UK_AIRPORTS : SERVICE_AREAS;
        locations.forEach(loc => {
          const option = document.createElement('option');
          option.value = loc.value;
          option.textContent = loc.label;
          pickup.appendChild(option);
        });
      } else {
        // Reset
        pickup.style.display = 'block';
        pickup.disabled = true;
        pickupAddress.style.display = 'none';
        pickupNote.style.display = 'none';
      }
    });'''

# Replace old pickup handler
old_pickup_pattern = r'    pickupType\.addEventListener\(\'change\', function\(\) \{[\s\S]*?\}\);'
content = re.sub(old_pickup_pattern, new_pickup_handler, content, count=1)

# New dropoff type handler
new_dropoff_handler = '''    dropoffType.addEventListener('change', function() {
      const dropoffAddress = document.getElementById('dropoffAddress');
      const dropoffNote = document.getElementById('dropoffNote');
      
      if (this.value === 'custom-address') {
        // Show address input
        dropoff.style.display = 'none';
        dropoff.disabled = true;
        dropoff.removeAttribute('required');
        dropoffAddress.style.display = 'block';
        dropoffAddress.setAttribute('required', 'required');
        dropoffNote.style.display = 'block';
      } else if (this.value === 'service-area' || this.value === 'airport') {
        // Show dropdown
        dropoff.style.display = 'block';
        dropoff.disabled = false;
        dropoff.setAttribute('required', 'required');
        dropoffAddress.style.display = 'none';
        dropoffAddress.removeAttribute('required');
        dropoffNote.style.display = 'none';
        
        dropoff.innerHTML = '<option value="">Select location...</option>';
        const locations = this.value === 'airport' ? UK_AIRPORTS : SERVICE_AREAS;
        locations.forEach(loc => {
          const option = document.createElement('option');
          option.value = loc.value;
          option.textContent = loc.label;
          dropoff.appendChild(option);
        });
      } else {
        // Reset
        dropoff.style.display = 'block';
        dropoff.disabled = true;
        dropoffAddress.style.display = 'none';
        dropoffNote.style.display = 'none';
      }
    });'''

# Replace old dropoff handler
old_dropoff_pattern = r'    dropoffType\.addEventListener\(\'change\', function\(\) \{[\s\S]*?\}\);'
content = re.sub(old_dropoff_pattern, new_dropoff_handler, content, count=1)

# Remove old postcode method handlers if they exist
content = re.sub(r'    // Pickup method selector.*?pickupMethod\.addEventListener.*?\}\);', '', content, flags=re.DOTALL)
content = re.sub(r'    // Dropoff method selector.*?dropoffMethod\.addEventListener.*?\}\);', '', content, flags=re.DOTALL)

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

print("✅ Updated JavaScript handlers")
print("✅ Added 'custom-address' logic")
print("✅ Show/hide address input based on selection")
print("✅ Removed old postcode method selectors")
