import requests
import threading
import atexit

class Transporter:
    def __init__(self, bunker_url: str):
        self.url = f"{bunker_url.rstrip('/')}/v1/log"
        self._threads = []

    def send(self, data: dict):
        thread = threading.Thread(target=self._post, args=(data,))
        thread.daemon = True 
        thread.start()
        self._threads.append(thread)
        # Clean up dead threads
        self._threads = [t for t in self._threads if t.is_alive()]

    def _post(self, data: dict):
        try:
            requests.post(self.url, json=data, timeout=2) # Reduced timeout for production
        except Exception as e:
            # In a real production environment, you'd log to a local rotating file here
            pass 

    def flush(self):
        """Wait for all pending threads to finish (max 2s)."""
        for t in self._threads:
            t.join(timeout=2)