from __future__ import annotations

from sqlalchemy.ext.asyncio import AsyncSession

from shared.events import register_activity_handler
from shared.models.federation import APActivity
from shared.services.registry import services


async def on_user_logged_in(activity: APActivity, session: AsyncSession) -> None:
    data = activity.object_data
    user_id = data["user_id"]
    session_id = data["session_id"]

    if services.has("cart"):
        await services.cart.adopt_cart_for_user(session, user_id, session_id)

    if services.has("calendar"):
        await services.calendar.adopt_entries_for_user(session, user_id, session_id)
        await services.calendar.adopt_tickets_for_user(session, user_id, session_id)


register_activity_handler("rose:Login", on_user_logged_in)
