"""
CLI (Command Line Interface) implementation for user interaction.
"""

from typing import Optional
import sys

from core.io.base import UserInterface


class CLIInterface(UserInterface):
    """Command-line user interface implementation."""
    
    def read_input(self, prompt: Optional[str] = None) -> str:
        """Read input from the user via stdin.
        
        Args:
            prompt: Optional prompt to display.
            
        Returns:
            The user's input as a string.
        """
        if prompt:
            return input(prompt).strip()
        return input().strip()
    
    def write_output(self, text: str) -> None:
        """Write output to stdout.
        
        Args:
            text: The text to display.
        """
        print(text)
    
    def write_error(self, text: str) -> None:
        """Write an error message to stderr.
        
        Args:
            text: The error message.
        """
        print(text, file=sys.stderr)
    
    def write_info(self, text: str) -> None:
        """Write an informational message to stdout.
        
        Args:
            The info message.
        """
        print(text)
    
    def run_session(self, agent, system_prompt: Optional[str] = None) -> None:
        """Run an interactive CLI session.
        
        Args:
            agent: The OpenRouterAgent instance.
            system_prompt: Optional system prompt.
        """
        self.write_info("OpenRouter AI Agent with Tool Calling")
        self.write_info("=" * 50)
        self.write_info("Type 'quit' to exit, 'clear' to clear history, 'history' to view conversation")
        self.write_info("")
        
        super().run_session(agent, system_prompt)