import os
import json
import logging
import sys
from dotenv import load_dotenv
load_dotenv(override=True)
from superoffice_client import SuperOfficeClient

logging.basicConfig(level=logging.INFO)
sys.stdout.reconfigure(line_buffering=True)

def attempt_send(to_email: str):
    client = SuperOfficeClient()
    
    # Payload for Agents/EMail/Send
    # It expects an array of "EMail" objects
    payload = [
        {
            "To": [{"Value": to_email, "Address": to_email}],
            "Subject": "Test from SuperOffice Agent API",
            "HTMLBody": "<h1>Hello!</h1><p>This is a test from the Agents/EMail/Send endpoint.</p>",
            "From": {"Value": "system@roboplanet.de", "Address": "system@roboplanet.de"} # Try to force a sender
        }
    ]
    
    print(f"🚀 Attempting POST /Agents/EMail/Send to {to_email}...")
    try:
        # Note: The endpoint might be v1/Agents/EMail/Send
        res = client._post("Agents/EMail/Send", payload)
        if res:
            print("✅ Success! Response:", json.dumps(res, indent=2))
        else:
            print("❌ Request failed (None returned).")
    except Exception as e:
        print(f"❌ Exception during send: {e}")

if __name__ == "__main__":
    attempt_send("floke.com@gmail.com")