import requests
import re

# Auth
soap_auth = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:ns="http://schemas.tradetracker.com/affiliate">
  <soap:Body>
    <ns:authenticate>
      <ns:customerID>35058</ns:customerID>
      <ns:passphrase>bf0b9eef0da6caf55fe614518375280fe5c4ecca</ns:passphrase>
      <ns:sandbox>false</ns:sandbox>
      <ns:locale>nl_NL</ns:locale>
      <ns:demo>false</ns:demo>
    </ns:authenticate>
  </soap:Body>
</soap:Envelope>'''

r = requests.post('https://ws.tradetracker.com/soap/affiliate',
                  data=soap_auth,
                  headers={'Content-Type': 'text/xml; charset=utf-8', 'SOAPAction': 'authenticate'},
                  timeout=15)
cookies = r.cookies
print(f'Auth: {r.status_code}')

# Get affiliate sites first
soap_sites = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:ns="http://schemas.tradetracker.com/affiliate">
  <soap:Body>
    <ns:getAffiliateSites/>
  </soap:Body>
</soap:Envelope>'''

# The WSDL says getAffiliateSites takes no params. But error says "Missing parameter"
# Maybe we need to set affiliateSiteID on the session first via setAffiliateSiteID
# Or pass it as header. Let me check WSDL...

# Actually, TradeTracker SOAP requires calling getAffiliateSites first, then setAffiliateSite
# But let's try getting WSDL first
print('\n--- Checking WSDL ---')
r_wsdl = requests.get('https://ws.tradetracker.com/soap/affiliate?wsdl', timeout=15)
wsdl = r_wsdl.text

# Find all operations
ops = re.findall(r'<operation name="([^"]+)"', wsdl)
print(f'Operations: {ops}')

# Check if there's a getAffiliateSites that needs params
sites_part = wsdl[wsdl.find('getAffiliateSites'):wsdl.find('getAffiliateSites')+500]
print(f'\ngetAffiliateSites context: {sites_part[:300]}')

# Try with explicit empty params
soap_sites2 = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:ns1="https://ws.tradetracker.com/soap/affiliate">
  <soap:Body>
    <ns1:getAffiliateSites/>
  </soap:Body>
</soap:Envelope>'''

r2 = requests.post('https://ws.tradetracker.com/soap/affiliate',
                   data=soap_sites2,
                   headers={'Content-Type': 'text/xml; charset=utf-8', 'SOAPAction': ''},
                   cookies=cookies, timeout=30)
print(f'\ngetAffiliateSites v2: {r2.status_code}')
print(f'{r2.text[:1000]}')

# Try with the correct namespace from WSDL response
soap_sites3 = '''<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:ns1="https://ws.tradetracker.com/soap/affiliate">
  <SOAP-ENV:Body>
    <ns1:getAffiliateSites/>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>'''

r3 = requests.post('https://ws.tradetracker.com/soap/affiliate',
                   data=soap_sites3,
                   headers={'Content-Type': 'text/xml; charset=utf-8'},
                   cookies=cookies, timeout=30)
print(f'\ngetAffiliateSites v3: {r3.status_code}')
print(f'{r3.text[:2000]}')
