"""Status display for RoastBuddy."""

from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from roastbuddy.utils.git_utils import (
    find_git_repo,
    get_last_commit,
    get_recent_commits,
    analyze_commit_patterns
)
from roastbuddy.utils.copilot_utils import is_copilot_available

console = Console()


def show_status():
    """Show current RoastBuddy status."""
    repo = find_git_repo()
    
    if not repo:
        console.print("[red]❌ Not in a git repository![/red]")
        return
    
    # Get commit data
    last_commit = get_last_commit(repo)
    recent_commits = get_recent_commits(repo, count=10)
    patterns = analyze_commit_patterns(repo)
    
    # Check Copilot availability
    copilot = "✅ Available" if is_copilot_available() else "❌ Not available"
    
    # Last commit info
    if last_commit:
        last_commit_text = (
            f"[bold]{last_commit['short_hash']}[/bold] - {last_commit['message'][:40]}...\n"
            f"Files: {last_commit['files_changed']} | "
            f"Lines: +{last_commit['lines_added']} -{last_commit['lines_deleted']}"
        )
    else:
        last_commit_text = "[dim]No commits yet[/dim]"
    
    # Statistics table
    stats_table = Table(show_header=False, box=None)
    stats_table.add_column("Metric", style="cyan")
    stats_table.add_column("Value", style="green")
    
    stats_table.add_row("Total Commits", str(patterns["total_commits"]))
    stats_table.add_row("Clean Commits", f"{patterns['clean_commits']} ({patterns.get('clean_ratio', 0):.0%})")
    stats_table.add_row("Avg Files/Commit", f"{patterns['avg_files_per_commit']:.1f}")
    stats_table.add_row("Avg Lines/Commit", f"{patterns['avg_lines_per_commit']:.0f}")
    stats_table.add_row("GitHub Copilot", copilot)
    
    # Display everything
    console.print(Panel(
        f"[bold cyan]📊 Repository Status[/bold cyan]\n\n"
        f"[bold]Last Commit:[/bold]\n{last_commit_text}\n\n"
        f"[bold]Statistics (last {patterns['total_commits']} commits):[/bold]",
        border_style="cyan"
    ))
    
    console.print(stats_table)
    
    console.print("\n[dim]💡 Try: [bold]roastbuddy roast[/bold] or [bold]roastbuddy praise[/bold][/dim]")
