import os
from typing import Any, Dict, Optional

from posthog import Posthog

_posthog: Posthog = Posthog(
    project_api_key="phc_aUO790jHd7z1SXwsYCz8dRApxueplZlZWeDSpKc5hol", host="https://us.posthog.com"
)


def ph(user_id: str, event: str, properties: Optional[Dict[str, Any]] = None) -> None:
    """Log an event to PostHog.
    :param user_id: user id e.g. github|1234567890
    :param event: The name of the event.
    :param properties: A dictionary of properties to attach to the event.
    """
    if properties is None:
        properties = {}

    properties["environment"] = os.environ.get("ENVIRONMENT", default="None")
    properties["openai_api_type"] = os.environ.get("OPENAI_API_TYPE", default="openai")

    _posthog.capture(distinct_id=user_id, event=event, properties=properties)
