import urllib.request
import urllib.parse
import http.cookiejar
import re

cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))

print("=== 1. Testing GET /login ===")
resp = opener.open("http://127.0.0.1:8000/login")
html = resp.read().decode("utf-8")
print("Status:", resp.status)
print("Contains LDAP:", "ldap_username" in html)
print("Contains WSPS Logo:", "wsps-logo.svg" in html)

# Extract CSRF token
csrf_match = re.search(r'name="_token"\s+value="([^"]+)"', html)
token = csrf_match.group(1) if csrf_match else None
print("CSRF Token found:", bool(token))

print("\n=== 2. Testing POST /login with admin / admin123 ===")
login_data = urllib.parse.urlencode({
    "_token": token,
    "ldap_username": "admin",
    "password": "admin123"
}).encode("utf-8")

req = urllib.request.Request("http://127.0.0.1:8000/login", data=login_data)
resp = opener.open(req)
html_home = resp.read().decode("utf-8")
print("Status:", resp.status)
print("URL after login:", resp.geturl())
print("Contains 'Pilih Jenis Pengiriman':", "Pilih Jenis Pengiriman" in html_home)
print("Contains 'Truck Charter':", "Truck Charter" in html_home)
print("Contains 'Air Freight & Reguler':", "Air Freight & Reguler" in html_home)
print("Contains 'Copyright by : Muhammad Rifqi':", "Muhammad Rifqi" in html_home)

print("\n=== 3. Testing GET /truck-charter ===")
resp = opener.open("http://127.0.0.1:8000/truck-charter")
html_truck = resp.read().decode("utf-8")
print("Status:", resp.status)
print("Contains '1. Tujuan':", "1. Tujuan" in html_truck)
print("Contains '2. Dimensi':", "2. Dimensi" in html_truck)
print("Contains '3. Jumlah Koli':", "3. Jumlah Koli" in html_truck)
print("Contains '4. Total Berat':", "4. Total Berat" in html_truck)

# Extract new CSRF token
csrf_match = re.search(r'name="_token"\s+value="([^"]+)"', html_truck)
token2 = csrf_match.group(1) if csrf_match else token

print("\n=== 4. Testing POST /truck-charter/search (Destination JAKARTA) ===")
search_data = urllib.parse.urlencode({
    "_token": token2,
    "destination_id": 1,
    "length": 150,
    "width": 100,
    "height": 100,
    "coly": 5,
    "weight": 1000
}).encode("utf-8")

req = urllib.request.Request("http://127.0.0.1:8000/truck-charter/search", data=search_data)
resp = opener.open(req)
html_result = resp.read().decode("utf-8")
print("Status:", resp.status)
print("Contains 'Rekomendasi Ekspedisi Termurah & Tercepat':", "Rekomendasi Ekspedisi Termurah & Tercepat" in html_result)
print("Contains 'Buka Linktree Ekspedisi':", "Buka Linktree Ekspedisi" in html_result)
print("Contains comparison table:", "Tabel Perbandingan" in html_result)

print("\n=== 5. Testing GET Linktree page ===")
resp = opener.open("http://127.0.0.1:8000/linktree/adam-indo?destination=JAKARTA&service=Truck+Charter&cost=10000000")
html_linktree = resp.read().decode("utf-8")
print("Status:", resp.status)
print("Contains 'ADAM INDO':", "ADAM INDO" in html_linktree)
print("Contains 'Hubungi Ekspedisi':", "Hubungi Ekspedisi" in html_linktree)
print("Contains 'Hubungi Petugas Gudang':", "Hubungi Petugas Gudang" in html_linktree)

print("\n=== 6. Testing GET /admin Dashboard ===")
resp = opener.open("http://127.0.0.1:8000/admin")
html_admin = resp.read().decode("utf-8")
print("Status:", resp.status)
print("Contains 'Dashboard Overview':", "Dashboard Overview" in html_admin)
print("Contains 'Import Data Excel':", "Import Data Excel" in html_admin)
print("Contains 'Setting Linktree Ekspedisi':", "Setting Linktree Ekspedisi" in html_admin)

print("\n=== 7. Testing GET /admin/linktree/1/edit ===")
resp = opener.open("http://127.0.0.1:8000/admin/linktree/1/edit")
html_linktree_edit = resp.read().decode("utf-8")
print("Status:", resp.status)
print("Contains 'Setting Tombol Linktree Dinamis':", "Setting Tombol Linktree Dinamis" in html_linktree_edit)
print("Contains 'Dynamic Inputs':", "Dynamic Inputs" in html_linktree_edit)

print("\n=== 8. Testing Select2 Destination dropdown in /truck-charter & /air-freight ===")
resp_truck = opener.open("http://127.0.0.1:8000/truck-charter")
html_truck = resp_truck.read().decode("utf-8")
print("Truck has destination_select:", 'id="destination_select"' in html_truck)
print("Truck has select2-destination class:", 'select2-destination' in html_truck)
print("Truck has Select2 JS:", 'select2.min.js' in html_truck)
print("Old datalist removed:", 'datalist id="destinationList"' not in html_truck)

resp_air = opener.open("http://127.0.0.1:8000/air-freight")
html_air = resp_air.read().decode("utf-8")
print("Air has destination_select:", 'id="destination_select"' in html_air)
print("Air has select2-destination class:", 'select2-destination' in html_air)
print("Air has Select2 JS:", 'select2.min.js' in html_air)
print("Old datalist removed:", 'datalist id="destinationList"' not in html_air)

print("\n=== ALL ROUTES AND SELECT2 VERIFIED SUCCESSFULLY! ===")
