"""History display functionality."""

from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from roastbuddy.core.database import get_recent_interactions
from roastbuddy.utils.git_utils import get_repo_path

console = Console()


def show_history(limit: int = 10):
    """Show roast and praise history."""
    repo_path = get_repo_path()
    interactions = get_recent_interactions(limit=limit, repo_path=repo_path)
    
    if not interactions:
        console.print("[yellow]📜 No history yet![/yellow]")
        console.print("[dim]Use [bold]roastbuddy roast[/bold] or [bold]roastbuddy praise[/bold] first![/dim]")
        return
    
    table = Table(title=f"📜 RoastBuddy History (last {len(interactions)})")
    table.add_column("Type", style="cyan", width=8)
    table.add_column("Content", style="white", width=60)
    table.add_column("Source", style="dim", width=10)
    table.add_column("Time", style="dim", width=16)
    
    for interaction in interactions:
        interaction_type = interaction["interaction_type"]
        emoji = "🔥" if interaction_type == "roast" else "✨"
        type_str = f"{emoji} {interaction_type.title()}"
        
        content = interaction["content"][:60] + "..." if len(interaction["content"]) > 60 else interaction["content"]
        
        source = interaction["source"]
        timestamp = interaction["timestamp"][:16]  # Just date and time
        
        table.add_row(type_str, content, source, timestamp)
    
    console.print(table)
