#!/usr/bin/env python3
"""Fix the quotation page JavaScript - remove duplicate code and fix event listeners"""

import re

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

# Find and fix the pickupType event listener
# The problem is lines 1043-1061 are duplicated/misplaced code

# Pattern to find the broken section
old_pattern = r"""    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';
      \}
    \}\);

      // 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'\)\);
      \}
    \}\);

      // 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'\)\);
      \}
    \}\);"""

# Replacement with properly structured code
new_code = """    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';
      }
    });"""

# Apply the fix
content = re.sub(old_pattern, new_code, content, flags=re.MULTILINE)

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

print("✅ Fixed pickupType event listener")
print("✅ Removed duplicate code blocks")
print("✅ JavaScript should now work properly")
