import os
import json
import sys
from dotenv import load_dotenv

# Path gymnastics
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "connector-superoffice"))

from superoffice_client import SuperOfficeClient

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

def debug_update(contact_id):
    client = SuperOfficeClient()
    print(f"--- Hard-Debug: Update Contact {contact_id} ---")
    
    # 1. Fetch full existing object
    contact = client._get(f"Contact/{contact_id}")
    if not contact:
        print("❌ Could not fetch contact.")
        return

    print(f"Current Name: {contact.get('Name')}")
    print(f"Current Dept: {contact.get('Department')}")

    # 2. Modify only one simple field
    contact["Department"] = "AI-Test 13:10"
    
    # 3. PUT it back
    print("Sending full object back with modified Department...")
    result = client._put(f"Contact/{contact_id}", contact)
    
    if result:
        print("✅ API accepted the update.")
        # Verify immediately
        updated = client._get(f"Contact/{contact_id}")
        print(f"New Department in SO: {updated.get('Department')}")
    else:
        print("❌ Update failed.")

if __name__ == "__main__":
    debug_update(2)
