import json
from typing import Any, Dict
from jazzmine.logging.sinks.base_sink import BaseSink
from jazzmine.logging.exceptions import LoggerDependencyError


class HoneycombLogger(BaseSink):
    """Honeycomb.io sink"""
    
    def initialize(self) -> None:
        try:
            import libhoney
            self.libhoney = libhoney
        except ImportError:
            raise LoggerDependencyError(
                "libhoney is required for Honeycomb sink. Install: pip install libhoney"
            )
        
        api_key = self.sink_config.get('api_key')
        dataset = self.sink_config.get('dataset', self.logger_name)
        
        self.libhoney.init(
            writekey=api_key,
            dataset=dataset,
        )
        
        self._initialized = True
    
    def shutdown(self) -> None:
        if self._initialized:
            self.libhoney.close()
    
    def log(self, level: str, msg: str, **kwargs) -> None:
        if not self._initialized:
            return
        
        event = self.libhoney.new_event()
        event.add_field('level', level)
        event.add_field('message', msg)
        event.add_field('logger', self.logger_name)
        
        for key, value in kwargs.items():
            event.add_field(key, value)
        
        try:
            event.send()
        except Exception:
            pass
    
    async def alog(self, level: str, msg: str, **kwargs) -> None:
        # libhoney handles async internally
        self.log(level, msg, **kwargs)
