# Diagnostic: check if a Telegram group has been migrated to a supergroup
# Usage: docker exec telegram-forwarder python3 <this-script>
#
# When a group migrates to a supergroup, the old ID becomes deactivated.
# The new ID is the channel_id from migrated_to, prefixed with -100.

from telethon import TelegramClient
import asyncio, socks

async def check_migration(chat_id: int):
    proxy = (socks.SOCKS5, '127.0.0.1', 7890)
    # Use bot client for checking
    bot = TelegramClient('./sessions/bot', API_ID, API_HASH, proxy=proxy)
    await bot.start()

    try:
        entity = await bot.get_entity(chat_id)
        print(f'Entity type: {type(entity).__name__}')
        print(f'ID: {entity.id}')
        print(f'Title: {getattr(entity, "title", "N/A")}')
        if hasattr(entity, 'deactivated') and entity.deactivated:
            print(f'⚠️  DEACTIVATED - group was migrated!')
            if hasattr(entity, 'migrated_to') and entity.migrated_to:
                new_id = entity.migrated_to.channel_id
                print(f'New channel_id: {new_id}')
                print(f'New Telethon ID: -100{new_id}')
                print(f'Hermes free_response_channels should be: "-100{new_id}"')
        else:
            print('✅ Active - no migration detected')
    except Exception as e:
        print(f'Error: {e}')

    await bot.disconnect()

# Example: check old group ID
# asyncio.run(check_migration(5586190970))
