import logging

from django.http import HttpRequest, HttpResponse

from events.models import Event, RSVP

# Logger
logger = logging.getLogger(__name__)

def set_rsvp_cookie(response:HttpResponse, event:Event, rsvp:RSVP):
    response.set_cookie(key=event.slug, value=rsvp.edit_token, expires=event.cookie_expire_datetime)

def get_rsvp_from_cookie(request:HttpRequest, slug:str) -> RSVP | bool:
    logger.debug(f"Cookies: {request.COOKIES}")
    
    edit_token = request.COOKIES.get(slug)
    
    logger.debug(f"RSVP Edit Token: {edit_token}")
    
    rsvp = RSVP.objects.filter(edit_token=edit_token).first()
    if rsvp is not None:
        return rsvp
    
    return False