"""
output.py — Color and formatted output helpers.
Wraps colorama for ANSI color support with a plain-text fallback.
"""

try:
    from colorama import init, Fore, Back, Style
    init(autoreset=True)
    COLORAMA_AVAILABLE = True
except ImportError:
    COLORAMA_AVAILABLE = False

import sys


def good(msg):
    """Print a green checkmark success message."""
    if COLORAMA_AVAILABLE:
        print(Fore.GREEN + f"\u2714 {msg}" + Style.RESET_ALL)
    else:
        print(f"[OK] {msg}")


def good2(msg):
    """Print a message with a bright green background (strongly positive)."""
    if COLORAMA_AVAILABLE:
        print(Back.GREEN + Fore.BLACK + f" {msg} " + Style.RESET_ALL)
    else:
        print(f"[GOOD] {msg}")


def bad(msg):
    """Print a red X failure/warning message."""
    if COLORAMA_AVAILABLE:
        print(Fore.RED + f"X {msg}" + Style.RESET_ALL)
    else:
        print(f"[FAIL] {msg}")


def bad2(msg):
    """Print a message with a red background (critical severity)."""
    if COLORAMA_AVAILABLE:
        print(Back.RED + Fore.WHITE + f" {msg} " + Style.RESET_ALL)
    else:
        print(f"[CRIT] {msg}")


def warn(msg):
    """Print a yellow warning message (used for missing deps etc)."""
    if COLORAMA_AVAILABLE:
        print(Fore.YELLOW + f"! {msg}" + Style.RESET_ALL)
    else:
        print(f"[WARN] {msg}")


def info(msg):
    """Print a cyan informational message."""
    if COLORAMA_AVAILABLE:
        print(Fore.CYAN + msg + Style.RESET_ALL)
    else:
        print(msg)


def magenta(msg):
    """Print a magenta message (used for WordPress function labels)."""
    if COLORAMA_AVAILABLE:
        print(Fore.MAGENTA + msg + Style.RESET_ALL)
    else:
        print(msg)


def svc_ok(msg):
    """Print a service running line: [OK]       msg"""
    if COLORAMA_AVAILABLE:
        print(Fore.GREEN + f"[OK]       {msg}" + Style.RESET_ALL)
    else:
        print(f"[OK]       {msg}")


def svc_stopped(msg):
    """Print a service stopped line: [STOPPED]  msg"""
    if COLORAMA_AVAILABLE:
        print(Fore.RED + f"[STOPPED]  {msg}" + Style.RESET_ALL)
    else:
        print(f"[STOPPED]  {msg}")


def sout(label, value):
    """Structured output: print [label] then value on next line."""
    print(f"[{label}]")
    print(value)


def header(msg):
    """Print a bold section header."""
    if COLORAMA_AVAILABLE:
        print(Style.BRIGHT + msg + Style.RESET_ALL)
    else:
        print(f"=== {msg} ===")


def colorama_missing_notice():
    """Warn once if colorama is not available."""
    if not COLORAMA_AVAILABLE:
        print("[WARN] colorama not installed. Output will be plain text.")
        print("       Install with: pip3 install colorama")
