# bot/handlers/chats.py
from telegram import Update
from telegram.ext import ContextTypes

async def on_my_chat_member(update: Update, context: ContextTypes.DEFAULT_TYPE):
    mcm = update.my_chat_member
    if not mcm: return
    chat = mcm.chat
    # сохранить/обновить TgChat
    from posts.models import TgChat
    TgChat.objects.update_or_create(
        chat_id=chat.id,
        defaults=dict(
            title=chat.title or "",
            username=getattr(chat, "username", "") or "",
            type=chat.type,
            is_active=(mcm.new_chat_member.status in ("administrator","member"))
        )
    )


