
import os
import sys
import json
from dotenv import load_dotenv

# Explicitly load .env
dotenv_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '.env'))
load_dotenv(dotenv_path=dotenv_path, override=True)

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from superoffice_client import SuperOfficeClient

def run_discovery():
    print(f"🚀 Running Full Discovery on PRODUCTION...")
    try:
        client = SuperOfficeClient()
        if not client.access_token: return

        # 1. Check Contact UDFs
        print("\n--- 🏢 CONTACT UDFs (ProgIDs) ---")
        contact_meta = client._get("Contact/default")
        if contact_meta and 'UserDefinedFields' in contact_meta:
             udfs = contact_meta['UserDefinedFields']
             for key in sorted(udfs.keys()):
                 print(f"  - {key}")
        
        # 2. Check Person UDFs
        print("\n--- 👤 PERSON UDFs (ProgIDs) ---")
        person_meta = client._get("Person/default")
        if person_meta and 'UserDefinedFields' in person_meta:
             udfs = person_meta['UserDefinedFields']
             for key in sorted(udfs.keys()):
                 print(f"  - {key}")

        # 3. Check specific List IDs (e.g. Verticals)
        # This often requires admin rights to see all list definitions
        print("\n--- 📋 LIST CHECK (Verticals) ---")
        # Assuming udlist331 is the list for Verticals (based on previous logs)
        list_data = client._get("List/udlist331/Items")
        if list_data and 'value' in list_data:
            print(f"Found {len(list_data['value'])} items in Vertical list.")
            for item in list_data['value'][:5]:
                print(f"  - ID {item['Id']}: {item['Name']}")
        else:
            print("  ⚠️ Could not access Vertical list items.")

        print("\n✅ Discovery Complete.")

    except Exception as e:
        print(f"❌ Error: {e}")

if __name__ == "__main__":
    run_discovery()
