from livekit import api

from core.config import settings
from datetime import timedelta

def generate_join_token(
    user_id: str,
    group_id: str,
    can_publish: bool
):

    token = api.AccessToken(
        settings.LIVEKIT_API_KEY,
        settings.LIVEKIT_API_SECRET
    )

    token.with_identity(user_id)
    token.with_ttl(timedelta(hours=2))

    token.with_grants(
        api.VideoGrants(
            room=group_id,
            room_join=True,
            can_publish=can_publish,
            can_subscribe=True
        )
    )

    return token.to_jwt()