import sys
import os
import requests
from dotenv import load_dotenv

# Ensure we use the correct config and client
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), 'connector-superoffice')))

from superoffice_client import SuperOfficeClient

def cleanup():
    print("🧹 Cleaning up Test Data...")
    client = SuperOfficeClient()
    
    if not client.access_token:
        print("❌ Auth failed.")
        return

    # Objects to delete (Reverse order of dependency)
    to_delete = [
        ("Sale", 342539),
        ("Appointment", 993350),
        ("Appointment", 993347),
        ("Person", 193092),
        ("Contact", 171185) # Attempting to delete the company too
    ]

    for entity_type, entity_id in to_delete:
        print(f"🗑️ Deleting {entity_type} {entity_id}...")
        try:
            # SuperOffice DELETE usually returns 204 No Content
            # Our client returns None on success if response body is empty, or the JSON if not.
            # We need to catch exceptions if it fails.
            resp = client._delete(f"{entity_type}/{entity_id}")
            print(f"✅ Deleted {entity_type} {entity_id}")
        except Exception as e:
            print(f"⚠️ Failed to delete {entity_type} {entity_id}: {e}")

if __name__ == "__main__":
    cleanup()
