"""
Debug Panel - Real-time debugging information in TUI
"""

from textual.widget import Widget
from textual.containers import Vertical
from textual.widgets import Static
from rich.text import Text
from rich.console import RenderableType
from typing import Optional, List
from datetime import datetime


class DebugPanel(Vertical):
    """Debug panel showing real-time flow selection and logic information"""
    
    DEFAULT_CSS = """
    DebugPanel {
        height: 1fr;
        padding: 1;
        background: #1a1a1a;
        border: solid #ff0080;
    }
    
    DebugPanel Static {
        margin-bottom: 0;
    }
    """
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.debug_messages = []
        self.max_messages = 20
    
    def compose(self):
        """Create the debug panel layout"""
        yield Static("DEBUG PANEL", classes="panel-header")
        yield Static(
            "Waiting for flow selection...",
            id="debug-content"
        )
    
    def add_debug_message(self, message: str) -> None:
        """Add a debug message with timestamp"""
        timestamp = datetime.now().strftime("%H:%M:%S.%f")[:-3]
        full_message = f"[{timestamp}] {message}"
        
        self.debug_messages.append(full_message)
        if len(self.debug_messages) > self.max_messages:
            self.debug_messages.pop(0)
        
        self._update_display()
    
    def _update_display(self) -> None:
        """Update the debug display with recent messages"""
        content_widget = self.query_one("#debug-content", Static)
        
        if not self.debug_messages:
            content_widget.update("No debug messages yet...")
            return
        
        # Show recent messages, newest at bottom
        display_text = "\n".join(self.debug_messages[-15:])  # Show last 15 messages
        content_widget.update(Text(display_text, style="white"))
    
    def clear_messages(self) -> None:
        """Clear all debug messages"""
        self.debug_messages.clear()
        self._update_display()