"""
Terminal UI for KTU Notes CLI.

Provides the ASCII banner, status messages, and helper output.
Works across Linux, macOS, Windows, and Android Termux.
"""

from rich.console import Console

console = Console()

# ─── ASCII Banner ────────────────────────────────────────────────────────────
# Uses Unicode box-drawing characters that render correctly in most terminals
# including Termux, GNOME Terminal, iTerm2, Windows Terminal, and VS Code.

BANNER_FULL = r"""
 ██╗  ██╗████████╗██╗   ██╗
 ██║ ██╔╝╚══██╔══╝██║   ██║
 █████╔╝    ██║   ██║   ██║
 ██╔═██╗    ██║   ██║   ██║
 ██║  ██╗   ██║   ╚██████╔╝
 ╚═╝  ╚═╝   ╚═╝    ╚═════╝

 ███╗   ██╗ ██████╗ ████████╗███████╗███████╗
 ████╗  ██║██╔═══██╗╚══██╔══╝██╔════╝██╔════╝
 ██╔██╗ ██║██║   ██║   ██║   █████╗  ███████╗
 ██║╚██╗██║██║   ██║   ██║   ██╔══╝  ╚════██║
 ██║ ╚████║╚██████╔╝   ██║   ███████╗███████║
 ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚══════╝╚══════╝
"""

BANNER_COMPACT = "KTU NOTES"

# Minimum width needed for the full banner (art is ~49 chars wide)
MIN_BANNER_WIDTH = 55


def print_banner():
    """
    Display the CLI banner.

    Automatically detects terminal width:
    - Wide terminals (>= 55 cols): full ASCII art
    - Narrow terminals (< 55 cols): compact text header

    Handles rendering errors gracefully (falls back to plain text).
    """
    try:
        term_width = console.size.width
    except Exception:
        term_width = 80

    console.print()

    if term_width >= MIN_BANNER_WIDTH:
        try:
            for line in BANNER_FULL.strip().splitlines():
                console.print(f"  [bold cyan]{line}[/]")
            console.print()
            console.print("  [dim]CLI tool for ktunotes.live[/]")
        except Exception:
            # Fallback if Unicode rendering fails
            console.print(f"  [bold cyan]{BANNER_COMPACT}[/]")
            console.print("  [dim]CLI tool for ktunotes.live[/]")
    else:
        console.print(f"  [bold cyan]{BANNER_COMPACT}[/]")
        console.print("  [dim]CLI tool for ktunotes.live[/]")

    console.print()


# ─── Status Messages ─────────────────────────────────────────────────────────

def show_success(msg: str):
    """Display a styled success message."""
    console.print(f"  [bold green]✔[/] {msg}")


def show_error(msg: str):
    """Display a styled error message."""
    console.print(f"  [bold red]✘[/] {msg}")


def show_info(msg: str):
    """Display a styled info message."""
    console.print(f"  [cyan]→[/] {msg}")
