import logging
import os

import posthog

from r2r.telemetry.events import BaseTelemetryEvent

logger = logging.getLogger(__name__)


class PosthogClient:
    """
    This is a write-only project API key, so it can only create new events. It can't
    read events or any of your other data stored with PostHog, so it's safe to use in public apps.
    """

    def __init__(self, api_key: str, enabled: bool = True):
        self.enabled = enabled
        if self.enabled:
            logger.info(
                "Initializing anonymized telemetry. To disable, set TELEMETRY_ENABLED=false in your environment."
            )
            posthog.project_api_key = api_key
        else:
            posthog.disabled = True
        logger.info(
            f"Posthog telemetry {'enabled' if self.enabled else 'disabled'}"
        )

    def capture(self, event: BaseTelemetryEvent):
        if self.enabled:
            posthog.capture(event.event_id, event.event_type, event.properties)


# Initialize the telemetry client with a flag to enable or disable telemetry
telemetry_enabled = os.getenv("TELEMETRY_ENABLED", "true").lower() in (
    "true",
    "1",
    "t",
)
telemetry_client = PosthogClient(
    api_key="phc_OPBbibOIErCGc4NDLQsOrMuYFTKDmRwXX6qxnTr6zpU",
    enabled=telemetry_enabled,
)
