from __future__ import annotations

from typing import TYPE_CHECKING, Literal

import typer
import rich

if TYPE_CHECKING:
    from collections.abc import Callable

console = rich.get_console()
list_app = typer.Typer(
    name="list",
    help="📋 List available resources in the registry",
    rich_markup_mode="rich",
    no_args_is_help=True,
)


@list_app.command(name="models")
def cli_list_models() -> None:
    """
    📋 List all available model organisms in the registry.
    """
    from bench_af.util import list_model_organisms
    _print_resource_list(
        "model organism",
        list_model_organisms,
    )


@list_app.command(name="detectors")
def cli_list_detectors() -> None:
    """
    📋 List all available detectors in the registry.
    """
    from bench_af.util import list_detectors
    _print_resource_list(
        "detector",
        list_detectors,
    )


@list_app.command(name="environments")
def cli_list_environments() -> None:
    """
    📋 List all available environments in the registry.
    """
    from bench_af.util import list_environments
    _print_resource_list(
        "environment",
        list_environments,
    )

@list_app.command(name="configs")
def cli_list_configs() -> None:
    """
    📋 List all available configs in the registry.
    """
    from bench_af.util import list_configs
    _print_resource_list(
        "config",
        list_configs,
    )


@list_app.command(name="all")
def cli_list_all() -> None:
    """
    📋 List all available components in the registry.
    """
    from bench_af.util import list_configs, list_detectors, list_environments, list_model_organisms
    _print_resource_list("model organism", list_model_organisms)
    _print_resource_list("detector", list_detectors)
    _print_resource_list("environment", list_environments)
    _print_resource_list("config", list_configs)


def _print_resource_list(
    resource_type: Literal["model organism", "detector", "environment", "config"],
    list_func: Callable[[], list[str]],
) -> None:
    """
    Helper function to print a list of resources with consistent formatting.

    Args:
        resource_type: Name of the resource
        list_func: Function that returns list of resource names
    """
    console.print(
        f"📋 [bold blue]Available {resource_type.title()}s:[/bold blue]")
    
    try:
        resources = list_func()
    except FileNotFoundError:
        console.print(
            "⚠️  [yellow]You are not in a Bench-AF project. Please run `bench-af status` to see the status of the project.[/yellow]")
        return

    if not resources:
        console.print(
            f"⚠️  [yellow]No {resource_type}s found in the registry.[/yellow]")
        return

    for resource in resources:
        console.print(f"  • [cyan]{resource}[/cyan]")
