import webbrowser
import httpx
from urllib.parse import urlparse, parse_qs

CLIENT_ID = "5811038f7c170b60b93fca3727545e6f"
CLIENT_SECRET = input("Paste your Client Secret: ").strip()
STORE = "cx0du9-sq.myshopify.com"

auth_url = (
    f"https://{STORE}/admin/oauth/authorize"
    f"?client_id={CLIENT_ID}"
    f"&scope=read_orders,write_orders,read_draft_orders,write_draft_orders,read_customers"
    f"&redirect_uri=https://example.com"
    f"&state=abc123"
)

print("\nOpening browser — click Allow...")
webbrowser.open(auth_url)

redirect_url = input("\nPaste the full redirect URL: ").strip()
code = parse_qs(urlparse(redirect_url).query)["code"][0]

response = httpx.post(
    f"https://{STORE}/admin/oauth/access_token",
    json={"client_id": CLIENT_ID, "client_secret": CLIENT_SECRET, "code": code},
)

data = response.json()
print("\n✅ Update your .env with this:")
print(f"SHOPIFY_ACCESS_TOKEN={data['access_token']}")