"""Help messages for the CLI."""

from . import console
from .find_subcommands import find_subcommands, warn_if_completion_not_installed

STYLE_OPTION = "bold cyan"
STYLE_SWITCH = "bold green"
STYLE_NEGATIVE_OPTION = "bold magenta"
STYLE_NEGATIVE_SWITCH = "bold red"

STYLE_OPTIONS_TABLE_SHOW_LINES = False
STYLE_OPTIONS_TABLE_LEADING = 0
STYLE_OPTIONS_TABLE_PAD_EDGE = False
STYLE_OPTIONS_TABLE_PADDING = (0, 1)
STYLE_OPTIONS_TABLE_BOX = ""
STYLE_OPTIONS_TABLE_ROW_STYLES = None
STYLE_OPTIONS_TABLE_BORDER_STYLE = None


def print_help():
    """Print help for the main CLI."""

    from rich.panel import Panel  # noqa: PLC0415  lazy loading of rich
    from rich.table import Table  # noqa: PLC0415  lazy loading of rich

    console.print("[yellow]Usage:[/yellow] [bold]slices [OPTIONS] COMMAND [ARGS]...[/bold]")
    console.print()
    console.print("Slices CLI")
    console.print()

    options_table = Table(highlight=False, show_header=False, expand=True, box=None)
    options_table.add_column(style=STYLE_OPTION, no_wrap=True)
    options_table.add_column(style=STYLE_SWITCH, no_wrap=True)
    options_table.add_column(justify="left", no_wrap=False, ratio=10)
    options_table.add_row("--help", "-h", "Show this message and exit.")
    options_table.add_row("--version", "", "Show CLI version.")
    options_table.add_row("--no-upgrade-check", "", "Do not check for newer version.")

    envvar_table = Table(highlight=False, show_header=False, expand=True, box=None)
    envvar_table.add_column(style=STYLE_OPTION, no_wrap=True)
    envvar_table.add_column(justify="left", no_wrap=False, ratio=10)
    envvar_table.add_row("SLICES_CUSTOM_CONFIG", "Override core config with file content.")
    envvar_table.add_row("SLICES_BI_CUSTOM_CONFIG", "Override BI config with file content.")
    envvar_table.add_row("SLICES_DEBUG", "Debug output level. 0, 1, 2 or 3. Default: 0")

    options_panel = Panel(
        options_table,
        title="Options",
        title_align="left",
        border_style="bright_black",
        expand=True,
    )
    envvars_panel = Panel(
        envvar_table,
        title="Environment Variables (advanced)",
        title_align="left",
        border_style="bright_black",
        expand=True,
    )
    # commands_panel = Panel(
    #     f"[{STYLE_OPTION}]clear-cache[/{STYLE_OPTION}]   Remove all cache files.",
    #     title="Commands",
    #     title_align="left",
    #     border_style="bright_black",
    #     expand=True,
    # )
    console.print(options_panel)
    console.print(envvars_panel)
    # console.print(commands_panel)
    subcommands = find_subcommands()
    warn_if_completion_not_installed(subcommands)

    subcommands_table = Table(highlight=False, show_header=False, expand=True, box=None)
    # max_cmd_len = max([len(subcommand.name) for subcommand in subcommands], default=0)
    # add_column(max_width=max_cmd_len
    subcommands_table.add_column(style="bold cyan", no_wrap=True)
    subcommands_table.add_column("Description", justify="left", no_wrap=False, ratio=10)
    subcommands_table.add_row("clear-cache", "Remove all cache files")
    subcommands_table.add_row("completion", "Show or install shell completions")
    for subcommand in subcommands:
        alias_str = ""
        for alias in subcommand.visible_aliases:
            alias_str += f" | {alias}"
        subcommands_table.add_row(subcommand.name + alias_str, subcommand.short_help)

    subcommands_panel = Panel(
        subcommands_table,
        # title="Commands with subcommands",
        title="Commands",
        title_align="left",
        border_style="bright_black",
        expand=True,
    )

    console.print(subcommands_panel)


def print_clear_cache_help():
    """Print help for clear-cache command."""

    from rich.panel import Panel  # noqa: PLC0415  lazy loading of rich

    console.print("[yellow]Usage:[/yellow] [bold]slices clear-cache [OPTIONS][/bold]")
    console.print()
    console.print("Remove all cache files.")
    console.print()

    options_panel = Panel(
        (
            f"[{STYLE_OPTION}]--help[/{STYLE_OPTION}]"
            f"                [{STYLE_SWITCH}]-h[/{STYLE_SWITCH}]"
            "        Show this message and exit."
        ),
        title="Options",
        title_align="left",
        border_style="bright_black",
        expand=True,
    )

    console.print(options_panel)


def print_completion_help():
    """Print help for completion command."""

    from rich.panel import Panel  # noqa: PLC0415  lazy loading of rich

    console.print("[yellow]Usage:[/yellow]")
    console.print("   [bold]slices completion install [OPTIONS][/bold]")
    console.print("   Install completion for the current shell.")
    console.print()
    console.print("   [bold]slices completion show [OPTIONS][/bold]")
    console.print("   Show completion for the current shell, to copy it or customize the installation.")
    console.print()

    options_panel = Panel(
        (
            f"[{STYLE_OPTION}]--help[/{STYLE_OPTION}]"
            f"                [{STYLE_SWITCH}]-h[/{STYLE_SWITCH}]"
            "        Show this message and exit."
        ),
        title="Options",
        title_align="left",
        border_style="bright_black",
        expand=True,
    )

    console.print(options_panel)
