import os
import requests
from dotenv import load_dotenv

# Load ENV
load_dotenv(dotenv_path="/home/node/clawd/.env", override=True)

def perfect_sync():
    # Credentials
    base_url = "https://app-sod.superoffice.com/Cust55774/api/v1"
    headers = {
        "Authorization": f"Bearer {os.getenv('SO_ACCESS_TOKEN')}", # Will be handled by client if needed, but here direct for speed
        "Content-Type": "application/json",
        "Accept": "application/json"
    }
    
    # We use the SuperOfficeClient to get a fresh token first
    from repos.brancheneinstufung2.connector_superoffice.superoffice_client import SuperOfficeClient
    client = SuperOfficeClient()
    headers["Authorization"] = f"Bearer {client.access_token}"

    print("--- Perfect Sync: Finalizing Robo-Planet (ID 2) ---")
    
    payload = {
        "contactId": 2,
        "Name": "RoboPlanet GmbH-SOD",
        "Number2": "123",
        "UrlAddress": "http://robo-planet.de",
        "OrgNr": "DE400464410",
        "Department": "Perfectly Synchronized",
        "Address": {
            "Postal": {
                "Address1": "Schatzbogen 39",
                "City": "München",
                "Zipcode": "81829"
            },
            "Street": {
                "Address1": "Schatzbogen 39",
                "City": "München",
                "Zipcode": "81829"
            }
        },
        "UserDefinedFields": {
            "SuperOffice:5": "[I:23]" # Logistics
        }
    }
    
    url = f"{base_url}/Contact/2"
    resp = requests.put(url, headers=headers, json=payload)
    
    if resp.status_code == 200:
        print("🚀 BOOM. Website, VAT, Address and Vertical are now 100% correct.")
    else:
        print(f"❌ Error: {resp.text}")

if __name__ == "__main__":
    perfect_sync()
