"""Event sink for persistence and external handlers."""

from __future__ import annotations

import json
import logging
import threading
import time
from typing import Any


class EventSink:
    """Handles event persistence to RAM/disk and optional external handlers."""

    def __init__(self, agent):
        self._agent = agent

    def register_to_auto_file(self, event_type: str, data: Any):
        """Writes to disk record by record if enabled."""
        agent = self._agent
        if not agent.auto_persist:
            return
        with agent.lock:
            agent.record_counter += 1
            if agent.record_counter > agent.max_records_per_file:
                agent.log_version += 1
                agent.current_log_file = f"auto_log_{agent.name}_v{agent.log_version}.json"
                agent.record_counter = 1

            record = {"ts": time.ctime(), "type": event_type, "data": data}
            try:
                with open(agent.current_log_file, "a") as f:
                    f.write(json.dumps(record) + "\n")
            except Exception as e:
                logging.error(f"Error in automatic persistence: {e}")

    def dispatch_event(self, event_type: str, data: Any):
        """Sends event to RAM, Disk and External Handler."""
        agent = self._agent
        with agent.lock:
            if event_type == "SUCCESS":
                agent.outbox.append(data)
            else:
                agent.incident_log.append(data)

        # Save to rotating log
        self.register_to_auto_file(event_type, data)

        # Execute External Handler (in separate thread to avoid blocking)
        if agent.external_handler:
            try:
                if agent.executor:
                    agent.executor.submit(agent.external_handler, event_type, data)
                else:
                    threading.Thread(
                        target=agent.external_handler,
                        args=(event_type, data),
                        daemon=True,
                    ).start()
            except Exception as e:
                logging.error(f"Error in external handler: {e}")
