import sys
import os
import requests
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 final_proof(contact_id: int):
    print(f"🚀 Final Data Check for ContactId: {contact_id}")
    try:
        client = SuperOfficeClient()
        # Get RAW text to be 100% safe
        url = f"{client.base_url}/Contact/{contact_id}?$select=Name,UserDefinedFields"
        resp = requests.get(url, headers=client.headers)
        raw_text = resp.text

        print("\n--- 🔍 EVIDENCE CHECK ---")
        print(f"Company Name found: {'Bremer Abenteuerland' in raw_text}")
        
        # Check for the Vertical ID '1628' (Leisure - Indoor Active)
        if '"SuperOffice:83":"[I:1628]"' in raw_text:
            print("✅ SUCCESS: Vertical 'Leisure - Indoor Active' (1628) is correctly set in SuperOffice!")
        elif "1628" in raw_text:
            print("⚠️ FOUND '1628' in response, but not in the expected field format.")
        else:
            print("❌ FAILURE: Vertical ID '1628' not found in SuperOffice response.")

        # Check for Summary (truncated)
        if "Abenteuerland" in raw_text and "SuperOffice:84" in raw_text:
             print("✅ SUCCESS: AI Summary field (SuperOffice:84) seems to contain data.")

        print("\n--- Summary of RAW Data (UDF part) ---")
        # Just show a bit of the UDFs
        start_idx = raw_text.find("UserDefinedFields")
        print(raw_text[start_idx:start_idx+500] + "...")

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

if __name__ == "__main__":
    final_proof(171185)
