"""Backward-compatible facade for building DeepthinkEngine via bootstrap."""

from typing import Optional
import signal

from analogai.deepthink.infrastructure.gateways.llm.gateway import LlmGateway
from analogai.deepthink.infrastructure.gateways.llm.enums.llm_provider import LlmProvider
from analogai.deepthink.application.value_objects.user_agent import UserAgent
from analogai.deepthink.messaging.fact.value_objects.source import FactSource
from analogai.deepthink.application.value_objects.search_result import SearchResult
from analogai.deepthink.presentation.preprocessing.previous_messages_stack import PreviousMessagesStack
from analogai.deepthink.messaging.question_answering.enums.synthesized_text_type import (
	SynthesizedTextType,
)
from analogai.deepthink.startup.bootstrap import create_deepthink_engine


class Deepthinker:
	"""Facade class to preserve the public API surface."""

	def __init__(
		self,
		llm_gateway_instance: Optional[LlmGateway] = None,
		llm_provider: Optional[LlmProvider] = None,
		llm_model: Optional[str] = None,
	):
		self._engine = create_deepthink_engine(
			llm_gateway_instance=llm_gateway_instance,
			llm_provider=llm_provider,
			llm_model=llm_model,
		)
		self._install_signal_handlers()

	def close(self) -> None:
		self._engine.close()

	def deepthink(
		self,
		input_text: str,
		user_agent: Optional[UserAgent] = None,
		previous_messages_stack: Optional[PreviousMessagesStack] = None,
	) -> Optional[SearchResult]:
		try:
			return self._engine.deepthink(
				input_text=input_text,
				user_agent=user_agent,
				previous_messages_stack=previous_messages_stack,
			)
		except KeyboardInterrupt:
			self._handle_keyboard_interrupt()
			return None

	def chat(
		self,
		input_text: str,
		user_agent: Optional[UserAgent] = None,
		previous_messages_stack: Optional[PreviousMessagesStack] = None,
		source: FactSource | str | None = None,
	) -> Optional[SearchResult]:
		try:
			return self._engine.chat(
				input_text=input_text,
				user_agent=user_agent,
				previous_messages_stack=previous_messages_stack,
				source=source,
			)
		except KeyboardInterrupt:
			self._handle_keyboard_interrupt()
			return None

	def remember(
		self,
		input_text: str,
		user_agent: Optional[UserAgent] = None,
		previous_messages_stack: Optional[PreviousMessagesStack] = None,
		source: FactSource | str | None = None,
	) -> Optional[str]:
		try:
			return self._engine.remember(
				input_text=input_text,
				user_agent=user_agent,
				previous_messages_stack=previous_messages_stack,
				source=source,
			)
		except KeyboardInterrupt:
			self._handle_keyboard_interrupt()
			return None

	def search(
		self,
		input_text: str,
		user_agent: Optional[UserAgent] = None,
		previous_messages_stack: Optional[PreviousMessagesStack] = None,
		synthesized_text_type: Optional[SynthesizedTextType] = None,
	) -> Optional[SearchResult]:
		try:
			return self._engine.search(
				input_text=input_text,
				user_agent=user_agent,
				previous_messages_stack=previous_messages_stack,
				synthesized_text_type=synthesized_text_type,
			)
		except KeyboardInterrupt:
			self._handle_keyboard_interrupt()
			return None

	def generate_background_info(
		self,
		input_text: str,
		user_agent: Optional[UserAgent] = None,
		previous_messages_stack: Optional[PreviousMessagesStack] = None,
	) -> str:
		try:
			return self._engine.generate_background_info(
				input_text=input_text,
				user_agent=user_agent,
				previous_messages_stack=previous_messages_stack,
			)
		except KeyboardInterrupt:
			self._handle_keyboard_interrupt()
			return ""

	def get_background_info(
		self,
		input_text: str,
		user_agent: Optional[UserAgent] = None,
		previous_messages_stack: Optional[PreviousMessagesStack] = None,
	) -> str:
		try:
			return self._engine.get_background_info(
				input_text=input_text,
				user_agent=user_agent,
				previous_messages_stack=previous_messages_stack,
			)
		except KeyboardInterrupt:
			self._handle_keyboard_interrupt()
			return ""

	@staticmethod
	def _handle_keyboard_interrupt() -> None:
		print("\nExiting...")
		raise SystemExit(0)

	def _install_signal_handlers(self) -> None:
		try:
			signal.signal(signal.SIGINT, lambda *_: self._handle_keyboard_interrupt())
			try:
				signal.siginterrupt(signal.SIGINT, True)
			except Exception:
				pass
		except Exception:
			pass


DeepThinker = Deepthinker
Thinker = Deepthinker



