import urllib.request
import urllib.parse
import json
import random
import time

BASE_URL = "http://127.0.0.1:8080/leads_collection_webhook.php"

def create_lead(index):
    # Generate random Pakistani phone number
    phone = f"923{random.randint(10, 49)}{random.randint(1000000, 9999999)}"
    
    sources = ['meta_ad', 'organic', 'google-ads', 'facebook-ads']
    source = random.choice(sources)
    
    payload = {
        "whatsappNumber": phone,
        "source": source,
        "messageText": f"Dummy lead message {index}",
        "channel": "whatsapp",
        "campaignId": f"camp_{random.randint(100,999)}" if source != 'organic' else ""
    }
    
    print(f"[{index}/10] Creating lead {phone} ({source})...", end=" ")
    
    try:
        data = urllib.parse.urlencode(payload).encode()
        req = urllib.request.Request(BASE_URL, data=data, method='POST')
        req.add_header('Content-Type', 'application/x-www-form-urlencoded')

        with urllib.request.urlopen(req) as response:
            status = response.getcode()
            body = response.read().decode()
            
            if status == 200:
                print("SUCCESS")
            else:
                print(f"FAILED ({status})")
                print(body)
                
    except Exception as e:
        print(f"ERROR: {e}")

print("Starting generation of 10 dummy leads...")
for i in range(1, 11):
    create_lead(i)
    time.sleep(0.5) # small delay to be nice
print("Done.")
