from rich.console import Console
from rich.panel import Panel
from rich.table import Table


def print_summary_table(console: Console, report: dict) -> None:
    table = Table(
        title="Conversion Summary",
        show_header=True,
        header_style="bold cyan",
    )
    table.add_column("File", style="dim", overflow="fold", max_width=50)
    table.add_column("Tokens", justify="right", style="green")
    table.add_column("Status", justify="center")

    for item in report.get("files", []):
        path = item.get("file", "")
        tokens = item.get("tokens", 0)
        status = item.get("status", "?")
        if status == "ok":
            status_str = "[green]✓ ok[/green]"
        else:
            status_str = f"[red]✗ {item.get('error', 'error')}[/red]"
        table.add_row(path, str(tokens), status_str)

    console.print(table)


def print_error_log(console: Console, report: dict) -> None:
    errors = report.get("errors", [])
    if not errors:
        return
    console.print()
    lines = []
    for err in errors:
        file = err.get("file", "?")
        msg = err.get("error", "Unknown error")
        if "permission" in msg.lower():
            msg = "Permission denied"
        elif "encoding" in msg.lower() or "unicode" in msg.lower():
            msg = "Encoding/corruption issue"
        lines.append(f"  [red]•[/red] {file}\n    [dim]{msg}[/dim]")
    panel = Panel(
        "\n".join(lines),
        title="[red]Errors[/red]",
        border_style="red",
    )
    console.print(panel)
