"""
Crowe Logic CLI — Branding & Terminal Art

Provides the logo, banner, and styled prompt for the CLI.
"""

import os

# Gold/amber ANSI escape — closest to the gold ring in the logo
GOLD = "\033[38;2;191;166;105m"
WHITE = "\033[97m"
BLACK_BG = "\033[40m"
DIM = "\033[2m"
BOLD = "\033[1m"
RESET = "\033[0m"
CYAN = "\033[36m"

# ── Compact logo mark for the prompt ────────────────────────────
# Uses Unicode block/braille characters for a tiny circular icon
PROMPT_ICON = f"{GOLD}\u25C9{RESET}"  # ◉ gold ring dot

# ── ASCII art banner ────────────────────────────────────────────
# Stencil-style portrait in the gold ring — simplified for terminal
BANNER = f"""{GOLD}
                    ╭━━━━━━━━━━━━━━━━━╮
                ╭━━━╯                   ╰━━━╮
              ╭━╯   {WHITE}▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄{GOLD}     ╰━╮
            ╭━╯   {WHITE}▄████████████████▄{GOLD}      ╰━╮
           ╭╯   {WHITE}▄██▀▀▀▀▀▀▀▀▀▀▀▀▀██▄{GOLD}       ╰╮
          ╭╯   {WHITE}███   ▄▄▄   ▄▄▄   ███{GOLD}        ╰╮
          ┃   {WHITE}████  ▀▄▄▀   ▀▄▄▀  ████{GOLD}        ┃
          ┃   {WHITE}████                ████{GOLD}        ┃
          ┃   {WHITE}████    ▄      ▄    ████{GOLD}        ┃
          ┃    {WHITE}███    ▀▄▄▄▄▄▀    ███{GOLD}         ┃
          ╰╮   {WHITE}███▄            ▄███{GOLD}         ╭╯
           ╰╮   {WHITE}▀████▄▄▄▄▄▄████▀{GOLD}          ╭╯
            ╰━╮   {WHITE}▀▀██████████▀▀{GOLD}         ╭━╯
              ╰━╮                       ╭━╯
                ╰━━━╮               ╭━━━╯
                    ╰━━━━━━━━━━━━━━━╯{RESET}
"""

# ── Minimal banner for compact mode ────────────────────────────
BANNER_COMPACT = f"""{GOLD}  ╭━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╮
  ┃  {WHITE}{BOLD}◉  C R O W E   L O G I C{RESET}{GOLD}              ┃
  ┃  {DIM}Universal AI Agent • gpt-oss-120b{RESET}{GOLD}   ┃
  ╰━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╯{RESET}"""

# ── Title text ──────────────────────────────────────────────────
TITLE = f"""{GOLD}{BOLD}
   ██████╗██████╗  ██████╗ ██╗    ██╗███████╗
  ██╔════╝██╔══██╗██╔═══██╗██║    ██║██╔════╝
  ██║     ██████╔╝██║   ██║██║ █╗ ██║█████╗
  ██║     ██╔══██╗██║   ██║██║███╗██║██╔══╝
  ╚██████╗██║  ██║╚██████╔╝╚███╔███╔╝███████╗
   ╚═════╝╚═╝  ╚═╝ ╚═════╝  ╚══╝╚══╝ ╚══════╝
  {DIM}L O G I C{RESET}{GOLD}                        {DIM}v0.1.0{RESET}
"""

# ── Full welcome screen ────────────────────────────────────────
def welcome_screen(model: str = "gpt-oss-120b", version: str = "0.1.0"):
    """Return the full welcome screen string."""
    return f"""{GOLD}{BOLD}
   ██████╗██████╗  ██████╗ ██╗    ██╗███████╗
  ██╔════╝██╔══██╗██╔═══██╗██║    ██║██╔════╝
  ██║     ██████╔╝██║   ██║██║ █╗ ██║█████╗
  ██║     ██╔══██╗██║   ██║██║███╗██║██╔══╝
  ╚██████╗██║  ██║╚██████╔╝╚███╔███╔╝███████╗
   ╚═════╝╚═╝  ╚═╝ ╚═════╝  ╚══╝╚══╝ ╚══════╝
  {RESET}{GOLD}{DIM}L O G I C{RESET}

  {DIM}Model:     {RESET}{WHITE}{model}{RESET}
  {DIM}Platform:  {RESET}{WHITE}Azure AI Foundry{RESET}
  {DIM}Tools:     {RESET}{WHITE}29 function tools + code interpreter{RESET}
  {DIM}Version:   {RESET}{WHITE}{version}{RESET}

  {DIM}Type naturally. The agent auto-selects tools.{RESET}
  {DIM}Commands: /exit /tools /status /clear{RESET}
{GOLD}  {'━' * 52}{RESET}
"""


def prompt_string() -> str:
    """Return the styled prompt string for input."""
    return f"{GOLD}{BOLD}you {PROMPT_ICON}{RESET} "


def agent_prefix() -> str:
    """Return the styled prefix for agent responses."""
    return f"{GOLD}{BOLD}crowe-logic {PROMPT_ICON}{RESET} "


# ── iTerm2 inline image protocol ───────────────────────────────
def show_inline_image(path: str, width: int = 10):
    """Display the icon inline in iTerm2/compatible terminals."""
    import base64
    if not os.path.exists(path):
        return
    term = os.environ.get("TERM_PROGRAM", "")
    if term not in ("iTerm.app", "WezTerm", "ghostty"):
        return  # Only works in supported terminals
    with open(path, "rb") as f:
        data = base64.b64encode(f.read()).decode()
    # iTerm2 inline image escape sequence
    print(f"\033]1337;File=inline=1;width={width};preserveAspectRatio=1:{data}\a")
