import os
from dotenv import load_dotenv
import urllib.parse

def generate_url():
    import os
    from dotenv import load_dotenv
    import urllib.parse
    
    # Try current and parent dir
    load_dotenv()
    load_dotenv(dotenv_path="../.env")
    
    client_id = os.getenv("SO_CLIENT_ID")
    # MUST match what is registered in the SuperOffice Developer Portal for this Client ID
    redirect_uri = os.getenv("SO_REDIRECT_URI", "http://localhost")
    state = "roboplanet_prod_init"

    if not client_id:
        print("Fehler: Keine SO_CLIENT_ID in der .env gefunden!")
        return

    params = {
        "client_id": client_id,
        "redirect_uri": redirect_uri,
        "response_type": "code",
        "scope": "openid", # Basic scope
        "state": state
    }
    
    # Use online.superoffice.com for Production
    base_url = "https://online.superoffice.com/login/common/oauth/authorize"
    auth_url = f"{base_url}?{urllib.parse.urlencode(params)}"
    
    print("\n--- PRODUKTIV-AUTH-LINK ---")
    print(f"Mandant: {os.getenv('SO_CONTEXT_IDENTIFIER', 'Cust26720')}")
    print(f"Client ID: {client_id[:5]}...")
    print("-" * 60)
    print(auth_url)
    print("-" * 60)
    print("\n1. Öffne diesen Link im Browser.")
    print("2. Logge dich in deinen ECHTEN Mandanten ein (Cust26720).")
    print("3. Nach der Bestätigung kopiere die URL aus der Adresszeile.")
    print("4. Paste die URL hier in den Chat.")

if __name__ == "__main__":
    generate_url()

