import urllib.request
import urllib.error
import json

# Detailed POST login test
print("Testing POST /api/v1/auth/login")
for username, password in [("e2etest", "Test12345!"), ("admin", "admin123")]:
    data = json.dumps({"username": username, "password": password}).encode("utf-8")
    req = urllib.request.Request(
        "http://127.0.0.1:5000/api/v1/auth/login",
        data=data,
        headers={
            "Content-Type": "application/json",
            "Accept": "application/json",
        },
    )
    try:
        with urllib.request.urlopen(req) as resp:
            body = resp.read().decode()
            print(f"[{resp.status}] {username}: SUCCESS! {body[:100]}")
    except urllib.error.HTTPError as e:
        body = e.read().decode()
        print(f"[{e.code}] {username}: {body[:300]}")
    except Exception as e:
        print(f"[ERR] {username}: {e}")

# Also test /register
print("\nTesting POST /api/v1/auth/register")
data = json.dumps({"username": "e2etestX", "email": "e2etestX@test.local", "password": "Test12345!"}).encode()
req = urllib.request.Request(
    "http://127.0.0.1:5000/api/v1/auth/register",
    data=data,
    headers={"Content-Type": "application/json", "Accept": "application/json"},
)
try:
    with urllib.request.urlopen(req) as resp:
        print(f"[{resp.status}] register: SUCCESS! {resp.read().decode()[:100]}")
except urllib.error.HTTPError as e:
    print(f"[{e.code}] register: {e.read().decode()[:300]}")
except Exception as e:
    print(f"[ERR] register: {e}")
