"""Minimal Telethon connect test - run on server to see exact error."""
import asyncio, os, sys
from pathlib import Path

BASE = Path(__file__).parent
for line in (BASE / ".env").read_text(errors="ignore").splitlines():
    line = line.strip()
    if line and not line.startswith("#") and "=" in line:
        k, v = line.split("=", 1)
        os.environ.setdefault(k.strip(), v.strip().strip('"').strip("'"))

from telethon import TelegramClient
from telethon.tl.functions.contacts import ImportContactsRequest
from telethon.tl.types import InputPhoneContact

async def test():
    c = TelegramClient(
        str(BASE / "data/processed/telegram/aima_support_session"),
        int(os.environ["TG_API_ID"]),
        os.environ["TG_API_HASH"]
    )
    print("connecting...", flush=True)
    await c.connect()
    print("authorized:", await c.is_user_authorized(), flush=True)

    # Test ImportContactsRequest on ONE contact
    r = await c(ImportContactsRequest([
        InputPhoneContact(client_id=1, phone="+380000000001", first_name="Test", last_name="")
    ]))
    print(f"ImportContacts: users={len(r.users)} retry={len(getattr(r,'retry_contacts',[]))}", flush=True)
    await c.disconnect()
    print("DONE", flush=True)

asyncio.run(test())
