import time

import rollbar
from django.conf import settings
from intercom.client import Client

from pinecast.post_processing import run_after_request

intercom = Client(personal_access_token=settings.INTERCOM_ACCESS_TOKEN)

@run_after_request
def log(user, event_name, **kw):
    if settings.DEBUG:
        print('Logging {} to intercom'.format(event_name))
    try:
        intercom.events.create(
            event_name=event_name,
            created_at=int(time.time()),
            email=user.email,
            user_id=user.id,
            metadata=kw,
        )
    except Exception as e:
        rollbar.report_exc_info()


__all__ = ['intercom', 'log']
